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 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 check_handles(void *arg) {
94 struct cdbtsd *tsd = (struct cdbtsd *)arg;
96 if (tsd->cursor != NULL) {
97 lprintf(1, "cdb_*: cursor still in progress!");
101 if (tsd->tid != NULL) {
102 lprintf(1, "cdb_*: transaction still in progress!");
108 static void dest_tsd(void *arg) {
116 * Ensure that we have a key for thread-specific data. We don't
117 * put anything in here that Citadel cares about; this is just database
118 * related stuff like cursors and transactions.
120 * This should be called immediately after startup by any thread which wants
121 * to use database calls, except for whatever thread calls open_databases.
123 void cdb_allocate_tsd(void) {
126 if (pthread_getspecific(tsdkey) != NULL)
129 tsd = mallok(sizeof *tsd);
133 pthread_setspecific(tsdkey, tsd);
136 void cdb_free_tsd(void) {
137 dest_tsd(pthread_getspecific(tsdkey));
138 pthread_setspecific(tsdkey, NULL);
141 void cdb_check_handles(void) {
142 check_handles(pthread_getspecific(tsdkey));
147 * Reclaim unused space in the databases. We need to do each one of
148 * these discretely, rather than in a loop.
150 * This is a stub function in the Sleepycat DB backend, because there is no
151 * such API call available.
153 void defrag_databases(void)
161 * Request a checkpoint of the database.
163 static void cdb_checkpoint(void) {
166 ret = txn_checkpoint(dbenv,
167 MAX_CHECKPOINT_KBYTES,
168 MAX_CHECKPOINT_MINUTES,
171 lprintf(1, "cdb_checkpoint: txn_checkpoint: %s\n", db_strerror(ret));
177 * Open the various databases we'll be using. Any database which
178 * does not exist should be created. Note that we don't need an S_DATABASE
179 * critical section here, because there aren't any active threads manipulating
180 * the database yet -- and besides, it causes problems on BSDI.
182 void open_databases(void)
186 char dbfilename[SIZ];
189 lprintf(9, "cdb_*: open_databases() starting\n");
191 * Silently try to create the database subdirectory. If it's
192 * already there, no problem.
194 system("exec mkdir data 2>/dev/null");
196 lprintf(9, "cdb_*: Setting up DB environment\n");
197 db_env_set_func_yield(sched_yield);
198 ret = db_env_create(&dbenv, 0);
200 lprintf(1, "cdb_*: db_env_create: %s\n", db_strerror(ret));
203 dbenv->set_errpfx(dbenv, "citserver");
206 * We want to specify the shared memory buffer pool cachesize,
207 * but everything else is the default.
209 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
211 lprintf(1, "cdb_*: set_cachesize: %s\n", db_strerror(ret));
212 dbenv->close(dbenv, 0);
216 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
217 lprintf(1, "cdb_*: set_lk_detect: %s\n", db_strerror(ret));
218 dbenv->close(dbenv, 0);
222 flags = DB_CREATE|DB_RECOVER|DB_INIT_MPOOL|DB_PRIVATE|DB_INIT_TXN|
223 DB_INIT_LOCK|DB_THREAD;
224 ret = dbenv->open(dbenv, "./data", flags, 0);
226 lprintf(1, "cdb_*: dbenv->open: %s\n", db_strerror(ret));
227 dbenv->close(dbenv, 0);
231 lprintf(7, "cdb_*: Starting up DB\n");
233 for (i = 0; i < MAXCDB; ++i) {
235 /* Create a database handle */
236 ret = db_create(&dbp[i], dbenv, 0);
238 lprintf(1, "cdb_*: db_create: %s\n", db_strerror(ret));
243 /* Arbitrary names for our tables -- we reference them by
244 * number, so we don't have string names for them.
246 sprintf(dbfilename, "cdb.%02x", i);
248 ret = dbp[i]->open(dbp[i],
255 lprintf(1, "cdb_*: db_open[%d]: %s\n", i, db_strerror(ret));
260 if ((ret = pthread_key_create(&tsdkey, dest_tsd))) {
261 lprintf(1, "cdb_*: pthread_key_create: %s\n", strerror(ret));
266 CtdlRegisterSessionHook(cdb_checkpoint, EVT_TIMER);
267 lprintf(9, "cdb_*: open_databases() finished\n");
272 * Close all of the db database files we've opened. This can be done
273 * in a loop, since it's just a bunch of closes.
275 void close_databases(void)
282 if ((ret = txn_checkpoint(dbenv, 0, 0, 0))) {
283 lprintf(1, "cdb_*: txn_checkpoint: %s\n", db_strerror(ret));
287 for (a = 0; a < MAXCDB; ++a) {
288 lprintf(7, "cdb_*: Closing database %d\n", a);
289 ret = dbp[a]->close(dbp[a], 0);
291 lprintf(1, "cdb_*: db_close: %s\n", db_strerror(ret));
297 /* Close the handle. */
298 ret = dbenv->close(dbenv, 0);
300 lprintf(1, "cdb_*: DBENV->close: %s\n", db_strerror(ret));
306 * Store a piece of data. Returns 0 if the operation was successful. If a
307 * key already exists it should be overwritten.
309 int cdb_store(int cdb,
310 void *ckey, int ckeylen,
311 void *cdata, int cdatalen)
318 memset(&dkey, 0, sizeof(DBT));
319 memset(&ddata, 0, sizeof(DBT));
322 ddata.size = cdatalen;
326 ret = dbp[cdb]->put(dbp[cdb], /* db */
327 MYTID, /* transaction ID */
332 lprintf(1, "cdb_store(%d): %s\n", cdb,
341 if ((ret = dbp[cdb]->put(dbp[cdb], /* db */
342 tid, /* transaction ID */
346 if (ret == DB_LOCK_DEADLOCK) {
350 lprintf(1, "cdb_store(%d): %s\n", cdb,
363 * Delete a piece of data. Returns 0 if the operation was successful.
365 int cdb_delete(int cdb, void *key, int keylen)
372 memset(&dkey, 0, sizeof dkey);
377 ret = dbp[cdb]->del(dbp[cdb], MYTID, &dkey, 0);
379 lprintf(1, "cdb_delete(%d): %s\n", cdb,
381 if (ret != DB_NOTFOUND)
388 if ((ret = dbp[cdb]->del(dbp[cdb], tid, &dkey, 0))
389 && ret != DB_NOTFOUND) {
390 if (ret == DB_LOCK_DEADLOCK) {
394 lprintf(1, "cdb_delete(%d): %s\n", cdb,
409 * Fetch a piece of data. If not found, returns NULL. Otherwise, it returns
410 * a struct cdbdata which it is the caller's responsibility to free later on
411 * using the cdb_free() routine.
413 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
416 struct cdbdata *tempcdb;
421 memset(&dkey, 0, sizeof(DBT));
422 memset(&dret, 0, sizeof(DBT));
425 dret.flags = DB_DBT_MALLOC;
428 ret = dbp[cdb]->get(dbp[cdb], MYTID, &dkey, &dret, 0);
433 ret = dbp[cdb]->get(dbp[cdb], tid, &dkey, &dret, 0);
435 if (ret == DB_LOCK_DEADLOCK) {
439 if (ret && ret != DB_NOTFOUND)
445 if ((ret != 0) && (ret != DB_NOTFOUND)) {
446 lprintf(1, "cdb_fetch: %s\n", db_strerror(ret));
449 if (ret != 0) return NULL;
450 tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
451 if (tempcdb == NULL) {
452 lprintf(2, "cdb_fetch: Cannot allocate memory!\n");
455 tempcdb->len = dret.size;
456 tempcdb->ptr = dret.data;
462 * Free a cdbdata item (ok, this is really no big deal, but we might need to do
463 * more complex stuff with other database managers in the future).
465 void cdb_free(struct cdbdata *cdb)
473 * Prepare for a sequential search of an entire database.
474 * (There is guaranteed to be no more than one traversal in
475 * progress per thread at any given time.)
477 void cdb_rewind(int cdb)
481 if (MYCURSOR != NULL)
485 lprintf(1, "cdb_rewind: ERROR: cursor use outside transaction\n");
490 * Now initialize the cursor
492 ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &MYCURSOR, 0);
494 lprintf(1, "cdb_rewind: db_cursor: %s\n", db_strerror(ret));
501 * Fetch the next item in a sequential search. Returns a pointer to a
502 * cdbdata structure, or NULL if we've hit the end.
504 struct cdbdata *cdb_next_item(int cdb)
507 struct cdbdata *cdbret;
510 /* Initialize the key/data pair so the flags aren't set. */
511 memset(&key, 0, sizeof(key));
512 memset(&data, 0, sizeof(data));
513 data.flags = DB_DBT_MALLOC;
515 ret = MYCURSOR->c_get(MYCURSOR,
516 &key, &data, DB_NEXT);
519 if (ret != DB_NOTFOUND) {
520 lprintf(1, "cdb_next_item(%d): %s\n",
521 cdb, db_strerror(ret));
526 return NULL; /* presumably, end of file */
529 cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
530 cdbret->len = data.size;
531 cdbret->ptr = data.data;
538 * Transaction-based stuff. I'm writing this as I bake cookies...
541 void cdb_begin_transaction(void) {
543 if (MYTID != NULL) { /* FIXME this slows it down, take it out */
544 lprintf(1, "cdb_begin_transaction: ERROR: opening a new transaction with one already open!\n");
552 void cdb_end_transaction(void) {
553 if (MYCURSOR != NULL) {
554 lprintf(1, "cdb_end_transaction: WARNING: cursor still open at transaction end\n");
559 lprintf(1, "cdb_end_transaction: ERROR: txcommit(NULL) !!\n");