summaryrefslogtreecommitdiff
path: root/lib/db2.c
diff options
context:
space:
mode:
authorjbj <devnull@localhost>2000-03-27 11:13:25 +0000
committerjbj <devnull@localhost>2000-03-27 11:13:25 +0000
commit62381feb62bc1980a24069386060bad917baf901 (patch)
tree938f05861f27dcc4f255e27c5fa9ca1385b3192d /lib/db2.c
parent22a911a3b36c9cde5159e0343632246ba9fa007c (diff)
downloadrpm-62381feb62bc1980a24069386060bad917baf901.tar.gz
rpm-62381feb62bc1980a24069386060bad917baf901.tar.bz2
rpm-62381feb62bc1980a24069386060bad917baf901.zip
Compilation with db3.
CVS patchset: 3642 CVS date: 2000/03/27 11:13:25
Diffstat (limited to 'lib/db2.c')
-rw-r--r--lib/db2.c407
1 files changed, 202 insertions, 205 deletions
diff --git a/lib/db2.c b/lib/db2.c
index 037133a2b..f64fb1114 100644
--- a/lib/db2.c
+++ b/lib/db2.c
@@ -1,6 +1,6 @@
#include "system.h"
-static int _debug = 0;
+static int _debug = 1;
#include <db.h>
@@ -30,71 +30,200 @@ static inline /*@observer@*/ /*@null@*/ DB * GetDB(dbiIndex dbi) {
return ((DB *)dbi->dbi_db);
}
+#if defined(__USE_DB2) || defined(__USE_DB3)
#if defined(__USE_DB2)
+static /*@observer@*/ const char * db_strerror(int error)
+{
+ if (error == 0)
+ return ("Successful return: 0");
+ if (error > 0)
+ return (strerror(error));
+
+ switch (error) {
+ case DB_INCOMPLETE:
+ return ("DB_INCOMPLETE: Cache flush was unable to complete");
+ case DB_KEYEMPTY:
+ return ("DB_KEYEMPTY: Non-existent key/data pair");
+ case DB_KEYEXIST:
+ return ("DB_KEYEXIST: Key/data pair already exists");
+ case DB_LOCK_DEADLOCK:
+ return ("DB_LOCK_DEADLOCK: Locker killed to resolve a deadlock");
+ case DB_LOCK_NOTGRANTED:
+ return ("DB_LOCK_NOTGRANTED: Lock not granted");
+ case DB_NOTFOUND:
+ return ("DB_NOTFOUND: No matching key/data pair found");
+#if defined(__USE_DB3)
+ case DB_OLD_VERSION:
+ return ("DB_OLDVERSION: Database requires a version upgrade");
+ case DB_RUNRECOVERY:
+ return ("DB_RUNRECOVERY: Fatal error, run database recovery");
+#else
+ case DB_LOCK_NOTHELD:
+ return ("DB_LOCK_NOTHELD:");
+ case DB_REGISTERED:
+ return ("DB_REGISTERED:");
+#endif
+ default:
+ {
+ /*
+ * !!!
+ * Room for a 64-bit number + slop. This buffer is only used
+ * if we're given an unknown error, which should never happen.
+ * Note, however, we're no longer thread-safe if it does.
+ */
+ static char ebuf[40];
+
+ (void)snprintf(ebuf, sizeof(ebuf), "Unknown error: %d", error);
+ return(ebuf);
+ }
+ }
+ /*@notreached@*/
+}
+
+static int db_env_create(DB_ENV **dbenvp, int foo)
+{
+ DB_ENV *dbenv;
+
+ if (dbenvp == NULL)
+ return 1;
+ dbenv = xcalloc(1, sizeof(*dbenv));
+
+ *dbenvp = dbenv;
+ return 0;
+}
+#endif /* __USE_DB2 */
+
+static int cvtdberr(dbiIndex dbi, const char * msg, int error, int printit) {
+ int rc = 0;
+
+ if (error == 0)
+ rc = 0;
+ else if (error < 0)
+ rc = -1;
+ else if (error > 0)
+ rc = 1;
+
+ if (printit && rc) {
+ fprintf(stderr, "*** db%d %s rc %d %s\n", dbi->dbi_major, msg,
+ rc, db_strerror(error));
+ }
+
+ return rc;
+}
+
static int db_init(dbiIndex dbi, const char *home, int dbflags,
- DB_ENV **dbenvp, DB_INFO **dbinfop)
+ DB_ENV **dbenvp, void **dbinfop)
{
- DB_ENV *dbenv = xcalloc(1, sizeof(*dbenv));
- DB_INFO *dbinfo = xcalloc(1, sizeof(*dbinfo));
+ DB_ENV *dbenv = NULL;
+ FILE * dberrfile = stderr;
+ const char * dberrpfx = "rpmdb";
+ int dbcachesize = 1024 * 1024;
+ int dbpagesize = 32 * 1024; /* 0 - 64K */
int rc;
- if (dbenvp) *dbenvp = NULL;
- if (dbinfop) *dbinfop = NULL;
-
- dbenv->db_errfile = stderr;
- dbenv->db_errpfx = "rpmdb";
- dbenv->mp_size = 1024 * 1024;
+ if (dbenvp == NULL || dbinfop == NULL)
+ return 1;
-#define _DBFMASK (DB_CREATE|DB_NOMMAP|DB_THREAD)
- rc = db_appinit(home, NULL, dbenv, (dbflags & _DBFMASK));
+ rc = db_env_create(&dbenv, 0);
+ rc = cvtdberr(dbi, "db_env_create", rc, _debug);
if (rc)
goto errxit;
- /* XXX W2DO? */
- dbinfo->db_pagesize = 32 * 1024;
+#if defined(__USE_DB3)
+ { int xx;
+ /* dbenv->set_errcall(???) */
+ xx = dbenv->set_errfile(dberrfile);
+ xx = cvtdberr(dbi, "dbenv->set_errfile", xx, _debug);
+ xx = dbenv->set_errpfx(dberrpfx);
+ xx = cvtdberr(dbi, "dbenv->set_errpfx", xx, _debug);
+ /* dbenv->set_paniccall(???) */
+ /* dbenv->set_verbose(???) */
+ /* dbenv->set_lg_max(???) */
+ /* dbenv->set_lk_conflicts(???) */
+ /* dbenv->set_lk_detect(???) */
+ /* dbenv->set_lk_max(???) */
+ /* dbenv->set_mp_mmapsize(???) */
+ xx = dbenv->set_cachesize(dbcachesize);
+ xx = cvtdberr(dbi, "dbenv->set_cachesize", xx, _debug);
+ /* dbenv->set_tx_max(???) */
+ /* dbenv->set_tx_recover(???) */
+ }
+#else
+ dbenv->db_errfile = dberrfile;
+ dbenv->db_errpfx = dberrpfx;
+ dbenv->mp_size = dbcachesize;
+#endif
- if (dbenvp)
- *dbenvp = dbenv;
- else
- free(dbenv);
+#define _DBFMASK (DB_CREATE|DB_NOMMAP|DB_THREAD)
- if (dbinfop)
+#if defined(__USE_DB3)
+ rc = dbenv->open(dbenv, home, NULL, (dbflags & _DBFMASK), 0);
+ rc = cvtdberr(dbi, "dbenv->open", rc, _debug);
+ if (rc)
+ goto errxit;
+ xx = db->set_pagesize(db, dbpagesize)
+ xx = cvtdberr(dbi, "db->set_pagesize", xx, _debug);
+ *dbinfop = NULL;
+#else
+ rc = db_appinit(home, NULL, dbenv, (dbflags & _DBFMASK));
+ rc = cvtdberr(dbi, "db_appinit", rc, _debug);
+ if (rc)
+ goto errxit;
+ { DB_INFO * dbinfo = xcalloc(1, sizeof(*dbinfo));
+ /* XXX W2DO? */
+ dbinfo->db_pagesize = dbpagesize;
*dbinfop = dbinfo;
- else
- free(dbinfo);
+ }
+#endif
+
+ *dbenvp = dbenv;
return 0;
errxit:
+
+#if defined(__USE_DB3)
+ if (dbenv) {
+ xx = dbenv->close(dbenv);
+ xx = cvtdberr(dbi, "dbenv->close", xx, _debug);
+ }
+#else
if (dbenv) free(dbenv);
- if (dbinfo) free(dbinfo);
+#endif
return rc;
}
-#endif
+#endif /* __USE_DB2 || __USE_DB3 */
int db2open(dbiIndex dbi)
{
int rc = 0;
-#if defined(__USE_DB2)
+#if defined(__USE_DB2) || defined(__USE_DB3)
char * dbhome = NULL;
DB * db = NULL;
DB_ENV * dbenv = NULL;
- DB_INFO * dbinfo = NULL;
+ void * dbinfo = NULL;
u_int32_t dbflags;
dbflags = ( !(dbi->dbi_flags & O_RDWR) ? DB_RDONLY :
((dbi->dbi_flags & O_CREAT) ? DB_CREATE : 0));
rc = db_init(dbi, dbhome, dbflags, &dbenv, &dbinfo);
-if (_debug)
-fprintf(stderr, "*** db%d db_init rc %d errno %d\n", dbi->dbi_major, rc, errno);
if (rc == 0) {
+#if defined(__USE_DB3)
+ rc = db_create(&db, dbenv, 0);
+ rc = cvtdberr(dbi, "db_create", rc, _debug);
+ if (rc == 0) {
+ rc = db->open(db, dbi->dbi_file, NULL, dbi_to_dbtype(dbi->dbi_type),
+ dbflags, dbi->dbi_perms);
+ rc = cvtdberr(dbi, "db->open", rc, _debug);
+ }
+#else
rc = db_open(dbi->dbi_file, dbi_to_dbtype(dbi->dbi_type), dbflags,
dbi->dbi_perms, dbenv, dbinfo, &db);
-if (_debug)
-fprintf(stderr, "*** db%d db_open rc %d errno %d\n", dbi->dbi_major, rc, errno);
+ rc = cvtdberr(dbi, "db_open", rc, _debug);
+#endif /* __USE_DB3 */
}
dbi->dbi_db = db;
@@ -104,7 +233,7 @@ fprintf(stderr, "*** db%d db_open rc %d errno %d\n", dbi->dbi_major, rc, errno);
#else
dbi->dbi_db = dbopen(dbi->dbi_file, dbi->dbi_flags, dbi->dbi_perms,
dbi_to_dbtype(dbi->dbi_type), dbi->dbi_openinfo);
-#endif
+#endif /* __USE_DB2 || __USE_DB3 */
if (rc == 0 && dbi->dbi_db != NULL) {
rc = 0;
@@ -119,63 +248,43 @@ fprintf(stderr, "*** db%d db_open rc %d errno %d\n", dbi->dbi_major, rc, errno);
int db2close(dbiIndex dbi, unsigned int flags) {
DB * db = GetDB(dbi);
- int rc, xx;
+ int rc = 0, xx;
-#if defined(__USE_DB2)
- DB_ENV * dbenv = (DB_ENV *)dbi->dbi_dbenv;
- DB_INFO * dbinfo = (DB_INFO *)dbi->dbi_dbinfo;
- DBC * dbcursor = (DBC *)dbi->dbi_dbcursor;
+#if defined(__USE_DB2) || defined(__USE_DB3)
- if (dbcursor) {
+ if (dbi->dbi_dbcursor) {
+ DBC * dbcursor = (DBC *)dbi->dbi_dbcursor;
xx = dbcursor->c_close(dbcursor);
-if (_debug)
-fprintf(stderr, "*** db%d db->c_close rc %d errno %d\n", dbi->dbi_major, xx, errno);
+ xx = cvtdberr(dbi, "dbcursor->c_close", xx, _debug);
dbi->dbi_dbcursor = NULL;
}
- rc = db->close(db, 0);
-if (_debug)
-fprintf(stderr, "*** db%d db->close rc %d errno %d\n", dbi->dbi_major, rc, errno);
- dbi->dbi_db = NULL;
+ if (db) {
+ rc = db->close(db, 0);
+ rc = cvtdberr(dbi, "db->close", rc, _debug);
+ db = dbi->dbi_db = NULL;
+ }
- if (dbinfo) {
- free(dbinfo);
+ if (dbi->dbi_dbinfo) {
+ free(dbi->dbi_dbinfo);
dbi->dbi_dbinfo = NULL;
}
- if (dbenv) {
+ if (dbi->dbi_dbenv) {
+ DB_ENV * dbenv = (DB_ENV *)dbi->dbi_dbenv;
+#if defined(__USE_DB3)
+ xx = dbenv->close(dbenv);
+ xx = cvtdberr(dbi, "dbenv->close", xx, _debug);
+#else
xx = db_appexit(dbenv);
-if (_debug)
-fprintf(stderr, "*** db%d db_appexit rc %d errno %d\n", dbi->dbi_major, xx, errno);
- free(dbenv);
+ xx = cvtdberr(dbi, "db_appexit", xx, _debug);
+#endif
+ free(dbi->dbi_dbenv);
dbi->dbi_dbenv = NULL;
}
#else
rc = db->close(db);
#endif
- switch (rc) {
- default:
- rc = -1;
- break;
- case DB_INCOMPLETE:
- case DB_KEYEMPTY:
- case DB_KEYEXIST:
- case DB_LOCK_DEADLOCK:
- case DB_LOCK_NOTGRANTED:
- case DB_LOCK_NOTHELD:
- case DB_NOTFOUND:
- case DB_DELETED:
- case DB_NEEDSPLIT:
- case DB_REGISTERED:
- case DB_SWAPBYTES:
- case DB_TXN_CKP:
- rc = 1;
- break;
- case 0:
- rc = 0;
- break;
- }
-
return rc;
}
@@ -183,37 +292,13 @@ int db2sync(dbiIndex dbi, unsigned int flags) {
DB * db = GetDB(dbi);
int rc;
-#if defined(__USE_DB2)
+#if defined(__USE_DB2) || defined(__USE_DB3)
rc = db->sync(db, flags);
-if (_debug)
-fprintf(stderr, "*** db%d db->sync rc %d errno %d\n", dbi->dbi_major, rc, errno);
+ rc = cvtdberr(dbi, "db->sync", rc, _debug);
#else
rc = db->sync(db, flags);
#endif
- switch (rc) {
- default:
- rc = -1;
- break;
- case DB_INCOMPLETE:
- case DB_KEYEMPTY:
- case DB_KEYEXIST:
- case DB_LOCK_DEADLOCK:
- case DB_LOCK_NOTGRANTED:
- case DB_LOCK_NOTHELD:
- case DB_NOTFOUND:
- case DB_DELETED:
- case DB_NEEDSPLIT:
- case DB_REGISTERED:
- case DB_SWAPBYTES:
- case DB_TXN_CKP:
- rc = 1;
- break;
- case 0:
- rc = 0;
- break;
- }
-
return rc;
}
@@ -232,51 +317,28 @@ int db2GetFirstKey(dbiIndex dbi, const char ** keyp) {
key.data = NULL;
key.size = 0;
-#if defined(__USE_DB2)
+#if defined(__USE_DB2) || defined(__USE_DB3)
{ DBC * dbcursor = NULL;
rc = db->cursor(db, NULL, &dbcursor);
-if (_debug)
-fprintf(stderr, "*** db%d db->cursor rc %d errno %d\n", dbi->dbi_major, rc, errno);
+ rc = cvtdberr(dbi, "db->cursor", rc, _debug);
if (rc == 0) {
rc = dbcursor->c_get(dbcursor, &key, &data, DB_FIRST);
-if (_debug)
-fprintf(stderr, "*** db%d dbcursor->c_get rc %d errno %d\n", dbi->dbi_major, rc, errno);
+ rc = cvtdberr(dbi, "dbcursor->c_get", rc, _debug);
}
if (dbcursor) {
xx = dbcursor->c_close(dbcursor);
-if (_debug)
-fprintf(stderr, "*** db%d dbcursor->c_close rc %d errno %d\n", dbi->dbi_major, xx, errno);
+ xx = cvtdberr(dbi, "dbcursor->c_close", xx, _debug);
}
}
#else
rc = db->seq(db, &key, &data, R_FIRST);
#endif
- switch (rc) {
- default:
- case DB_INCOMPLETE:
- case DB_KEYEMPTY:
- case DB_KEYEXIST:
- case DB_LOCK_DEADLOCK:
- case DB_LOCK_NOTGRANTED:
- case DB_LOCK_NOTHELD:
- case DB_NOTFOUND:
- case DB_DELETED:
- case DB_NEEDSPLIT:
- case DB_REGISTERED:
- case DB_SWAPBYTES:
- case DB_TXN_CKP:
- rc = 1;
- break;
- case 0:
- rc = 0;
- if (keyp) {
- char *k = xmalloc(key.size + 1);
- memcpy(k, key.data, key.size);
- k[key.size] = '\0';
- *keyp = k;
- }
- break;
+ if (rc == 0 && keyp) {
+ char *k = xmalloc(key.size + 1);
+ memcpy(k, key.data, key.size);
+ k[key.size] = '\0';
+ *keyp = k;
}
return rc;
@@ -296,41 +358,18 @@ int db2SearchIndex(dbiIndex dbi, const char * str, dbiIndexSet * set) {
data.data = NULL;
data.size = 0;
-#if defined(__USE_DB2)
+#if defined(__USE_DB2) || defined(__USE_DB3)
rc = db->get(db, NULL, &key, &data, 0);
-if (_debug)
-fprintf(stderr, "*** db%d db->get rc %d errno %d\n", dbi->dbi_major, rc, errno);
+ rc = cvtdberr(dbi, "db->get", rc, _debug);
#else
rc = db->get(db, &key, &data, 0);
#endif
- switch (rc) {
- default:
- rc = -1;
- break;
- case DB_INCOMPLETE:
- case DB_KEYEMPTY:
- case DB_KEYEXIST:
- case DB_LOCK_DEADLOCK:
- case DB_LOCK_NOTGRANTED:
- case DB_LOCK_NOTHELD:
- case DB_NOTFOUND:
- case DB_DELETED:
- case DB_NEEDSPLIT:
- case DB_REGISTERED:
- case DB_SWAPBYTES:
- case DB_TXN_CKP:
- rc = 1;
- break;
- case 0:
- rc = 0;
- if (set) {
- *set = dbiCreateIndexSet();
- (*set)->recs = xmalloc(data.size);
- memcpy((*set)->recs, data.data, data.size);
- (*set)->count = data.size / sizeof(*(*set)->recs);
- }
- break;
+ if (rc == 0 && set) {
+ *set = dbiCreateIndexSet();
+ (*set)->recs = xmalloc(data.size);
+ memcpy((*set)->recs, data.data, data.size);
+ (*set)->count = data.size / sizeof(*(*set)->recs);
}
return rc;
}
@@ -352,64 +391,22 @@ int db2UpdateIndex(dbiIndex dbi, const char * str, dbiIndexSet set) {
data.data = set->recs;
data.size = set->count * sizeof(*(set->recs));
-#if defined(__USE_DB2)
+#if defined(__USE_DB2) || defined(__USE_DB3)
rc = db->put(db, NULL, &key, &data, 0);
-if (_debug)
-fprintf(stderr, "*** db%d db->put rc %d errno %d\n", dbi->dbi_major, rc, errno);
+ rc = cvtdberr(dbi, "db->put", rc, _debug);
#else
rc = db->put(db, &key, &data, 0);
#endif
- switch (rc) {
- default:
- case DB_INCOMPLETE:
- case DB_KEYEMPTY:
- case DB_KEYEXIST:
- case DB_LOCK_DEADLOCK:
- case DB_LOCK_NOTGRANTED:
- case DB_LOCK_NOTHELD:
- case DB_NOTFOUND:
- case DB_DELETED:
- case DB_NEEDSPLIT:
- case DB_REGISTERED:
- case DB_SWAPBYTES:
- case DB_TXN_CKP:
- rc = 1;
- break;
- case 0: /* 0 */
- rc = 0;
- break;
- }
} else {
-#if defined(__USE_DB2)
+#if defined(__USE_DB2) || defined(__USE_DB3)
rc = db->del(db, NULL, &key, 0);
-if (_debug)
-fprintf(stderr, "*** db%d db->del rc %d errno %d\n", dbi->dbi_major, rc, errno);
+ rc = cvtdberr(dbi, "db->del", rc, _debug);
#else
rc = db->del(db, &key, 0);
#endif
- switch (rc) {
- default:
- case DB_INCOMPLETE:
- case DB_KEYEMPTY:
- case DB_KEYEXIST:
- case DB_LOCK_DEADLOCK:
- case DB_LOCK_NOTGRANTED:
- case DB_LOCK_NOTHELD:
- case DB_NOTFOUND:
- case DB_DELETED:
- case DB_NEEDSPLIT:
- case DB_REGISTERED:
- case DB_SWAPBYTES:
- case DB_TXN_CKP:
- rc = 1;
- break;
- case 0:
- rc = 0;
- break;
- }
}
return rc;