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, "txn_abort: %s\n", db_strerror(ret));
63 /* this one is even more helpful than the last. */
64 static int txcommit(DB_TXN *tid) {
65 int ret = txn_commit(tid, 0);
68 lprintf(1, "txn_commit: %s\n", db_strerror(ret));
73 /* are you sensing a pattern yet? */
74 static int txbegin(DB_TXN **tid) {
75 int ret = txn_begin(dbenv, NULL, tid, 0);
78 lprintf(1, "txn_begin: %s\n", db_strerror(ret));
83 static void release_handles(void *arg) {
85 struct cdbtsd *tsd = (struct cdbtsd *)arg;
87 if (tsd->cursor != NULL) {
88 lprintf(1, "WARNING: cursor still in progress; "
90 tsd->cursor->c_close(tsd->cursor);
93 if (tsd->tid != NULL) {
94 lprintf(1, "ERROR: transaction still in progress; "
101 static void dest_tsd(void *arg) {
103 release_handles(arg);
109 * Ensure that we have a key for thread-specific data. We don't
110 * put anything in here that Citadel cares about; this is just database
111 * related stuff like cursors and transactions.
113 * This should be called immediately after startup by any thread which wants
114 * to use database calls, except for whatever thread calls open_databases.
116 void cdb_allocate_tsd(void) {
117 struct cdbtsd *tsd = mallok(sizeof *tsd);
121 pthread_setspecific(tsdkey, tsd);
124 void cdb_free_tsd(void) {
125 dest_tsd(pthread_getspecific(tsdkey));
126 pthread_setspecific(tsdkey, NULL);
129 void cdb_release_handles(void) {
130 release_handles(pthread_getspecific(tsdkey));
135 * Reclaim unused space in the databases. We need to do each one of
136 * these discretely, rather than in a loop.
138 * This is a stub function in the Sleepycat DB backend, because there is no
139 * such API call available.
141 void defrag_databases(void)
149 * Request a checkpoint of the database.
151 static void cdb_checkpoint(void) {
154 ret = txn_checkpoint(dbenv,
155 MAX_CHECKPOINT_KBYTES,
156 MAX_CHECKPOINT_MINUTES,
159 lprintf(1, "txn_checkpoint: %s\n", db_strerror(ret));
164 * Open the various databases we'll be using. Any database which
165 * does not exist should be created. Note that we don't need an S_DATABASE
166 * critical section here, because there aren't any active threads manipulating
167 * the database yet -- and besides, it causes problems on BSDI.
169 void open_databases(void)
173 char dbfilename[SIZ];
176 lprintf(9, "open_databases() starting\n");
178 * Silently try to create the database subdirectory. If it's
179 * already there, no problem.
181 system("exec mkdir data 2>/dev/null");
183 lprintf(9, "Setting up DB environment\n");
184 db_env_set_func_yield(sched_yield);
185 ret = db_env_create(&dbenv, 0);
187 lprintf(1, "db_env_create: %s\n", db_strerror(ret));
190 dbenv->set_errpfx(dbenv, "citserver");
193 * We want to specify the shared memory buffer pool cachesize,
194 * but everything else is the default.
196 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
198 lprintf(1, "set_cachesize: %s\n", db_strerror(ret));
199 dbenv->close(dbenv, 0);
203 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
204 lprintf(1, "set_lk_detect: %s\n", db_strerror(ret));
205 dbenv->close(dbenv, 0);
209 flags = DB_CREATE|DB_RECOVER|DB_INIT_MPOOL|DB_PRIVATE|DB_INIT_TXN|
210 DB_INIT_LOCK|DB_THREAD;
211 ret = dbenv->open(dbenv, "./data", flags, 0);
213 lprintf(1, "dbenv->open: %s\n", db_strerror(ret));
214 dbenv->close(dbenv, 0);
218 lprintf(7, "Starting up DB\n");
220 for (i = 0; i < MAXCDB; ++i) {
222 /* Create a database handle */
223 ret = db_create(&dbp[i], dbenv, 0);
225 lprintf(1, "db_create: %s\n", db_strerror(ret));
230 /* Arbitrary names for our tables -- we reference them by
231 * number, so we don't have string names for them.
233 sprintf(dbfilename, "cdb.%02x", i);
235 ret = dbp[i]->open(dbp[i],
242 lprintf(1, "db_open[%d]: %s\n", i, db_strerror(ret));
247 if ((ret = pthread_key_create(&tsdkey, dest_tsd))) {
248 lprintf(1, "pthread_key_create: %s\n", strerror(ret));
253 CtdlRegisterSessionHook(cdb_checkpoint, EVT_TIMER);
254 lprintf(9, "open_databases() finished\n");
259 * Close all of the gdbm database files we've opened. This can be done
260 * in a loop, since it's just a bunch of closes.
262 void close_databases(void)
269 if ((ret = txn_checkpoint(dbenv, 0, 0, 0))) {
270 lprintf(1, "txn_checkpoint: %s\n", db_strerror(ret));
273 for (a = 0; a < MAXCDB; ++a) {
274 lprintf(7, "Closing database %d\n", a);
275 ret = dbp[a]->close(dbp[a], 0);
277 lprintf(1, "db_close: %s\n", db_strerror(ret));
282 /* Close the handle. */
283 ret = dbenv->close(dbenv, 0);
285 lprintf(1, "DBENV->close: %s\n", db_strerror(ret));
290 * Store a piece of data. Returns 0 if the operation was successful. If a
291 * key already exists it should be overwritten.
293 int cdb_store(int cdb,
294 void *ckey, int ckeylen,
295 void *cdata, int cdatalen)
302 memset(&dkey, 0, sizeof(DBT));
303 memset(&ddata, 0, sizeof(DBT));
306 ddata.size = cdatalen;
310 ret = dbp[cdb]->put(dbp[cdb], /* db */
311 MYTID, /* transaction ID */
316 lprintf(1, "cdb_store(%d): %s\n", cdb,
325 if ((ret = dbp[cdb]->put(dbp[cdb], /* db */
326 tid, /* transaction ID */
330 if (ret == DB_LOCK_DEADLOCK) {
336 lprintf(1, "cdb_store(%d): %s\n", cdb,
342 return txcommit(tid);
349 * Delete a piece of data. Returns 0 if the operation was successful.
351 int cdb_delete(int cdb, void *key, int keylen)
362 ret = dbp[cdb]->del(dbp[cdb], MYTID, &dkey, 0);
369 if ((ret = dbp[cdb]->del(dbp[cdb], tid, &dkey, 0))) {
370 if (ret == DB_LOCK_DEADLOCK) {
376 lprintf(1, "cdb_store(%d): %s\n", cdb,
382 return txcommit(tid);
391 * Fetch a piece of data. If not found, returns NULL. Otherwise, it returns
392 * a struct cdbdata which it is the caller's responsibility to free later on
393 * using the cdb_free() routine.
395 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
398 struct cdbdata *tempcdb;
403 memset(&dkey, 0, sizeof(DBT));
404 memset(&dret, 0, sizeof(DBT));
407 dret.flags = DB_DBT_MALLOC;
410 ret = dbp[cdb]->get(dbp[cdb], MYTID, &dkey, &dret, 0);
416 ret = dbp[cdb]->get(dbp[cdb], tid, &dkey, &dret, 0);
418 if (ret == DB_LOCK_DEADLOCK) {
423 } else if (txcommit(tid))
427 if ((ret != 0) && (ret != DB_NOTFOUND)) {
428 lprintf(1, "cdb_fetch: %s\n", db_strerror(ret));
430 if (ret != 0) return NULL;
431 tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
432 if (tempcdb == NULL) {
433 lprintf(2, "Cannot allocate memory!\n");
435 tempcdb->len = dret.size;
436 tempcdb->ptr = dret.data;
442 * Free a cdbdata item (ok, this is really no big deal, but we might need to do
443 * more complex stuff with other database managers in the future).
445 void cdb_free(struct cdbdata *cdb)
453 * Prepare for a sequential search of an entire database.
454 * (There is guaranteed to be no more than one traversal in
455 * progress per thread at any given time.)
457 void cdb_rewind(int cdb)
461 if (MYCURSOR != NULL)
462 MYCURSOR->c_close(MYCURSOR);
465 lprintf(1, "ERROR: cursor use outside transaction\n");
470 * Now initialize the cursor
472 ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &MYCURSOR, 0);
474 lprintf(1, "db_cursor: %s\n", db_strerror(ret));
480 * Fetch the next item in a sequential search. Returns a pointer to a
481 * cdbdata structure, or NULL if we've hit the end.
483 struct cdbdata *cdb_next_item(int cdb)
486 struct cdbdata *cdbret;
489 /* Initialize the key/data pair so the flags aren't set. */
490 memset(&key, 0, sizeof(key));
491 memset(&data, 0, sizeof(data));
492 data.flags = DB_DBT_MALLOC;
494 ret = MYCURSOR->c_get(MYCURSOR,
495 &key, &data, DB_NEXT);
498 MYCURSOR->c_close(MYCURSOR);
500 return NULL; /* presumably, end of file */
503 cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
504 cdbret->len = data.size;
505 cdbret->ptr = data.data;
512 * Transaction-based stuff. I'm writing this as I bake cookies...
515 void cdb_begin_transaction(void) {
517 if (MYTID != NULL) { /* FIXME this slows it down, take it out */
518 lprintf(1, "ERROR: opening a new transaction with one already open!\n");
526 void cdb_end_transaction(void) {
527 if (MYCURSOR != NULL) {
528 lprintf(1, "WARNING: cursor still open at transaction end\n");
529 MYCURSOR->c_close(MYCURSOR);
532 if (MYTID == NULL) lprintf(1, "ERROR: txcommit(NULL) !!\n");
533 else txcommit(MYTID);