gstein 01/11/06 17:24:54
Modified: dbm apr_dbm.c apr_dbm_berkeleydb.c apr_dbm_gdbm.c
apr_dbm_sdbm.c
include/private apr_dbm_private.h
Log:
Capture some low-hanging fruit on shifting functionality to the
separate .c files: set_error, close, freedatum, and getusednames.
Set the vtable uesd in the DBM_VTABLE cpp symbol and store it within
the apr_dbm_t type returned by open().
NEEDS_CLEANUP was removed in this pass, as we shifted and them
simplified the #if/#else/#endif branches in freedatum.
set_error and getusednames were shifted from #if sequences into three
new code sections; no changes within the code blocks.
close was a simply copy to the new .c files since they all still use
the APR_DBM_CLOSE cover macro.
Note: no macro expansions were done yet. Keeping the code shift simple
for review purposes.
Revision Changes Path
1.31 +12 -67 apr-util/dbm/apr_dbm.c
Index: apr_dbm.c
===================================================================
RCS file: /home/cvs/apr-util/dbm/apr_dbm.c,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -r1.30 -r1.31
--- apr_dbm.c 2001/11/06 22:23:26 1.30
+++ apr_dbm.c 2001/11/07 01:24:53 1.31
@@ -73,72 +73,30 @@
#define SET_FILE(pdb, f) ((pdb)->file = (f))
+/* ### note: the setting of DBM_VTABLE will go away once we have multiple
+ ### DBMs in here. */
+
#if APU_USE_SDBM
#include "apr_dbm_sdbm.c"
+#define DBM_VTABLE apr_dbm_type_sdbm
#elif APU_USE_GDBM
#include "apr_dbm_gdbm.c"
+#define DBM_VTABLE apr_dbm_type_gdbm
#elif APU_USE_DB
#include "apr_dbm_berkeleydb.c"
+#define DBM_VTABLE apr_dbm_type_db
#else /* Not in the USE_xDBM list above */
#error a DBM implementation was not specified
#endif
-static apr_status_t set_error(apr_dbm_t *dbm, apr_status_t dbm_said)
-{
- apr_status_t rv = APR_SUCCESS;
-
- /* ### ignore whatever the DBM said (dbm_said); ask it explicitly */
-
-#if APU_USE_SDBM
-
- if ((dbm->errcode = dbm_said) == APR_SUCCESS) {
- dbm->errmsg = NULL;
- }
- else {
- dbm->errmsg = "I/O error occurred.";
- rv = APR_EGENERAL; /* ### need something better */
- }
-#elif APU_USE_GDBM
-
- if ((dbm->errcode = gdbm_errno) == GDBM_NO_ERROR) {
- dbm->errmsg = NULL;
- }
- else {
- dbm->errmsg = gdbm_strerror(gdbm_errno);
- rv = APR_EGENERAL; /* ### need something better */
- }
-
- /* captured it. clear it now. */
- gdbm_errno = GDBM_NO_ERROR;
-
-#elif APU_USE_DB
-
- if (dbm_said == APR_SUCCESS) {
- dbm->errcode = 0;
- dbm->errmsg = NULL;
- }
- else {
- /* ### need to fix. dberr was tossed in db2s(). */
- /* ### use db_strerror() */
- dbm->errcode = dbm_said;
- dbm->errmsg = db_strerror( dbm_said - APR_OS_START_USEERR);
- rv = dbm_said;
- }
-#else
-#error set_error has not been coded for this database type
-#endif
-
- return rv;
-}
-
APU_DECLARE(apr_status_t) apr_dbm_open(apr_dbm_t **pdb, const char
*pathname,
apr_int32_t mode, apr_fileperms_t
perm,
apr_pool_t *pool)
@@ -222,6 +180,7 @@
/* we have an open database... return it */
*pdb = apr_pcalloc(pool, sizeof(**pdb));
(*pdb)->pool = pool;
+ (*pdb)->type = &DBM_VTABLE;
SET_FILE(*pdb, file);
/* ### register a cleanup to close the DBM? */
@@ -231,7 +190,7 @@
APU_DECLARE(void) apr_dbm_close(apr_dbm_t *dbm)
{
- APR_DBM_CLOSE(dbm->file);
+ (*dbm->type->close)(dbm);
}
APU_DECLARE(apr_status_t) apr_dbm_fetch(apr_dbm_t *dbm, apr_datum_t key,
@@ -347,11 +306,7 @@
APU_DECLARE(void) apr_dbm_freedatum(apr_dbm_t *dbm, apr_datum_t data)
{
-#ifdef NEEDS_CLEANUP
- (void) apr_pool_cleanup_run(dbm->pool, data.dptr, datum_cleanup);
-#else
- APR_DBM_FREEDPTR(data.dptr);
-#endif
+ (*dbm->type->freedatum)(dbm, data);
}
APU_DECLARE(char *) apr_dbm_geterror(apr_dbm_t *dbm, int *errcode,
@@ -374,20 +329,10 @@
const char **used1,
const char **used2)
{
-#if APU_USE_SDBM
- char *work;
+ /* ### one day, we will pass in a DBM name and need to look it up.
+ ### for now, it is constant. */
- *used1 = apr_pstrcat(p, pathname, APR_SDBM_DIRFEXT, NULL);
- *used2 = work = apr_pstrdup(p, *used1);
-
- /* we know the extension is 4 characters */
- memcpy(&work[strlen(work) - 4], APR_SDBM_PAGFEXT, 4);
-#elif APU_USE_GDBM || APU_USE_DB
- *used1 = apr_pstrdup(p, pathname);
- *used2 = NULL;
-#else
-#error apr_dbm_get_usednames has not been coded for this database type
-#endif
+ return (*DBM_VTABLE.getusednames)(p, pathname, used1, used2);
}
/* Most DBM libraries take a POSIX mode for creating files. Don't trust
1.4 +25 -3 apr-util/dbm/apr_dbm_berkeleydb.c
Index: apr_dbm_berkeleydb.c
===================================================================
RCS file: /home/cvs/apr-util/dbm/apr_dbm_berkeleydb.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- apr_dbm_berkeleydb.c 2001/11/07 00:59:00 1.3
+++ apr_dbm_berkeleydb.c 2001/11/07 01:24:54 1.4
@@ -195,6 +195,27 @@
return db2s(dberr);
}
+static apr_status_t set_error(apr_dbm_t *dbm, apr_status_t dbm_said)
+{
+ apr_status_t rv = APR_SUCCESS;
+
+ /* ### ignore whatever the DBM said (dbm_said); ask it explicitly */
+
+ if (dbm_said == APR_SUCCESS) {
+ dbm->errcode = 0;
+ dbm->errmsg = NULL;
+ }
+ else {
+ /* ### need to fix. dberr was tossed in db2s(). */
+ /* ### use db_strerror() */
+ dbm->errcode = dbm_said;
+ dbm->errmsg = db_strerror( dbm_said - APR_OS_START_USEERR);
+ rv = dbm_said;
+ }
+
+ return rv;
+}
+
/* --------------------------------------------------------------------------
**
** DEFINE THE VTABLE FUNCTIONS FOR BERKELEY DB
@@ -212,7 +233,7 @@
static void vt_db_close(apr_dbm_t *dbm)
{
- abort();
+ APR_DBM_CLOSE(dbm->file);
}
static apr_status_t vt_db_fetch(apr_dbm_t *dbm, apr_datum_t key,
@@ -262,13 +283,14 @@
static void vt_db_freedatum(apr_dbm_t *dbm, apr_datum_t data)
{
- abort();
+ APR_DBM_FREEDPTR(data.dptr);
}
static void vt_db_usednames(apr_pool_t *pool, const char *pathname,
const char **used1, const char **used2)
{
- abort();
+ *used1 = apr_pstrdup(pool, pathname);
+ *used2 = NULL;
}
1.4 +24 -5 apr-util/dbm/apr_dbm_gdbm.c
Index: apr_dbm_gdbm.c
===================================================================
RCS file: /home/cvs/apr-util/dbm/apr_dbm_gdbm.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- apr_dbm_gdbm.c 2001/11/07 00:59:00 1.3
+++ apr_dbm_gdbm.c 2001/11/07 01:24:54 1.4
@@ -73,8 +73,6 @@
#define APR_DBM_NEXTKEY(f, k, nk) ((nk) = gdbm_nextkey(f, *(k)), APR_SUCCESS)
#define APR_DBM_FREEDPTR(dptr) ((dptr) ? free(dptr) : 0)
-/* ### in apr_dbm_freedatum(), run the cleanup */
-#define NEEDS_CLEANUP
#undef REGISTER_CLEANUP
#define REGISTER_CLEANUP(dbm, pdatum) \
@@ -107,6 +105,26 @@
return APR_SUCCESS;
}
+static apr_status_t set_error(apr_dbm_t *dbm, apr_status_t dbm_said)
+{
+ apr_status_t rv = APR_SUCCESS;
+
+ /* ### ignore whatever the DBM said (dbm_said); ask it explicitly */
+
+ if ((dbm->errcode = gdbm_errno) == GDBM_NO_ERROR) {
+ dbm->errmsg = NULL;
+ }
+ else {
+ dbm->errmsg = gdbm_strerror(gdbm_errno);
+ rv = APR_EGENERAL; /* ### need something better */
+ }
+
+ /* captured it. clear it now. */
+ gdbm_errno = GDBM_NO_ERROR;
+
+ return rv;
+}
+
/* --------------------------------------------------------------------------
**
** DEFINE THE VTABLE FUNCTIONS FOR GDBM
@@ -122,7 +140,7 @@
static void vt_gdbm_close(apr_dbm_t *dbm)
{
- abort();
+ APR_DBM_CLOSE(dbm->file);
}
static apr_status_t vt_gdbm_fetch(apr_dbm_t *dbm, apr_datum_t key,
@@ -172,13 +190,14 @@
static void vt_gdbm_freedatum(apr_dbm_t *dbm, apr_datum_t data)
{
- abort();
+ (void) apr_pool_cleanup_run(dbm->pool, data.dptr, datum_cleanup);
}
static void vt_gdbm_usednames(apr_pool_t *pool, const char *pathname,
const char **used1, const char **used2)
{
- abort();
+ *used1 = apr_pstrdup(pool, pathname);
+ *used2 = NULL;
}
1.3 +29 -3 apr-util/dbm/apr_dbm_sdbm.c
Index: apr_dbm_sdbm.c
===================================================================
RCS file: /home/cvs/apr-util/dbm/apr_dbm_sdbm.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- apr_dbm_sdbm.c 2001/11/06 22:23:26 1.2
+++ apr_dbm_sdbm.c 2001/11/07 01:24:54 1.3
@@ -78,6 +78,23 @@
#define APR_DBM_DBMODE_RWCREATE (APR_READ | APR_WRITE | APR_CREATE)
#define APR_DBM_DBMODE_RWTRUNC (APR_READ | APR_WRITE |
APR_CREATE|APR_TRUNCATE)
+static apr_status_t set_error(apr_dbm_t *dbm, apr_status_t dbm_said)
+{
+ apr_status_t rv = APR_SUCCESS;
+
+ /* ### ignore whatever the DBM said (dbm_said); ask it explicitly */
+
+ if ((dbm->errcode = dbm_said) == APR_SUCCESS) {
+ dbm->errmsg = NULL;
+ }
+ else {
+ dbm->errmsg = "I/O error occurred.";
+ rv = APR_EGENERAL; /* ### need something better */
+ }
+
+ return rv;
+}
+
/* --------------------------------------------------------------------------
**
** DEFINE THE VTABLE FUNCTIONS FOR SDBM
@@ -93,7 +110,7 @@
static void vt_sdbm_close(apr_dbm_t *dbm)
{
- abort();
+ APR_DBM_CLOSE(dbm->file);
}
static apr_status_t vt_sdbm_fetch(apr_dbm_t *dbm, apr_datum_t key,
@@ -143,13 +160,22 @@
static void vt_sdbm_freedatum(apr_dbm_t *dbm, apr_datum_t data)
{
- abort();
+ APR_DBM_FREEDPTR(data.dptr);
}
static void vt_sdbm_usednames(apr_pool_t *pool, const char *pathname,
const char **used1, const char **used2)
{
- abort();
+ char *work;
+
+ /* ### this could be optimized by computing strlen() once and using
+ ### memcpy and pmemdup instead. but why bother? */
+
+ *used1 = apr_pstrcat(pool, pathname, APR_SDBM_DIRFEXT, NULL);
+ *used2 = work = apr_pstrdup(pool, *used1);
+
+ /* we know the extension is 4 characters */
+ memcpy(&work[strlen(work) - 4], APR_SDBM_PAGFEXT, 4);
}
1.2 +4 -4 apr-util/include/private/apr_dbm_private.h
Index: apr_dbm_private.h
===================================================================
RCS file: /home/cvs/apr-util/include/private/apr_dbm_private.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- apr_dbm_private.h 2001/11/06 09:36:34 1.1
+++ apr_dbm_private.h 2001/11/07 01:24:54 1.2
@@ -120,10 +120,10 @@
void (*freedatum)(apr_dbm_t *dbm, apr_datum_t data);
/** Get the names that the DBM will use for a given pathname. */
- void (*apr_dbm_get_usednames)(apr_pool_t *pool,
- const char *pathname,
- const char **used1,
- const char **used2);
+ void (*getusednames)(apr_pool_t *pool,
+ const char *pathname,
+ const char **used1,
+ const char **used2);
} apr_dbm_type_t;