Add a db cursor abstraction and interfaces for using it
authorPanu Matilainen <pmatilai@redhat.com>
Wed, 20 Apr 2011 10:52:41 +0000 (13:52 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Wed, 20 Apr 2011 11:04:17 +0000 (14:04 +0300)
- Callers are currently required to pass all sorts of unnecessary
  gunk to the cursor operations just for performance statistics
  etc, and the interface doesn't match the uses inside rpmdb.c
  very well. This adds a more "rpm-style" interface around the
  BDB API.
- Cursor open + close can only fail on invalid arguments (which would
  be programmer errors), and various conditions on replication which
  we dont care about. So we essentially ignore the errors, except
  for cursor open which will return NULL to indicate an error.
- As an intermediate step, these are just wrappers to the older
  dbiFoo interfaces to permit converting things to the new interfaces
  piece by piece.

lib/backend/db3.c
lib/backend/dbi.h

index 0b364bd..41c550d 100644 (file)
@@ -20,6 +20,11 @@ static int _debug = 1;       /* XXX if < 0 debugging, > 0 unusual error returns */
 
 static const char * _errpfx = "rpmdb";
 
+struct dbiCursor_s {
+    dbiIndex dbi;
+    DBC *cursor;
+};
+
 static int dbapi_err(rpmdb rdb, const char * msg, int error, int printit)
 {
     if (printit && error) {
@@ -269,7 +274,6 @@ int dbiCopen(dbiIndex dbi, DBC ** dbcp, unsigned int dbiflags)
     return rc;
 }
 
-
 /* Store (key,data) pair in index database. */
 int dbiPut(dbiIndex dbi, DBC * dbcursor, DBT * key, DBT * data,
                unsigned int flags)
@@ -357,6 +361,49 @@ int dbiCount(dbiIndex dbi, DBC * dbcursor,
     return rc;
 }
 
+dbiCursor dbiCursorInit(dbiIndex dbi, unsigned int flags)
+{
+    dbiCursor dbc = NULL;
+    DBC * cursor = NULL;
+
+    if (dbi && dbiCopen(dbi, &cursor, flags) == 0) {
+       dbc = xcalloc(1, sizeof(*dbc));
+       dbc->cursor = cursor;
+       dbc->dbi = dbi;
+    }
+    return dbc;
+}
+
+dbiCursor dbiCursorFree(dbiCursor dbc)
+{
+    if (dbc) {
+       dbiCclose(dbc->dbi, dbc->cursor, 0);
+    }
+    return NULL;
+}
+
+int dbiCursorPut(dbiCursor dbc, DBT * key, DBT * data, unsigned int flags)
+{
+    return dbiPut(dbc->dbi, dbc->cursor, key, data, flags);
+}
+
+int dbiCursorGet(dbiCursor dbc, DBT * key, DBT * data, unsigned int flags)
+{
+    return dbiGet(dbc->dbi, dbc->cursor, key, data, flags);
+}
+
+int dbiCursorDel(dbiCursor dbc, DBT * key, DBT * data, unsigned int flags)
+{
+    return dbiDel(dbc->dbi, dbc->cursor, key, data, flags);
+}
+
+unsigned int dbiCursorCount(dbiCursor dbc)
+{
+    db_recno_t count = 0;
+    dbiCount(dbc->dbi, dbc->cursor, &count, 0);
+    return count;
+}
+
 int dbiByteSwapped(dbiIndex dbi)
 {
     DB * db = dbi->dbi_db;
index 1e51a3a..249a35f 100644 (file)
@@ -8,6 +8,7 @@ enum rpmdbFlags {
 };
 
 typedef struct dbiIndex_s * dbiIndex;
+typedef struct dbiCursor_s * dbiCursor;
 
 struct dbConfig_s {
     int        db_mmapsize;    /*!< (10Mb) */
@@ -263,6 +264,24 @@ int dbiFlags(dbiIndex dbi);
 RPM_GNUC_INTERNAL
 const char * dbiName(dbiIndex dbi);
 
+RPM_GNUC_INTERNAL
+dbiCursor dbiCursorInit(dbiIndex dbi, unsigned int flags);
+
+RPM_GNUC_INTERNAL
+dbiCursor dbiCursorFree(dbiCursor dbc);
+
+RPM_GNUC_INTERNAL
+int dbiCursorPut(dbiCursor dbc, DBT * key, DBT * data, unsigned int flags);
+
+RPM_GNUC_INTERNAL
+int dbiCursorGet(dbiCursor dbc, DBT * key, DBT * data, unsigned int flags);
+
+RPM_GNUC_INTERNAL
+int dbiCursorDel(dbiCursor dbc, DBT * key, DBT * data, unsigned int flags);
+
+RPM_GNUC_INTERNAL
+unsigned int dbiCursorCount(dbiCursor dbc);
+
 #ifdef __cplusplus
 }
 #endif