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 DB *dbp[MAXCDB]; /* One DB handle for each Citadel database */
41 DB_ENV *dbenv; /* The DB environment (global) */
43 struct cdbtsd { /* Thread-specific DB stuff */
44 DB_TXN *tid; /* Transaction handle */
45 DBC *cursor; /* Cursor, for traversals... */
50 static pthread_key_t tsdkey;
52 #define MYCURSOR (((struct cdbtsd*)pthread_getspecific(tsdkey))->cursor)
53 #define MYTID (((struct cdbtsd*)pthread_getspecific(tsdkey))->tid)
55 /* just a little helper function */
56 static void txabort(DB_TXN *tid) {
57 int ret = txn_abort(tid);
60 lprintf(1, "cdb_*: txn_abort: %s\n", db_strerror(ret));
65 /* this one is even more helpful than the last. */
66 static void txcommit(DB_TXN *tid) {
67 int ret = txn_commit(tid, 0);
70 lprintf(1, "cdb_*: txn_commit: %s\n", db_strerror(ret));
75 /* are you sensing a pattern yet? */
76 static void txbegin(DB_TXN **tid) {
77 int ret = txn_begin(dbenv, NULL, tid, 0);
80 lprintf(1, "cdb_*: txn_begin: %s\n", db_strerror(ret));
85 static void cclose(DBC *cursor) {
88 if ((ret = cursor->c_close(cursor))) {
89 lprintf(1, "cdb_*: c_close: %s\n", db_strerror(ret));
94 static void check_handles(void *arg) {
96 struct cdbtsd *tsd = (struct cdbtsd *)arg;
98 if (tsd->cursor != NULL) {
99 lprintf(1, "cdb_*: cursor still in progress!");
103 if (tsd->tid != NULL) {
104 lprintf(1, "cdb_*: transaction still in progress!");
110 static void dest_tsd(void *arg) {
118 * Ensure that we have a key for thread-specific data. We don't
119 * put anything in here that Citadel cares about; this is just database
120 * related stuff like cursors and transactions.
122 * This should be called immediately after startup by any thread which wants
123 * to use database calls, except for whatever thread calls open_databases.
125 void cdb_allocate_tsd(void) {
128 if (pthread_getspecific(tsdkey) != NULL)
131 tsd = mallok(sizeof *tsd);
135 pthread_setspecific(tsdkey, tsd);
138 void cdb_free_tsd(void) {
139 dest_tsd(pthread_getspecific(tsdkey));
140 pthread_setspecific(tsdkey, NULL);
143 void cdb_check_handles(void) {
144 check_handles(pthread_getspecific(tsdkey));
149 * Reclaim unused space in the databases. We need to do each one of
150 * these discretely, rather than in a loop.
152 * This is a stub function in the Sleepycat DB backend, because there is no
153 * such API call available.
155 void defrag_databases(void)
162 * Cull the database logs
164 void cdb_cull_logs(void) {
170 lprintf(5, "Database log file cull started.\n");
172 dp = opendir("data");
173 if (dp == NULL) return;
175 while (d = readdir(dp), d != NULL) {
176 if (!strncasecmp(d->d_name, "log.", 4)) {
177 sprintf(filename, "./data/%s", d->d_name);
178 stat(filename, &statbuf);
179 if ((time(NULL) - statbuf.st_mtime) > 432000L) {
180 lprintf(5, "%s ... deleted\n", filename);
184 lprintf(5, "%s ... kept\n", filename);
191 lprintf(5, "Database log file cull ended.\n");
196 * Request a checkpoint of the database.
198 static void cdb_checkpoint(void) {
200 static time_t last_cull = 0L;
202 ret = txn_checkpoint(dbenv,
203 MAX_CHECKPOINT_KBYTES,
204 MAX_CHECKPOINT_MINUTES,
207 lprintf(1, "cdb_checkpoint: txn_checkpoint: %s\n", db_strerror(ret));
212 /* Cull the logs if we haven't done so for 24 hours */
213 if ((time(NULL) - last_cull) > 86400L) {
214 last_cull = time(NULL);
221 * Open the various databases we'll be using. Any database which
222 * does not exist should be created. Note that we don't need an S_DATABASE
223 * critical section here, because there aren't any active threads manipulating
224 * the database yet -- and besides, it causes problems on BSDI.
226 void open_databases(void)
230 char dbfilename[SIZ];
233 lprintf(9, "cdb_*: open_databases() starting\n");
235 * Silently try to create the database subdirectory. If it's
236 * already there, no problem.
238 system("exec mkdir data 2>/dev/null");
240 lprintf(9, "cdb_*: Setting up DB environment\n");
241 db_env_set_func_yield(sched_yield);
242 ret = db_env_create(&dbenv, 0);
244 lprintf(1, "cdb_*: db_env_create: %s\n", db_strerror(ret));
247 dbenv->set_errpfx(dbenv, "citserver");
250 * We want to specify the shared memory buffer pool cachesize,
251 * but everything else is the default.
253 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
255 lprintf(1, "cdb_*: set_cachesize: %s\n", db_strerror(ret));
256 dbenv->close(dbenv, 0);
260 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
261 lprintf(1, "cdb_*: set_lk_detect: %s\n", db_strerror(ret));
262 dbenv->close(dbenv, 0);
266 flags = DB_CREATE|DB_RECOVER|DB_INIT_MPOOL|DB_PRIVATE|DB_INIT_TXN|
267 DB_INIT_LOCK|DB_THREAD;
268 ret = dbenv->open(dbenv, "./data", flags, 0);
270 lprintf(1, "cdb_*: dbenv->open: %s\n", db_strerror(ret));
271 dbenv->close(dbenv, 0);
275 lprintf(7, "cdb_*: Starting up DB\n");
277 for (i = 0; i < MAXCDB; ++i) {
279 /* Create a database handle */
280 ret = db_create(&dbp[i], dbenv, 0);
282 lprintf(1, "cdb_*: db_create: %s\n", db_strerror(ret));
287 /* Arbitrary names for our tables -- we reference them by
288 * number, so we don't have string names for them.
290 sprintf(dbfilename, "cdb.%02x", i);
292 ret = dbp[i]->open(dbp[i],
299 lprintf(1, "cdb_*: db_open[%d]: %s\n", i, db_strerror(ret));
304 if ((ret = pthread_key_create(&tsdkey, dest_tsd))) {
305 lprintf(1, "cdb_*: pthread_key_create: %s\n", strerror(ret));
310 CtdlRegisterSessionHook(cdb_checkpoint, EVT_TIMER);
311 lprintf(9, "cdb_*: open_databases() finished\n");
316 * Close all of the db database files we've opened. This can be done
317 * in a loop, since it's just a bunch of closes.
319 void close_databases(void)
326 if ((ret = txn_checkpoint(dbenv, 0, 0, 0))) {
327 lprintf(1, "cdb_*: txn_checkpoint: %s\n", db_strerror(ret));
331 for (a = 0; a < MAXCDB; ++a) {
332 lprintf(7, "cdb_*: Closing database %d\n", a);
333 ret = dbp[a]->close(dbp[a], 0);
335 lprintf(1, "cdb_*: db_close: %s\n", db_strerror(ret));
341 /* Close the handle. */
342 ret = dbenv->close(dbenv, 0);
344 lprintf(1, "cdb_*: DBENV->close: %s\n", db_strerror(ret));
350 * Store a piece of data. Returns 0 if the operation was successful. If a
351 * key already exists it should be overwritten.
353 int cdb_store(int cdb,
354 void *ckey, int ckeylen,
355 void *cdata, int cdatalen)
362 memset(&dkey, 0, sizeof(DBT));
363 memset(&ddata, 0, sizeof(DBT));
366 ddata.size = cdatalen;
370 ret = dbp[cdb]->put(dbp[cdb], /* db */
371 MYTID, /* transaction ID */
376 lprintf(1, "cdb_store(%d): %s\n", cdb,
385 if ((ret = dbp[cdb]->put(dbp[cdb], /* db */
386 tid, /* transaction ID */
390 if (ret == DB_LOCK_DEADLOCK) {
394 lprintf(1, "cdb_store(%d): %s\n", cdb,
407 * Delete a piece of data. Returns 0 if the operation was successful.
409 int cdb_delete(int cdb, void *key, int keylen)
416 memset(&dkey, 0, sizeof dkey);
421 ret = dbp[cdb]->del(dbp[cdb], MYTID, &dkey, 0);
423 lprintf(1, "cdb_delete(%d): %s\n", cdb,
425 if (ret != DB_NOTFOUND)
432 if ((ret = dbp[cdb]->del(dbp[cdb], tid, &dkey, 0))
433 && ret != DB_NOTFOUND) {
434 if (ret == DB_LOCK_DEADLOCK) {
438 lprintf(1, "cdb_delete(%d): %s\n", cdb,
453 * Fetch a piece of data. If not found, returns NULL. Otherwise, it returns
454 * a struct cdbdata which it is the caller's responsibility to free later on
455 * using the cdb_free() routine.
457 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
460 struct cdbdata *tempcdb;
465 memset(&dkey, 0, sizeof(DBT));
470 memset(&dret, 0, sizeof(DBT));
471 dret.flags = DB_DBT_MALLOC;
472 ret = dbp[cdb]->get(dbp[cdb], MYTID, &dkey, &dret, 0);
475 memset(&dret, 0, sizeof(DBT));
476 dret.flags = DB_DBT_MALLOC;
480 ret = dbp[cdb]->get(dbp[cdb], tid, &dkey, &dret, 0);
482 if (ret == DB_LOCK_DEADLOCK) {
486 if (ret && ret != DB_NOTFOUND)
492 if ((ret != 0) && (ret != DB_NOTFOUND)) {
493 lprintf(1, "cdb_fetch(%d): %s\n", cdb, db_strerror(ret));
496 if (ret != 0) return NULL;
497 tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
498 if (tempcdb == NULL) {
499 lprintf(2, "cdb_fetch: Cannot allocate memory for tempcdb\n");
502 tempcdb->len = dret.size;
503 tempcdb->ptr = dret.data;
509 * Free a cdbdata item (ok, this is really no big deal, but we might need to do
510 * more complex stuff with other database managers in the future).
512 void cdb_free(struct cdbdata *cdb)
520 * Prepare for a sequential search of an entire database.
521 * (There is guaranteed to be no more than one traversal in
522 * progress per thread at any given time.)
524 void cdb_rewind(int cdb)
528 if (MYCURSOR != NULL)
532 lprintf(1, "cdb_rewind: ERROR: cursor use outside transaction\n");
537 * Now initialize the cursor
539 ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &MYCURSOR, 0);
541 lprintf(1, "cdb_rewind: db_cursor: %s\n", db_strerror(ret));
548 * Fetch the next item in a sequential search. Returns a pointer to a
549 * cdbdata structure, or NULL if we've hit the end.
551 struct cdbdata *cdb_next_item(int cdb)
554 struct cdbdata *cdbret;
557 /* Initialize the key/data pair so the flags aren't set. */
558 memset(&key, 0, sizeof(key));
559 memset(&data, 0, sizeof(data));
560 data.flags = DB_DBT_MALLOC;
562 ret = MYCURSOR->c_get(MYCURSOR,
563 &key, &data, DB_NEXT);
566 if (ret != DB_NOTFOUND) {
567 lprintf(1, "cdb_next_item(%d): %s\n",
568 cdb, db_strerror(ret));
573 return NULL; /* presumably, end of file */
576 cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
577 cdbret->len = data.size;
578 cdbret->ptr = data.data;
585 * Transaction-based stuff. I'm writing this as I bake cookies...
588 void cdb_begin_transaction(void) {
590 /******** this check slows it down and is not needed except to debug
592 lprintf(1, "cdb_begin_transaction: ERROR: opening a new transaction with one already open!\n");
595 ***************************/
600 void cdb_end_transaction(void) {
601 if (MYCURSOR != NULL) {
602 lprintf(1, "cdb_end_transaction: WARNING: cursor still open at transaction end\n");
607 lprintf(1, "cdb_end_transaction: ERROR: txcommit(NULL) !!\n");