From a3585c5d0d780f6680bf66bb807ae375b9591cb2 Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Fri, 11 Aug 2023 11:14:09 -0900 Subject: [PATCH] More renaming of symbols that are local to the berkeley_db module. dbp[] is now bdb_table[] dbenv is now bdb_env --- .../server/backends/berkeley_db/berkeley_db.c | 121 +++++++++--------- 1 file changed, 63 insertions(+), 58 deletions(-) diff --git a/citadel/server/backends/berkeley_db/berkeley_db.c b/citadel/server/backends/berkeley_db/berkeley_db.c index 650279296..62b8738f6 100644 --- a/citadel/server/backends/berkeley_db/berkeley_db.c +++ b/citadel/server/backends/berkeley_db/berkeley_db.c @@ -26,11 +26,11 @@ #endif // Globals (these are used across multiple functions in *this* module, but not elsewhere) -static DB *dbp[MAXCDB]; // One DB handle for each Citadel database -static DB_ENV *dbenv; // The DB environment (global) +static DB *bdb_table[MAXCDB]; // One DB handle for each Citadel database +static DB_ENV *bdb_env; // The DB environment (global) -// Thread-Specific-Storage items for this backend +// These are items that we need to store "per thread" rather than "per session". struct bdb_tsd { DB_TXN *tid; // Transaction handle DBC *cursors[MAXCDB]; // Cursors, for traversals... @@ -41,7 +41,8 @@ pthread_key_t bdb_thread_key; #define TSD bdb_get_tsd() -// Return a pointer to our thread-specific (not session-specific) data. +// Some functions in this backend need to store some per-thread data. +// This returns the pointer to the current thread's per-thread data block, creating it if necessary. struct bdb_tsd *bdb_get_tsd(void) { struct bdb_tsd *c = (struct bdb_tsd *) pthread_getspecific(bdb_thread_key) ; @@ -68,7 +69,7 @@ void bdb_abort(void) { // Verbose logging callback for Berkeley DB. Not part of the backend API. -void bdb_verbose_log(const DB_ENV *dbenv, const char *msg, const char *foo) { +void bdb_verbose_log(const DB_ENV *bdb_env, const char *msg, const char *foo) { if (!IsEmptyStr(msg)) { syslog(LOG_DEBUG, "bdb: %s %s", msg, foo); } @@ -76,7 +77,7 @@ void bdb_verbose_log(const DB_ENV *dbenv, const char *msg, const char *foo) { // Verbose error logging callback for Berkeley DB. Not part of the backend API. -void bdb_verbose_err(const DB_ENV *dbenv, const char *errpfx, const char *msg) { +void bdb_verbose_err(const DB_ENV *bdb_env, const char *errpfx, const char *msg) { syslog(LOG_ERR, "bdb: %s", msg); } @@ -111,7 +112,7 @@ static void bdb_txcommit(DB_TXN *tid) { static void bdb_txbegin(DB_TXN **tid) { int ret; - ret = dbenv->txn_begin(dbenv, NULL, tid, 0); + ret = bdb_env->txn_begin(bdb_env, NULL, tid, 0); if (ret) { syslog(LOG_ERR, "bdb: txn_begin: %s", db_strerror(ret)); @@ -166,7 +167,7 @@ void bdb_checkpoint(void) { int ret; syslog(LOG_DEBUG, "bdb: -- checkpoint --"); - ret = dbenv->txn_checkpoint(dbenv, MAX_CHECKPOINT_KBYTES, MAX_CHECKPOINT_MINUTES, 0); + ret = bdb_env->txn_checkpoint(bdb_env, MAX_CHECKPOINT_KBYTES, MAX_CHECKPOINT_MINUTES, 0); if (ret != 0) { syslog(LOG_ERR, "bdb: bdb_checkpoint() txn_checkpoint: %s", db_strerror(ret)); @@ -175,15 +176,15 @@ void bdb_checkpoint(void) { // After a successful checkpoint, we can cull the unused logs if (CtdlGetConfigInt("c_auto_cull")) { - ret = dbenv->log_set_config(dbenv, DB_LOG_AUTO_REMOVE, 1); + ret = bdb_env->log_set_config(bdb_env, DB_LOG_AUTO_REMOVE, 1); } else { - ret = dbenv->log_set_config(dbenv, DB_LOG_AUTO_REMOVE, 0); + ret = bdb_env->log_set_config(bdb_env, DB_LOG_AUTO_REMOVE, 0); } } -// Open the various databases we'll be using. Any database which +// Open the various tables we'll be using. Any table which // does not exist should be created. Note that we don't need a // critical section here, because there aren't any active threads // manipulating the database yet. @@ -210,61 +211,61 @@ void bdb_open_databases(void) { } syslog(LOG_DEBUG, "bdb: Setting up DB environment"); - ret = db_env_create(&dbenv, 0); + ret = db_env_create(&bdb_env, 0); if (ret) { syslog(LOG_ERR, "bdb: db_env_create: %s", db_strerror(ret)); syslog(LOG_ERR, "bdb: exit code %d", ret); exit(CTDLEXIT_DB); } - dbenv->set_errpfx(dbenv, "citserver"); - dbenv->set_paniccall(dbenv, bdb_dbpanic); - dbenv->set_errcall(dbenv, bdb_verbose_err); - dbenv->set_msgcall(dbenv, bdb_verbose_log); - dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK, 1); - dbenv->set_verbose(dbenv, DB_VERB_RECOVERY, 1); + bdb_env->set_errpfx(bdb_env, "citserver"); + bdb_env->set_paniccall(bdb_env, bdb_dbpanic); + bdb_env->set_errcall(bdb_env, bdb_verbose_err); + bdb_env->set_msgcall(bdb_env, bdb_verbose_log); + bdb_env->set_verbose(bdb_env, DB_VERB_DEADLOCK, 1); + bdb_env->set_verbose(bdb_env, DB_VERB_RECOVERY, 1); // We want to specify the shared memory buffer pool cachesize, but everything else is the default. - ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0); + ret = bdb_env->set_cachesize(bdb_env, 0, 64 * 1024, 0); if (ret) { syslog(LOG_ERR, "bdb: set_cachesize: %s", db_strerror(ret)); - dbenv->close(dbenv, 0); + bdb_env->close(bdb_env, 0); syslog(LOG_ERR, "bdb: exit code %d", ret); exit(CTDLEXIT_DB); } - if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) { + if ((ret = bdb_env->set_lk_detect(bdb_env, DB_LOCK_DEFAULT))) { syslog(LOG_ERR, "bdb: set_lk_detect: %s", db_strerror(ret)); - dbenv->close(dbenv, 0); + bdb_env->close(bdb_env, 0); syslog(LOG_ERR, "bdb: exit code %d", ret); exit(CTDLEXIT_DB); } flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_TXN | DB_INIT_LOCK | DB_THREAD | DB_INIT_LOG; - syslog(LOG_DEBUG, "bdb: dbenv->open(dbenv, %s, %d, 0)", ctdl_db_dir, flags); - ret = dbenv->open(dbenv, ctdl_db_dir, flags, 0); // try opening the database cleanly + syslog(LOG_DEBUG, "bdb: bdb_env->open(bdb_env, %s, %d, 0)", ctdl_db_dir, flags); + ret = bdb_env->open(bdb_env, ctdl_db_dir, flags, 0); // try opening the database cleanly if (ret == DB_RUNRECOVERY) { - syslog(LOG_ERR, "bdb: dbenv->open: %s", db_strerror(ret)); + syslog(LOG_ERR, "bdb: bdb_env->open: %s", db_strerror(ret)); syslog(LOG_ERR, "bdb: attempting recovery..."); flags |= DB_RECOVER; - ret = dbenv->open(dbenv, ctdl_db_dir, flags, 0); // try recovery + ret = bdb_env->open(bdb_env, ctdl_db_dir, flags, 0); // try recovery } if (ret == DB_RUNRECOVERY) { - syslog(LOG_ERR, "bdb: dbenv->open: %s", db_strerror(ret)); + syslog(LOG_ERR, "bdb: bdb_env->open: %s", db_strerror(ret)); syslog(LOG_ERR, "bdb: attempting catastrophic recovery..."); flags &= ~DB_RECOVER; flags |= DB_RECOVER_FATAL; - ret = dbenv->open(dbenv, ctdl_db_dir, flags, 0); // try catastrophic recovery + ret = bdb_env->open(bdb_env, ctdl_db_dir, flags, 0); // try catastrophic recovery } if (ret) { - syslog(LOG_ERR, "bdb: dbenv->open: %s", db_strerror(ret)); - dbenv->close(dbenv, 0); + syslog(LOG_ERR, "bdb: bdb_env->open: %s", db_strerror(ret)); + bdb_env->close(bdb_env, 0); syslog(LOG_ERR, "bdb: exit code %d", ret); exit(CTDLEXIT_DB); } syslog(LOG_INFO, "bdb: mounting databases"); for (i = 0; i < MAXCDB; ++i) { - ret = db_create(&dbp[i], dbenv, 0); // Create a database handle + ret = db_create(&bdb_table[i], bdb_env, 0); // Create a database handle if (ret) { syslog(LOG_ERR, "bdb: db_create: %s", db_strerror(ret)); syslog(LOG_ERR, "bdb: exit code %d", ret); @@ -272,7 +273,7 @@ void bdb_open_databases(void) { } snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", i); // table names by number - ret = dbp[i]->open(dbp[i], NULL, dbfilename, NULL, DB_BTREE, DB_CREATE | DB_AUTO_COMMIT | DB_THREAD, 0600); + ret = bdb_table[i]->open(bdb_table[i], NULL, dbfilename, NULL, DB_BTREE, DB_CREATE | DB_AUTO_COMMIT | DB_THREAD, 0600); if (ret) { syslog(LOG_ERR, "bdb: db_open[%02x]: %s", i, db_strerror(ret)); if (ret == ENOMEM) { @@ -297,12 +298,12 @@ void bdb_close_databases(void) { closing = 1; syslog(LOG_INFO, "bdb: performing final checkpoint"); - if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, 0))) { + if ((ret = bdb_env->txn_checkpoint(bdb_env, 0, 0, 0))) { syslog(LOG_ERR, "bdb: txn_checkpoint: %s", db_strerror(ret)); } syslog(LOG_INFO, "bdb: flushing the database logs"); - if ((ret = dbenv->log_flush(dbenv, NULL))) { + if ((ret = bdb_env->log_flush(bdb_env, NULL))) { syslog(LOG_ERR, "bdb: log_flush: %s", db_strerror(ret)); } @@ -310,14 +311,14 @@ void bdb_close_databases(void) { syslog(LOG_INFO, "bdb: closing databases"); for (i = 0; i < MAXCDB; ++i) { syslog(LOG_INFO, "bdb: closing database %02x", i); - ret = dbp[i]->close(dbp[i], 0); + ret = bdb_table[i]->close(bdb_table[i], 0); if (ret) { syslog(LOG_ERR, "bdb: db_close: %s", db_strerror(ret)); } } // Close the handle. - ret = dbenv->close(dbenv, DB_FORCESYNC); + ret = bdb_env->close(bdb_env, DB_FORCESYNC); if (ret) { syslog(LOG_ERR, "bdb: DBENV->close: %s", db_strerror(ret)); } @@ -406,11 +407,11 @@ int bdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen) } if (TSD->tid != NULL) { - ret = dbp[cdb]->put(dbp[cdb], // db - TSD->tid, // transaction ID - &dkey, // key - &ddata, // data - 0 // flags + ret = bdb_table[cdb]->put(bdb_table[cdb], // db + TSD->tid, // transaction ID + &dkey, // key + &ddata, // data + 0 // flags ); if (ret) { syslog(LOG_ERR, "bdb: bdb_store(%d): %s", cdb, db_strerror(ret)); @@ -427,11 +428,11 @@ int bdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen) retry: bdb_txbegin(&tid); - if ((ret = dbp[cdb]->put(dbp[cdb], // db - tid, // transaction ID - &dkey, // key - &ddata, // data - 0))) { // flags + if ((ret = bdb_table[cdb]->put(bdb_table[cdb], // db + tid, // transaction ID + &dkey, // key + &ddata, // data + 0))) { // flags if (ret == DB_LOCK_DEADLOCK) { bdb_txabort(tid); goto retry; @@ -464,7 +465,7 @@ int bdb_delete(int cdb, void *key, int keylen) { dkey.data = key; if (TSD->tid != NULL) { - ret = dbp[cdb]->del(dbp[cdb], TSD->tid, &dkey, 0); + ret = bdb_table[cdb]->del(bdb_table[cdb], TSD->tid, &dkey, 0); if (ret) { syslog(LOG_ERR, "bdb: bdb_delete(%d): %s", cdb, db_strerror(ret)); if (ret != DB_NOTFOUND) { @@ -478,7 +479,7 @@ int bdb_delete(int cdb, void *key, int keylen) { retry: bdb_txbegin(&tid); - if ((ret = dbp[cdb]->del(dbp[cdb], tid, &dkey, 0)) && ret != DB_NOTFOUND) { + if ((ret = bdb_table[cdb]->del(bdb_table[cdb], tid, &dkey, 0)) && ret != DB_NOTFOUND) { if (ret == DB_LOCK_DEADLOCK) { bdb_txabort(tid); goto retry; @@ -501,7 +502,7 @@ static DBC *bdb_localcursor(int cdb) { DBC *curs; if (TSD->cursors[cdb] == NULL) { - ret = dbp[cdb]->cursor(dbp[cdb], TSD->tid, &curs, 0); + ret = bdb_table[cdb]->cursor(bdb_table[cdb], TSD->tid, &curs, 0); } else { ret = TSD->cursors[cdb]->c_dup(TSD->cursors[cdb], &curs, DB_POSITION); @@ -536,7 +537,7 @@ struct cdbdata *bdb_fetch(int cdb, const void *key, int keylen) { if (TSD->tid != NULL) { memset(&dret, 0, sizeof(DBT)); dret.flags = DB_DBT_MALLOC; - ret = dbp[cdb]->get(dbp[cdb], TSD->tid, &dkey, &dret, 0); + ret = bdb_table[cdb]->get(bdb_table[cdb], TSD->tid, &dkey, &dret, 0); } else { DBC *curs; @@ -608,7 +609,7 @@ void bdb_rewind(int cdb) { } // Now initialize the cursor - ret = dbp[cdb]->cursor(dbp[cdb], TSD->tid, &TSD->cursors[cdb], 0); + ret = bdb_table[cdb]->cursor(bdb_table[cdb], TSD->tid, &TSD->cursors[cdb], 0); if (ret) { syslog(LOG_ERR, "bdb: bdb_rewind: db_cursor: %s", db_strerror(ret)); bdb_abort(); @@ -636,7 +637,7 @@ struct cdbdata *bdb_next_item(int cdb) { bdb_abort(); } bdb_close_cursor(cdb); - return NULL; // presumably, end of file + return NULL; // presumably, we are at the end } cdbret = (struct cdbdata *) malloc(sizeof(struct cdbdata)); @@ -698,10 +699,10 @@ void bdb_trunc(int cdb) { retry: - if ((ret = dbp[cdb]->truncate(dbp[cdb], // db - NULL, // transaction ID - &count, // #rows deleted - 0))) { // flags + if ((ret = bdb_table[cdb]->truncate(bdb_table[cdb], // db + NULL, // transaction ID + &count, // #rows deleted + 0))) { // flags if (ret == DB_LOCK_DEADLOCK) { goto retry; } @@ -725,7 +726,7 @@ void bdb_compact(void) { syslog(LOG_DEBUG, "bdb: bdb_compact() started"); for (i = 0; i < MAXCDB; i++) { syslog(LOG_DEBUG, "bdb: compacting database %d", i); - ret = dbp[i]->compact(dbp[i], NULL, NULL, NULL, NULL, DB_FREE_SPACE, NULL); + ret = bdb_table[i]->compact(bdb_table[i], NULL, NULL, NULL, NULL, DB_FREE_SPACE, NULL); if (ret) { syslog(LOG_ERR, "bdb: compact: %s", db_strerror(ret)); } @@ -736,6 +737,8 @@ void bdb_compact(void) { // Calling this function activates the Berkeley DB back end. void bdb_init_backend(void) { + + // Assign the backend API stubs to the functions in this module. cdb_compact = bdb_compact; cdb_checkpoint = bdb_checkpoint; cdb_rewind = bdb_rewind; @@ -752,7 +755,9 @@ void bdb_init_backend(void) { cdb_check_handles = bdb_check_handles; cdb_trunc = bdb_trunc; - if (pthread_key_create(&bdb_thread_key, NULL) != 0) { // TSD key for this module + // Some functions in this backend need to store some per-thread data. + // We crerate the key here, during module initialization. + if (pthread_key_create(&bdb_thread_key, NULL) != 0) { syslog(LOG_ERR, "pthread_key_create() : %m"); exit(CTDLEXIT_THREAD); } -- 2.39.2