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>
33 #include "citserver.h"
35 #include "sysdep_decls.h"
36 #include "dynloader.h"
38 DB *dbp[MAXCDB]; /* One DB handle for each Citadel database */
39 DB_ENV *dbenv; /* The DB environment (global) */
41 struct cdbtsd { /* Thread-specific DB stuff */
42 DB_TXN *tid; /* Transaction handle */
43 DBC *cursor; /* Cursor, for traversals... */
48 static pthread_key_t tsdkey;
50 #define MYCURSOR (((struct cdbtsd*)pthread_getspecific(tsdkey))->cursor)
51 #define MYTID (((struct cdbtsd*)pthread_getspecific(tsdkey))->tid)
53 /* just a little helper function */
54 static int txabort(DB_TXN *tid) {
55 int ret = txn_abort(tid);
58 lprintf(1, "cdb_*: txn_abort: %s\n", db_strerror(ret));
65 /* this one is even more helpful than the last. */
66 static int txcommit(DB_TXN *tid) {
67 int ret = txn_commit(tid, 0);
70 lprintf(1, "cdb_*: txn_commit: %s\n", db_strerror(ret));
77 /* are you sensing a pattern yet? */
78 static int txbegin(DB_TXN **tid) {
79 int ret = txn_begin(dbenv, NULL, tid, 0);
82 lprintf(1, "cdb_*: txn_begin: %s\n", db_strerror(ret));
89 static void release_handles(void *arg) {
91 struct cdbtsd *tsd = (struct cdbtsd *)arg;
93 if (tsd->cursor != NULL) {
94 lprintf(1, "cdb_*: WARNING: cursor still in progress; "
96 tsd->cursor->c_close(tsd->cursor);
99 if (tsd->tid != NULL) {
100 lprintf(1, "cdb_*: ERROR: transaction still in progress; "
107 static void dest_tsd(void *arg) {
109 release_handles(arg);
115 * Ensure that we have a key for thread-specific data. We don't
116 * put anything in here that Citadel cares about; this is just database
117 * related stuff like cursors and transactions.
119 * This should be called immediately after startup by any thread which wants
120 * to use database calls, except for whatever thread calls open_databases.
122 void cdb_allocate_tsd(void) {
123 struct cdbtsd *tsd = mallok(sizeof *tsd);
127 pthread_setspecific(tsdkey, tsd);
130 void cdb_free_tsd(void) {
131 dest_tsd(pthread_getspecific(tsdkey));
132 pthread_setspecific(tsdkey, NULL);
135 void cdb_release_handles(void) {
136 release_handles(pthread_getspecific(tsdkey));
141 * Reclaim unused space in the databases. We need to do each one of
142 * these discretely, rather than in a loop.
144 * This is a stub function in the Sleepycat DB backend, because there is no
145 * such API call available.
147 void defrag_databases(void)
155 * Request a checkpoint of the database.
157 static void cdb_checkpoint(void) {
160 ret = txn_checkpoint(dbenv,
161 MAX_CHECKPOINT_KBYTES,
162 MAX_CHECKPOINT_MINUTES,
165 lprintf(1, "cdb_checkpoint: txn_checkpoint: %s\n", db_strerror(ret));
171 * Open the various databases we'll be using. Any database which
172 * does not exist should be created. Note that we don't need an S_DATABASE
173 * critical section here, because there aren't any active threads manipulating
174 * the database yet -- and besides, it causes problems on BSDI.
176 void open_databases(void)
180 char dbfilename[SIZ];
183 lprintf(9, "cdb_*: open_databases() starting\n");
185 * Silently try to create the database subdirectory. If it's
186 * already there, no problem.
188 system("exec mkdir data 2>/dev/null");
190 lprintf(9, "cdb_*: Setting up DB environment\n");
191 db_env_set_func_yield(sched_yield);
192 ret = db_env_create(&dbenv, 0);
194 lprintf(1, "cdb_*: db_env_create: %s\n", db_strerror(ret));
197 dbenv->set_errpfx(dbenv, "citserver");
200 * We want to specify the shared memory buffer pool cachesize,
201 * but everything else is the default.
203 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
205 lprintf(1, "cdb_*: set_cachesize: %s\n", db_strerror(ret));
206 dbenv->close(dbenv, 0);
210 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
211 lprintf(1, "cdb_*: set_lk_detect: %s\n", db_strerror(ret));
212 dbenv->close(dbenv, 0);
216 flags = DB_CREATE|DB_RECOVER|DB_INIT_MPOOL|DB_PRIVATE|DB_INIT_TXN|
217 DB_INIT_LOCK|DB_THREAD;
218 ret = dbenv->open(dbenv, "./data", flags, 0);
220 lprintf(1, "cdb_*: dbenv->open: %s\n", db_strerror(ret));
221 dbenv->close(dbenv, 0);
225 lprintf(7, "cdb_*: Starting up DB\n");
227 for (i = 0; i < MAXCDB; ++i) {
229 /* Create a database handle */
230 ret = db_create(&dbp[i], dbenv, 0);
232 lprintf(1, "cdb_*: db_create: %s\n", db_strerror(ret));
237 /* Arbitrary names for our tables -- we reference them by
238 * number, so we don't have string names for them.
240 sprintf(dbfilename, "cdb.%02x", i);
242 ret = dbp[i]->open(dbp[i],
249 lprintf(1, "cdb_*: db_open[%d]: %s\n", i, db_strerror(ret));
254 if ((ret = pthread_key_create(&tsdkey, dest_tsd))) {
255 lprintf(1, "cdb_*: pthread_key_create: %s\n", strerror(ret));
260 CtdlRegisterSessionHook(cdb_checkpoint, EVT_TIMER);
261 lprintf(9, "cdb_*: open_databases() finished\n");
266 * Close all of the db database files we've opened. This can be done
267 * in a loop, since it's just a bunch of closes.
269 void close_databases(void)
276 if ((ret = txn_checkpoint(dbenv, 0, 0, 0))) {
277 lprintf(1, "cdb_*: txn_checkpoint: %s\n", db_strerror(ret));
281 for (a = 0; a < MAXCDB; ++a) {
282 lprintf(7, "cdb_*: Closing database %d\n", a);
283 ret = dbp[a]->close(dbp[a], 0);
285 lprintf(1, "cdb_*: db_close: %s\n", db_strerror(ret));
291 /* Close the handle. */
292 ret = dbenv->close(dbenv, 0);
294 lprintf(1, "cdb_*: DBENV->close: %s\n", db_strerror(ret));
300 * Store a piece of data. Returns 0 if the operation was successful. If a
301 * key already exists it should be overwritten.
303 int cdb_store(int cdb,
304 void *ckey, int ckeylen,
305 void *cdata, int cdatalen)
312 memset(&dkey, 0, sizeof(DBT));
313 memset(&ddata, 0, sizeof(DBT));
316 ddata.size = cdatalen;
320 ret = dbp[cdb]->put(dbp[cdb], /* db */
321 MYTID, /* transaction ID */
326 lprintf(1, "cdb_store(%d): %s\n", cdb,
336 if ((ret = dbp[cdb]->put(dbp[cdb], /* db */
337 tid, /* transaction ID */
341 if (ret == DB_LOCK_DEADLOCK) {
347 lprintf(1, "cdb_store(%d): %s\n", cdb,
352 return txcommit(tid);
359 * Delete a piece of data. Returns 0 if the operation was successful.
361 int cdb_delete(int cdb, void *key, int keylen)
368 memset(&dkey, 0, sizeof dkey);
373 ret = dbp[cdb]->del(dbp[cdb], MYTID, &dkey, 0);
380 if ((ret = dbp[cdb]->del(dbp[cdb], tid, &dkey, 0))) {
381 if (ret == DB_LOCK_DEADLOCK) {
387 lprintf(1, "cdb_delete(%d): %s\n", cdb,
389 if (ret != DB_NOTFOUND) {
394 return txcommit(tid);
404 * Fetch a piece of data. If not found, returns NULL. Otherwise, it returns
405 * a struct cdbdata which it is the caller's responsibility to free later on
406 * using the cdb_free() routine.
408 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
411 struct cdbdata *tempcdb;
416 memset(&dkey, 0, sizeof(DBT));
417 memset(&dret, 0, sizeof(DBT));
420 dret.flags = DB_DBT_MALLOC;
423 ret = dbp[cdb]->get(dbp[cdb], MYTID, &dkey, &dret, 0);
429 ret = dbp[cdb]->get(dbp[cdb], tid, &dkey, &dret, 0);
431 if (ret == DB_LOCK_DEADLOCK) {
436 } else if (txcommit(tid))
440 if ((ret != 0) && (ret != DB_NOTFOUND)) {
441 lprintf(1, "cdb_fetch: %s\n", db_strerror(ret));
444 if (ret != 0) return NULL;
445 tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
446 if (tempcdb == NULL) {
447 lprintf(2, "cdb_fetch: Cannot allocate memory!\n");
450 tempcdb->len = dret.size;
451 tempcdb->ptr = dret.data;
457 * Free a cdbdata item (ok, this is really no big deal, but we might need to do
458 * more complex stuff with other database managers in the future).
460 void cdb_free(struct cdbdata *cdb)
468 * Prepare for a sequential search of an entire database.
469 * (There is guaranteed to be no more than one traversal in
470 * progress per thread at any given time.)
472 void cdb_rewind(int cdb)
476 if (MYCURSOR != NULL)
477 MYCURSOR->c_close(MYCURSOR);
480 lprintf(1, "cdb_rewind: ERROR: cursor use outside transaction\n");
485 * Now initialize the cursor
487 ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &MYCURSOR, 0);
489 lprintf(1, "cdb_rewind: db_cursor: %s\n", db_strerror(ret));
496 * Fetch the next item in a sequential search. Returns a pointer to a
497 * cdbdata structure, or NULL if we've hit the end.
499 struct cdbdata *cdb_next_item(int cdb)
502 struct cdbdata *cdbret;
505 /* Initialize the key/data pair so the flags aren't set. */
506 memset(&key, 0, sizeof(key));
507 memset(&data, 0, sizeof(data));
508 data.flags = DB_DBT_MALLOC;
510 ret = MYCURSOR->c_get(MYCURSOR,
511 &key, &data, DB_NEXT);
514 MYCURSOR->c_close(MYCURSOR);
516 return NULL; /* presumably, end of file */
519 cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
520 cdbret->len = data.size;
521 cdbret->ptr = data.data;
528 * Transaction-based stuff. I'm writing this as I bake cookies...
531 void cdb_begin_transaction(void) {
533 if (MYTID != NULL) { /* FIXME this slows it down, take it out */
534 lprintf(1, "cdb_begin_transaction: ERROR: opening a new transaction with one already open!\n");
542 void cdb_end_transaction(void) {
543 if (MYCURSOR != NULL) {
544 lprintf(1, "cdb_end_transaction: WARNING: cursor still open at transaction end\n");
545 MYCURSOR->c_close(MYCURSOR);
549 lprintf(1, "cdb_end_transaction: ERROR: txcommit(NULL) !!\n");