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 0
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,
215 lprintf(1, "cdb_checkpoint: txn_checkpoint: %s\n", db_strerror(ret));
220 /* Cull the logs if we haven't done so for 24 hours */
221 if ((time(NULL) - last_cull) > 86400L) {
222 last_cull = time(NULL);
229 * Open the various databases we'll be using. Any database which
230 * does not exist should be created. Note that we don't need an S_DATABASE
231 * critical section here, because there aren't any active threads manipulating
232 * the database yet -- and besides, it causes problems on BSDI.
234 void open_databases(void)
238 char dbfilename[SIZ];
241 lprintf(9, "cdb_*: open_databases() starting\n");
243 * Silently try to create the database subdirectory. If it's
244 * already there, no problem.
246 system("exec mkdir data 2>/dev/null");
248 lprintf(9, "cdb_*: Setting up DB environment\n");
249 db_env_set_func_yield(sched_yield);
250 ret = db_env_create(&dbenv, 0);
252 lprintf(1, "cdb_*: db_env_create: %s\n", db_strerror(ret));
255 dbenv->set_errpfx(dbenv, "citserver");
258 * We want to specify the shared memory buffer pool cachesize,
259 * but everything else is the default.
261 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
263 lprintf(1, "cdb_*: set_cachesize: %s\n", db_strerror(ret));
264 dbenv->close(dbenv, 0);
268 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
269 lprintf(1, "cdb_*: set_lk_detect: %s\n", db_strerror(ret));
270 dbenv->close(dbenv, 0);
274 flags = DB_CREATE|DB_RECOVER|DB_INIT_MPOOL|DB_PRIVATE|DB_INIT_TXN|
275 DB_INIT_LOCK|DB_THREAD;
276 ret = dbenv->open(dbenv, "./data", flags, 0);
278 lprintf(1, "cdb_*: dbenv->open: %s\n", db_strerror(ret));
279 dbenv->close(dbenv, 0);
283 lprintf(7, "cdb_*: Starting up DB\n");
285 for (i = 0; i < MAXCDB; ++i) {
287 /* Create a database handle */
288 ret = db_create(&dbp[i], dbenv, 0);
290 lprintf(1, "cdb_*: db_create: %s\n", db_strerror(ret));
295 /* Arbitrary names for our tables -- we reference them by
296 * number, so we don't have string names for them.
298 sprintf(dbfilename, "cdb.%02x", i);
300 ret = dbp[i]->open(dbp[i],
307 lprintf(1, "cdb_*: db_open[%d]: %s\n", i, db_strerror(ret));
312 if ((ret = pthread_key_create(&tsdkey, dest_tsd))) {
313 lprintf(1, "cdb_*: pthread_key_create: %s\n", strerror(ret));
318 CtdlRegisterSessionHook(cdb_checkpoint, EVT_TIMER);
319 lprintf(9, "cdb_*: open_databases() finished\n");
324 * Close all of the db database files we've opened. This can be done
325 * in a loop, since it's just a bunch of closes.
327 void close_databases(void)
334 if ((ret = txn_checkpoint(dbenv, 0, 0, 0))) {
335 lprintf(1, "cdb_*: txn_checkpoint: %s\n", db_strerror(ret));
339 for (a = 0; a < MAXCDB; ++a) {
340 lprintf(7, "cdb_*: Closing database %d\n", a);
341 ret = dbp[a]->close(dbp[a], 0);
343 lprintf(1, "cdb_*: db_close: %s\n", db_strerror(ret));
349 /* Close the handle. */
350 ret = dbenv->close(dbenv, 0);
352 lprintf(1, "cdb_*: DBENV->close: %s\n", db_strerror(ret));
358 * Store a piece of data. Returns 0 if the operation was successful. If a
359 * key already exists it should be overwritten.
361 int cdb_store(int cdb,
362 void *ckey, int ckeylen,
363 void *cdata, int cdatalen)
370 memset(&dkey, 0, sizeof(DBT));
371 memset(&ddata, 0, sizeof(DBT));
374 ddata.size = cdatalen;
379 ret = dbp[cdb]->put(dbp[cdb], /* db */
380 MYTID, /* transaction ID */
386 lprintf(1, "cdb_store(%d): %s\n", cdb,
395 bailIfCursor(MYCURSORS, "attempt to write during r/o cursor");
400 if ((ret = dbp[cdb]->put(dbp[cdb], /* db */
401 tid, /* transaction ID */
406 if (ret == DB_LOCK_DEADLOCK)
413 lprintf(1, "cdb_store(%d): %s\n", cdb,
428 * Delete a piece of data. Returns 0 if the operation was successful.
430 int cdb_delete(int cdb, void *key, int keylen)
437 memset(&dkey, 0, sizeof dkey);
443 ret = dbp[cdb]->del(dbp[cdb], MYTID, &dkey, 0);
446 lprintf(1, "cdb_delete(%d): %s\n", cdb,
448 if (ret != DB_NOTFOUND)
454 bailIfCursor(MYCURSORS, "attempt to delete during r/o cursor");
459 if ((ret = dbp[cdb]->del(dbp[cdb], tid, &dkey, 0))
460 && ret != DB_NOTFOUND)
462 if (ret == DB_LOCK_DEADLOCK)
469 lprintf(1, "cdb_delete(%d): %s\n", cdb,
482 static DBC *localcursor(int cdb)
487 if (MYCURSORS[cdb] == NULL)
488 ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &curs, 0);
490 ret = MYCURSORS[cdb]->c_dup(MYCURSORS[cdb], &curs, DB_POSITION);
494 lprintf(1, "localcursor: %s\n", db_strerror(ret));
503 * Fetch a piece of data. If not found, returns NULL. Otherwise, it returns
504 * a struct cdbdata which it is the caller's responsibility to free later on
505 * using the cdb_free() routine.
507 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
510 struct cdbdata *tempcdb;
514 memset(&dkey, 0, sizeof(DBT));
520 memset(&dret, 0, sizeof(DBT));
521 dret.flags = DB_DBT_MALLOC;
522 ret = dbp[cdb]->get(dbp[cdb], MYTID, &dkey, &dret, 0);
530 memset(&dret, 0, sizeof(DBT));
531 dret.flags = DB_DBT_MALLOC;
533 curs = localcursor(cdb);
535 ret = curs->c_get(curs, &dkey, &dret, DB_SET);
538 while (ret == DB_LOCK_DEADLOCK);
542 if ((ret != 0) && (ret != DB_NOTFOUND))
544 lprintf(1, "cdb_fetch(%d): %s\n", cdb, db_strerror(ret));
548 if (ret != 0) return NULL;
549 tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
553 lprintf(2, "cdb_fetch: Cannot allocate memory for tempcdb\n");
557 tempcdb->len = dret.size;
558 tempcdb->ptr = dret.data;
564 * Free a cdbdata item (ok, this is really no big deal, but we might need to do
565 * more complex stuff with other database managers in the future).
567 void cdb_free(struct cdbdata *cdb)
573 void cdb_close_cursor(int cdb)
575 if (MYCURSORS[cdb] != NULL)
576 cclose(MYCURSORS[cdb]);
578 MYCURSORS[cdb] = NULL;
582 * Prepare for a sequential search of an entire database.
583 * (There is guaranteed to be no more than one traversal in
584 * progress per thread at any given time.)
586 void cdb_rewind(int cdb)
590 if (MYCURSORS[cdb] != NULL)
591 cclose(MYCURSORS[cdb]);
594 * Now initialize the cursor
596 ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &MYCURSORS[cdb], 0);
598 lprintf(1, "cdb_rewind: db_cursor: %s\n", db_strerror(ret));
605 * Fetch the next item in a sequential search. Returns a pointer to a
606 * cdbdata structure, or NULL if we've hit the end.
608 struct cdbdata *cdb_next_item(int cdb)
611 struct cdbdata *cdbret;
614 /* Initialize the key/data pair so the flags aren't set. */
615 memset(&key, 0, sizeof(key));
616 memset(&data, 0, sizeof(data));
617 data.flags = DB_DBT_MALLOC;
619 ret = MYCURSORS[cdb]->c_get(MYCURSORS[cdb],
620 &key, &data, DB_NEXT);
623 if (ret != DB_NOTFOUND) {
624 lprintf(1, "cdb_next_item(%d): %s\n",
625 cdb, db_strerror(ret));
628 cclose(MYCURSORS[cdb]);
629 MYCURSORS[cdb] = NULL;
630 return NULL; /* presumably, end of file */
633 cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
634 cdbret->len = data.size;
635 cdbret->ptr = data.data;
642 * Transaction-based stuff. I'm writing this as I bake cookies...
645 void cdb_begin_transaction(void) {
647 bailIfCursor(MYCURSORS, "can't begin transaction during r/o cursor");
651 lprintf(1, "cdb_begin_transaction: ERROR: nested transaction\n");
658 void cdb_end_transaction(void) {
661 for (i = 0; i < MAXCDB; i++)
662 if (MYCURSORS[i] != NULL) {
663 lprintf(1, "cdb_end_transaction: WARNING: cursor %d still open at transaction end\n", i);
664 cclose(MYCURSORS[i]);
670 lprintf(1, "cdb_end_transaction: ERROR: txcommit(NULL) !!\n");