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>
45 #include "dynloader.h"
46 #include "citserver.h"
48 #include "sysdep_decls.h"
50 static DB *dbp[MAXCDB]; /* One DB handle for each Citadel database */
51 static DB_ENV *dbenv; /* The DB environment (global) */
53 struct cdbtsd { /* Thread-specific DB stuff */
54 DB_TXN *tid; /* Transaction handle */
55 DBC *cursors[MAXCDB]; /* Cursors, for traversals... */
58 static pthread_key_t tsdkey;
60 #define MYCURSORS (((struct cdbtsd*)pthread_getspecific(tsdkey))->cursors)
61 #define MYTID (((struct cdbtsd*)pthread_getspecific(tsdkey))->tid)
63 /* just a little helper function */
64 static void txabort(DB_TXN *tid) {
65 int ret = txn_abort(tid);
68 lprintf(1, "cdb_*: txn_abort: %s\n", db_strerror(ret));
73 /* this one is even more helpful than the last. */
74 static void txcommit(DB_TXN *tid) {
75 int ret = txn_commit(tid, 0);
78 lprintf(1, "cdb_*: txn_commit: %s\n", db_strerror(ret));
83 /* are you sensing a pattern yet? */
84 static void txbegin(DB_TXN **tid) {
85 int ret = txn_begin(dbenv, NULL, tid, 0);
88 lprintf(1, "cdb_*: txn_begin: %s\n", db_strerror(ret));
93 static void cclose(DBC *cursor) {
96 if ((ret = cursor->c_close(cursor))) {
97 lprintf(1, "cdb_*: c_close: %s\n", db_strerror(ret));
102 static void bailIfCursor(DBC **cursors, const char *msg)
106 for (i = 0; i < MAXCDB; i++)
107 if (cursors[i] != NULL)
109 lprintf(1, "cdb_*: cursor still in progress on cdb %d: %s\n", i, msg);
114 static void check_handles(void *arg) {
116 struct cdbtsd *tsd = (struct cdbtsd *)arg;
118 bailIfCursor(tsd->cursors, "in check_handles");
120 if (tsd->tid != NULL) {
121 lprintf(1, "cdb_*: transaction still in progress!");
127 static void dest_tsd(void *arg) {
135 * Ensure that we have a key for thread-specific data. We don't
136 * put anything in here that Citadel cares about; this is just database
137 * related stuff like cursors and transactions.
139 * This should be called immediately after startup by any thread which wants
140 * to use database calls, except for whatever thread calls open_databases.
142 void cdb_allocate_tsd(void) {
145 if (pthread_getspecific(tsdkey) != NULL)
148 tsd = mallok(sizeof *tsd);
152 memset(tsd->cursors, 0, sizeof tsd->cursors);
153 pthread_setspecific(tsdkey, tsd);
156 void cdb_free_tsd(void) {
157 dest_tsd(pthread_getspecific(tsdkey));
158 pthread_setspecific(tsdkey, NULL);
161 void cdb_check_handles(void) {
162 check_handles(pthread_getspecific(tsdkey));
167 * Reclaim unused space in the databases. We need to do each one of
168 * these discretely, rather than in a loop.
170 * This is a stub function in the Sleepycat DB backend, because there is no
171 * such API call available.
173 void defrag_databases(void)
180 * Cull the database logs
182 static void cdb_cull_logs(void) {
187 lprintf(5, "Database log file cull started.\n");
191 /* Get the list of names. */
192 #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 3
193 if ((ret = log_archive(dbenv, &list, flags, NULL)) != 0) {
195 if ((ret = log_archive(dbenv, &list, flags)) != 0) {
197 lprintf(1, "cdb_cull_logs: %s\n", db_strerror(ret));
201 /* Print the list of names. */
203 for (file = list; *file != NULL; ++file) {
204 lprintf(9, "Deleting log: %s\n", *file);
210 lprintf(5, "Database log file cull ended.\n");
215 * Request a checkpoint of the database.
217 static void cdb_checkpoint(void) {
219 static time_t last_cull = 0L;
221 ret = txn_checkpoint(dbenv,
222 MAX_CHECKPOINT_KBYTES,
223 MAX_CHECKPOINT_MINUTES,
225 if ( (ret != 0) && (ret != DB_INCOMPLETE) ) {
226 lprintf(1, "cdb_checkpoint: txn_checkpoint: %s\n", db_strerror(ret));
230 if (ret == DB_INCOMPLETE) {
231 lprintf(3, "WARNING: txn_checkpoint: %s\n", db_strerror(ret));
234 /* Cull the logs if we haven't done so for 24 hours */
235 if ((time(NULL) - last_cull) > 86400L) {
236 last_cull = time(NULL);
243 * Open the various databases we'll be using. Any database which
244 * does not exist should be created. Note that we don't need an S_DATABASE
245 * critical section here, because there aren't any active threads manipulating
246 * the database yet -- and besides, it causes problems on BSDI.
248 void open_databases(void)
252 char dbfilename[SIZ];
255 lprintf(9, "cdb_*: open_databases() starting\n");
257 * Silently try to create the database subdirectory. If it's
258 * already there, no problem.
260 system("exec mkdir data 2>/dev/null");
262 lprintf(9, "cdb_*: Setting up DB environment\n");
263 db_env_set_func_yield(sched_yield);
264 ret = db_env_create(&dbenv, 0);
266 lprintf(1, "cdb_*: db_env_create: %s\n", db_strerror(ret));
269 dbenv->set_errpfx(dbenv, "citserver");
272 * We want to specify the shared memory buffer pool cachesize,
273 * but everything else is the default.
275 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
277 lprintf(1, "cdb_*: set_cachesize: %s\n", db_strerror(ret));
278 dbenv->close(dbenv, 0);
282 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
283 lprintf(1, "cdb_*: set_lk_detect: %s\n", db_strerror(ret));
284 dbenv->close(dbenv, 0);
288 flags = DB_CREATE|DB_RECOVER|DB_INIT_MPOOL|DB_PRIVATE|DB_INIT_TXN|
289 DB_INIT_LOCK|DB_THREAD;
290 ret = dbenv->open(dbenv, "./data", flags, 0);
292 lprintf(1, "cdb_*: dbenv->open: %s\n", db_strerror(ret));
293 dbenv->close(dbenv, 0);
297 lprintf(7, "cdb_*: Starting up DB\n");
299 for (i = 0; i < MAXCDB; ++i) {
301 /* Create a database handle */
302 ret = db_create(&dbp[i], dbenv, 0);
304 lprintf(1, "cdb_*: db_create: %s\n", db_strerror(ret));
309 /* Arbitrary names for our tables -- we reference them by
310 * number, so we don't have string names for them.
312 sprintf(dbfilename, "cdb.%02x", i);
314 ret = dbp[i]->open(dbp[i],
321 lprintf(1, "cdb_*: db_open[%d]: %s\n", i, db_strerror(ret));
326 if ((ret = pthread_key_create(&tsdkey, dest_tsd))) {
327 lprintf(1, "cdb_*: pthread_key_create: %s\n", strerror(ret));
332 CtdlRegisterSessionHook(cdb_checkpoint, EVT_TIMER);
333 lprintf(9, "cdb_*: open_databases() finished\n");
338 * Close all of the db database files we've opened. This can be done
339 * in a loop, since it's just a bunch of closes.
341 void close_databases(void)
348 if ((ret = txn_checkpoint(dbenv, 0, 0, 0))) {
349 lprintf(1, "cdb_*: txn_checkpoint: %s\n", db_strerror(ret));
353 for (a = 0; a < MAXCDB; ++a) {
354 lprintf(7, "cdb_*: Closing database %d\n", a);
355 ret = dbp[a]->close(dbp[a], 0);
357 lprintf(1, "cdb_*: db_close: %s\n", db_strerror(ret));
363 /* Close the handle. */
364 ret = dbenv->close(dbenv, 0);
366 lprintf(1, "cdb_*: DBENV->close: %s\n", db_strerror(ret));
372 * Store a piece of data. Returns 0 if the operation was successful. If a
373 * key already exists it should be overwritten.
375 int cdb_store(int cdb,
376 void *ckey, int ckeylen,
377 void *cdata, int cdatalen)
384 memset(&dkey, 0, sizeof(DBT));
385 memset(&ddata, 0, sizeof(DBT));
388 ddata.size = cdatalen;
393 ret = dbp[cdb]->put(dbp[cdb], /* db */
394 MYTID, /* transaction ID */
400 lprintf(1, "cdb_store(%d): %s\n", cdb,
409 bailIfCursor(MYCURSORS, "attempt to write during r/o cursor");
414 if ((ret = dbp[cdb]->put(dbp[cdb], /* db */
415 tid, /* transaction ID */
420 if (ret == DB_LOCK_DEADLOCK)
427 lprintf(1, "cdb_store(%d): %s\n", cdb,
442 * Delete a piece of data. Returns 0 if the operation was successful.
444 int cdb_delete(int cdb, void *key, int keylen)
451 memset(&dkey, 0, sizeof dkey);
457 ret = dbp[cdb]->del(dbp[cdb], MYTID, &dkey, 0);
460 lprintf(1, "cdb_delete(%d): %s\n", cdb,
462 if (ret != DB_NOTFOUND)
468 bailIfCursor(MYCURSORS, "attempt to delete during r/o cursor");
473 if ((ret = dbp[cdb]->del(dbp[cdb], tid, &dkey, 0))
474 && ret != DB_NOTFOUND)
476 if (ret == DB_LOCK_DEADLOCK)
483 lprintf(1, "cdb_delete(%d): %s\n", cdb,
496 static DBC *localcursor(int cdb)
501 if (MYCURSORS[cdb] == NULL)
502 ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &curs, 0);
504 ret = MYCURSORS[cdb]->c_dup(MYCURSORS[cdb], &curs, DB_POSITION);
508 lprintf(1, "localcursor: %s\n", db_strerror(ret));
517 * Fetch a piece of data. If not found, returns NULL. Otherwise, it returns
518 * a struct cdbdata which it is the caller's responsibility to free later on
519 * using the cdb_free() routine.
521 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
524 struct cdbdata *tempcdb;
528 memset(&dkey, 0, sizeof(DBT));
534 memset(&dret, 0, sizeof(DBT));
535 dret.flags = DB_DBT_MALLOC;
536 ret = dbp[cdb]->get(dbp[cdb], MYTID, &dkey, &dret, 0);
544 memset(&dret, 0, sizeof(DBT));
545 dret.flags = DB_DBT_MALLOC;
547 curs = localcursor(cdb);
549 ret = curs->c_get(curs, &dkey, &dret, DB_SET);
552 while (ret == DB_LOCK_DEADLOCK);
556 if ((ret != 0) && (ret != DB_NOTFOUND))
558 lprintf(1, "cdb_fetch(%d): %s\n", cdb, db_strerror(ret));
562 if (ret != 0) return NULL;
563 tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
567 lprintf(2, "cdb_fetch: Cannot allocate memory for tempcdb\n");
571 tempcdb->len = dret.size;
572 tempcdb->ptr = dret.data;
578 * Free a cdbdata item (ok, this is really no big deal, but we might need to do
579 * more complex stuff with other database managers in the future).
581 void cdb_free(struct cdbdata *cdb)
587 void cdb_close_cursor(int cdb)
589 if (MYCURSORS[cdb] != NULL)
590 cclose(MYCURSORS[cdb]);
592 MYCURSORS[cdb] = NULL;
596 * Prepare for a sequential search of an entire database.
597 * (There is guaranteed to be no more than one traversal in
598 * progress per thread at any given time.)
600 void cdb_rewind(int cdb)
604 if (MYCURSORS[cdb] != NULL)
605 cclose(MYCURSORS[cdb]);
608 * Now initialize the cursor
610 ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &MYCURSORS[cdb], 0);
612 lprintf(1, "cdb_rewind: db_cursor: %s\n", db_strerror(ret));
619 * Fetch the next item in a sequential search. Returns a pointer to a
620 * cdbdata structure, or NULL if we've hit the end.
622 struct cdbdata *cdb_next_item(int cdb)
625 struct cdbdata *cdbret;
628 /* Initialize the key/data pair so the flags aren't set. */
629 memset(&key, 0, sizeof(key));
630 memset(&data, 0, sizeof(data));
631 data.flags = DB_DBT_MALLOC;
633 ret = MYCURSORS[cdb]->c_get(MYCURSORS[cdb],
634 &key, &data, DB_NEXT);
637 if (ret != DB_NOTFOUND) {
638 lprintf(1, "cdb_next_item(%d): %s\n",
639 cdb, db_strerror(ret));
642 cclose(MYCURSORS[cdb]);
643 MYCURSORS[cdb] = NULL;
644 return NULL; /* presumably, end of file */
647 cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
648 cdbret->len = data.size;
649 cdbret->ptr = data.data;
656 * Transaction-based stuff. I'm writing this as I bake cookies...
659 void cdb_begin_transaction(void) {
661 bailIfCursor(MYCURSORS, "can't begin transaction during r/o cursor");
665 lprintf(1, "cdb_begin_transaction: ERROR: nested transaction\n");
672 void cdb_end_transaction(void) {
675 for (i = 0; i < MAXCDB; i++)
676 if (MYCURSORS[i] != NULL) {
677 lprintf(1, "cdb_end_transaction: WARNING: cursor %d still open at transaction end\n", i);
678 cclose(MYCURSORS[i]);
684 lprintf(1, "cdb_end_transaction: ERROR: txcommit(NULL) !!\n");