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 /*****************************************************************************/
28 #include <sys/types.h>
35 #include "citserver.h"
37 #include "sysdep_decls.h"
38 #include "dynloader.h"
40 static DB *dbp[MAXCDB]; /* One DB handle for each Citadel database */
41 static DB_ENV *dbenv; /* The DB environment (global) */
43 struct cdbtsd { /* Thread-specific DB stuff */
44 DB_TXN *tid; /* Transaction handle */
45 DBC *cursors[MAXCDB]; /* Cursors, for traversals... */
48 static pthread_key_t tsdkey;
50 #define MYCURSORS (((struct cdbtsd*)pthread_getspecific(tsdkey))->cursors)
51 #define MYTID (((struct cdbtsd*)pthread_getspecific(tsdkey))->tid)
53 /* just a little helper function */
54 static void txabort(DB_TXN *tid) {
55 int ret = txn_abort(tid);
58 lprintf(1, "cdb_*: txn_abort: %s\n", db_strerror(ret));
63 /* this one is even more helpful than the last. */
64 static void txcommit(DB_TXN *tid) {
65 int ret = txn_commit(tid, 0);
68 lprintf(1, "cdb_*: txn_commit: %s\n", db_strerror(ret));
73 /* are you sensing a pattern yet? */
74 static void txbegin(DB_TXN **tid) {
75 int ret = txn_begin(dbenv, NULL, tid, 0);
78 lprintf(1, "cdb_*: txn_begin: %s\n", db_strerror(ret));
83 static void cclose(DBC *cursor) {
86 if ((ret = cursor->c_close(cursor))) {
87 lprintf(1, "cdb_*: c_close: %s\n", db_strerror(ret));
92 static void bailIfCursor(DBC **cursors, const char *msg)
96 for (i = 0; i < MAXCDB; i++)
97 if (cursors[i] != NULL)
99 lprintf(1, "cdb_*: cursor still in progress on cdb %d: %s\n", i, msg);
104 static void check_handles(void *arg) {
106 struct cdbtsd *tsd = (struct cdbtsd *)arg;
108 bailIfCursor(tsd->cursors, "in check_handles");
110 if (tsd->tid != NULL) {
111 lprintf(1, "cdb_*: transaction still in progress!");
117 static void dest_tsd(void *arg) {
125 * Ensure that we have a key for thread-specific data. We don't
126 * put anything in here that Citadel cares about; this is just database
127 * related stuff like cursors and transactions.
129 * This should be called immediately after startup by any thread which wants
130 * to use database calls, except for whatever thread calls open_databases.
132 void cdb_allocate_tsd(void) {
135 if (pthread_getspecific(tsdkey) != NULL)
138 tsd = mallok(sizeof *tsd);
142 memset(tsd->cursors, 0, sizeof tsd->cursors);
143 pthread_setspecific(tsdkey, tsd);
146 void cdb_free_tsd(void) {
147 dest_tsd(pthread_getspecific(tsdkey));
148 pthread_setspecific(tsdkey, NULL);
151 void cdb_check_handles(void) {
152 check_handles(pthread_getspecific(tsdkey));
157 * Reclaim unused space in the databases. We need to do each one of
158 * these discretely, rather than in a loop.
160 * This is a stub function in the Sleepycat DB backend, because there is no
161 * such API call available.
163 void defrag_databases(void)
170 * Cull the database logs
172 static void cdb_cull_logs(void) {
178 lprintf(5, "Database log file cull started.\n");
180 dp = opendir("data");
181 if (dp == NULL) return;
183 while (d = readdir(dp), d != NULL) {
184 if (!strncasecmp(d->d_name, "log.", 4)) {
185 sprintf(filename, "./data/%s", d->d_name);
186 stat(filename, &statbuf);
187 if ((time(NULL) - statbuf.st_mtime) > 432000L) {
188 lprintf(5, "%s ... deleted\n", filename);
192 lprintf(5, "%s ... kept\n", filename);
199 lprintf(5, "Database log file cull ended.\n");
204 * Request a checkpoint of the database.
206 static void cdb_checkpoint(void) {
208 static time_t last_cull = 0L;
210 ret = txn_checkpoint(dbenv,
211 MAX_CHECKPOINT_KBYTES,
212 MAX_CHECKPOINT_MINUTES,
214 if ( (ret != 0) && (ret != DB_INCOMPLETE) ) {
215 lprintf(1, "cdb_checkpoint: txn_checkpoint: %s\n", db_strerror(ret));
219 if (ret == DB_INCOMPLETE) {
220 lprintf(3, "WARNING: txn_checkpoint: %s\n", db_strerror(ret));
223 /* Cull the logs if we haven't done so for 24 hours */
224 if ((time(NULL) - last_cull) > 86400L) {
225 last_cull = time(NULL);
232 * Open the various databases we'll be using. Any database which
233 * does not exist should be created. Note that we don't need an S_DATABASE
234 * critical section here, because there aren't any active threads manipulating
235 * the database yet -- and besides, it causes problems on BSDI.
237 void open_databases(void)
241 char dbfilename[SIZ];
244 lprintf(9, "cdb_*: open_databases() starting\n");
246 * Silently try to create the database subdirectory. If it's
247 * already there, no problem.
249 system("exec mkdir data 2>/dev/null");
251 lprintf(9, "cdb_*: Setting up DB environment\n");
252 db_env_set_func_yield(sched_yield);
253 ret = db_env_create(&dbenv, 0);
255 lprintf(1, "cdb_*: db_env_create: %s\n", db_strerror(ret));
258 dbenv->set_errpfx(dbenv, "citserver");
261 * We want to specify the shared memory buffer pool cachesize,
262 * but everything else is the default.
264 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
266 lprintf(1, "cdb_*: set_cachesize: %s\n", db_strerror(ret));
267 dbenv->close(dbenv, 0);
271 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
272 lprintf(1, "cdb_*: set_lk_detect: %s\n", db_strerror(ret));
273 dbenv->close(dbenv, 0);
277 flags = DB_CREATE|DB_RECOVER|DB_INIT_MPOOL|DB_PRIVATE|DB_INIT_TXN|
278 DB_INIT_LOCK|DB_THREAD;
279 ret = dbenv->open(dbenv, "./data", flags, 0);
281 lprintf(1, "cdb_*: dbenv->open: %s\n", db_strerror(ret));
282 dbenv->close(dbenv, 0);
286 lprintf(7, "cdb_*: Starting up DB\n");
288 for (i = 0; i < MAXCDB; ++i) {
290 /* Create a database handle */
291 ret = db_create(&dbp[i], dbenv, 0);
293 lprintf(1, "cdb_*: db_create: %s\n", db_strerror(ret));
298 /* Arbitrary names for our tables -- we reference them by
299 * number, so we don't have string names for them.
301 sprintf(dbfilename, "cdb.%02x", i);
303 ret = dbp[i]->open(dbp[i],
310 lprintf(1, "cdb_*: db_open[%d]: %s\n", i, db_strerror(ret));
315 if ((ret = pthread_key_create(&tsdkey, dest_tsd))) {
316 lprintf(1, "cdb_*: pthread_key_create: %s\n", strerror(ret));
321 CtdlRegisterSessionHook(cdb_checkpoint, EVT_TIMER);
322 lprintf(9, "cdb_*: open_databases() finished\n");
327 * Close all of the db database files we've opened. This can be done
328 * in a loop, since it's just a bunch of closes.
330 void close_databases(void)
337 if ((ret = txn_checkpoint(dbenv, 0, 0, 0))) {
338 lprintf(1, "cdb_*: txn_checkpoint: %s\n", db_strerror(ret));
342 for (a = 0; a < MAXCDB; ++a) {
343 lprintf(7, "cdb_*: Closing database %d\n", a);
344 ret = dbp[a]->close(dbp[a], 0);
346 lprintf(1, "cdb_*: db_close: %s\n", db_strerror(ret));
352 /* Close the handle. */
353 ret = dbenv->close(dbenv, 0);
355 lprintf(1, "cdb_*: DBENV->close: %s\n", db_strerror(ret));
361 * Store a piece of data. Returns 0 if the operation was successful. If a
362 * key already exists it should be overwritten.
364 int cdb_store(int cdb,
365 void *ckey, int ckeylen,
366 void *cdata, int cdatalen)
373 memset(&dkey, 0, sizeof(DBT));
374 memset(&ddata, 0, sizeof(DBT));
377 ddata.size = cdatalen;
382 ret = dbp[cdb]->put(dbp[cdb], /* db */
383 MYTID, /* transaction ID */
389 lprintf(1, "cdb_store(%d): %s\n", cdb,
398 bailIfCursor(MYCURSORS, "attempt to write during r/o cursor");
403 if ((ret = dbp[cdb]->put(dbp[cdb], /* db */
404 tid, /* transaction ID */
409 if (ret == DB_LOCK_DEADLOCK)
416 lprintf(1, "cdb_store(%d): %s\n", cdb,
431 * Delete a piece of data. Returns 0 if the operation was successful.
433 int cdb_delete(int cdb, void *key, int keylen)
440 memset(&dkey, 0, sizeof dkey);
446 ret = dbp[cdb]->del(dbp[cdb], MYTID, &dkey, 0);
449 lprintf(1, "cdb_delete(%d): %s\n", cdb,
451 if (ret != DB_NOTFOUND)
457 bailIfCursor(MYCURSORS, "attempt to delete during r/o cursor");
462 if ((ret = dbp[cdb]->del(dbp[cdb], tid, &dkey, 0))
463 && ret != DB_NOTFOUND)
465 if (ret == DB_LOCK_DEADLOCK)
472 lprintf(1, "cdb_delete(%d): %s\n", cdb,
485 static DBC *localcursor(int cdb)
490 if (MYCURSORS[cdb] == NULL)
491 ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &curs, 0);
493 ret = MYCURSORS[cdb]->c_dup(MYCURSORS[cdb], &curs, DB_POSITION);
497 lprintf(1, "localcursor: %s\n", db_strerror(ret));
506 * Fetch a piece of data. If not found, returns NULL. Otherwise, it returns
507 * a struct cdbdata which it is the caller's responsibility to free later on
508 * using the cdb_free() routine.
510 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
513 struct cdbdata *tempcdb;
517 memset(&dkey, 0, sizeof(DBT));
523 memset(&dret, 0, sizeof(DBT));
524 dret.flags = DB_DBT_MALLOC;
525 ret = dbp[cdb]->get(dbp[cdb], MYTID, &dkey, &dret, 0);
533 memset(&dret, 0, sizeof(DBT));
534 dret.flags = DB_DBT_MALLOC;
536 curs = localcursor(cdb);
538 ret = curs->c_get(curs, &dkey, &dret, DB_SET);
541 while (ret == DB_LOCK_DEADLOCK);
545 if ((ret != 0) && (ret != DB_NOTFOUND))
547 lprintf(1, "cdb_fetch(%d): %s\n", cdb, db_strerror(ret));
551 if (ret != 0) return NULL;
552 tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
556 lprintf(2, "cdb_fetch: Cannot allocate memory for tempcdb\n");
560 tempcdb->len = dret.size;
561 tempcdb->ptr = dret.data;
567 * Free a cdbdata item (ok, this is really no big deal, but we might need to do
568 * more complex stuff with other database managers in the future).
570 void cdb_free(struct cdbdata *cdb)
576 void cdb_close_cursor(int cdb)
578 if (MYCURSORS[cdb] != NULL)
579 cclose(MYCURSORS[cdb]);
581 MYCURSORS[cdb] = NULL;
585 * Prepare for a sequential search of an entire database.
586 * (There is guaranteed to be no more than one traversal in
587 * progress per thread at any given time.)
589 void cdb_rewind(int cdb)
593 if (MYCURSORS[cdb] != NULL)
594 cclose(MYCURSORS[cdb]);
597 * Now initialize the cursor
599 ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &MYCURSORS[cdb], 0);
601 lprintf(1, "cdb_rewind: db_cursor: %s\n", db_strerror(ret));
608 * Fetch the next item in a sequential search. Returns a pointer to a
609 * cdbdata structure, or NULL if we've hit the end.
611 struct cdbdata *cdb_next_item(int cdb)
614 struct cdbdata *cdbret;
617 /* Initialize the key/data pair so the flags aren't set. */
618 memset(&key, 0, sizeof(key));
619 memset(&data, 0, sizeof(data));
620 data.flags = DB_DBT_MALLOC;
622 ret = MYCURSORS[cdb]->c_get(MYCURSORS[cdb],
623 &key, &data, DB_NEXT);
626 if (ret != DB_NOTFOUND) {
627 lprintf(1, "cdb_next_item(%d): %s\n",
628 cdb, db_strerror(ret));
631 cclose(MYCURSORS[cdb]);
632 MYCURSORS[cdb] = NULL;
633 return NULL; /* presumably, end of file */
636 cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
637 cdbret->len = data.size;
638 cdbret->ptr = data.data;
645 * Transaction-based stuff. I'm writing this as I bake cookies...
648 void cdb_begin_transaction(void) {
650 bailIfCursor(MYCURSORS, "can't begin transaction during r/o cursor");
654 lprintf(1, "cdb_begin_transaction: ERROR: nested transaction\n");
661 void cdb_end_transaction(void) {
664 for (i = 0; i < MAXCDB; i++)
665 if (MYCURSORS[i] != NULL) {
666 lprintf(1, "cdb_end_transaction: WARNING: cursor %d still open at transaction end\n", i);
667 cclose(MYCURSORS[i]);
673 lprintf(1, "cdb_end_transaction: ERROR: txcommit(NULL) !!\n");