Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 15 Aug 2007 06:43:28 GMT
From:      Garrett Cooper <gcooper@FreeBSD.org>
To:        Perforce Change Reviews <perforce@FreeBSD.org>
Subject:   PERFORCE change 125165 for review
Message-ID:  <200708150643.l7F6hS7O072109@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=125165

Change 125165 by gcooper@optimus-revised_pkgtools on 2007/08/15 06:43:07

	- Fill in more DB API blanks, in particular a 'constructor' and 'destructor'.
	- Convert all DB arguments to db arguments. Need to pass in the database object to all called 'methods' (currently it doesn't do much, but just in case..).

Affected files ...

.. //depot/projects/soc2007/revised_fbsd_pkgtools/v2/lib/db/api.c#3 edit
.. //depot/projects/soc2007/revised_fbsd_pkgtools/v2/lib/db/api.h#3 edit

Differences ...

==== //depot/projects/soc2007/revised_fbsd_pkgtools/v2/lib/db/api.c#3 (text+ko) ====

@@ -6,7 +6,7 @@
  * @return 1 on failure
  */
 int
-build_filedb(DB *database, const char *filedb_filename, const char **file_manifest_filenames)
+build_filedb(db *database, const char *filedb_filename, const char **file_manifest_filenames)
 {
 
     int build_db_exit_status;
@@ -32,7 +32,7 @@
  * @return 1 on failure
  */
 int
-build_pkgdb(DB *database, const char *pkgdb_filename, const char **index_filenames)
+build_pkgdb(db *database, const char *pkgdb_filename, const char **index_filenames)
 {
 
     int build_db_exit_status;
@@ -59,18 +59,18 @@
  * @return 1 on failure
  */
 int
-build_db(DB *database, const char *db_filename, const char *data)
+build_db(db *database, const char *db_filename, const char *data)
 {
 
     int build_db_exit_status;
 	
-    build_db_exit_status =
+    database->bdb_obj =
 	dbopen(db_filename, O_CREAT | O_EXLOCK | O_EXCL | O_WRLOCK, 0644, DB_HASH, NULL);
 
     /**
      * dbopen was successful.
      */
-    if(0 == build_db_exit_status) {
+    if(NULL != build_db_exit_status) {
 
 	/**
 	 * Try initializing the database header.
@@ -91,6 +91,8 @@
 
 	}
 
+    } else {
+	build_db_exit_status = 1;
     }
 
     /**
@@ -108,7 +110,7 @@
  * @return 1 on failure
  */
 int
-cache_file_entry(DB *database, const void *entry, const size_t entry_size)
+cache_file_entry(db *database, const void *entry, const size_t entry_size)
 {
 
     int cache_exit_status;
@@ -128,7 +130,7 @@
  * @return 1 on failure
  */
 int
-cache_pkg_entry(DB *database, const void *entry, const size_t entry_size)
+cache_pkg_entry(db *database, const void *entry, const size_t entry_size)
 {
 
     int cache_exit_status;
@@ -149,7 +151,7 @@
  * @return 1 on failure
  */
 int
-remove_lru_cache_entry(DB *database, const size_t entry_size)
+remove_lru_cache_entry(db *database, const size_t entry_size)
 {
 
     DBT
@@ -174,7 +176,7 @@
 	header_query = { __HEADER_TITLE, sizeof(__HEADER_TITLE) },
 	header_retrieved;
 
-    int db_exit_status = database->get(, 0);
+//    int db_exit_status = database->bdb_obj->get(, 0);
 
     /**
      * Ok, there is a cache. Let's proceed.
@@ -196,7 +198,7 @@
  * @return 1 on failure
  */
 int
-initialize_database_header(DB *database)
+initialize_database_header(db *database)
 {
 
     
@@ -210,7 +212,7 @@
  * @return 1 on failure
  */
 int
-initialize_entry_cache(DB *database)
+initialize_entry_cache(db *database)
 {
 
     
@@ -223,7 +225,7 @@
  * @return 1 on failure
  */
 int
-flush_changes(DB *database, const void **entry_list, const size_t entry_size)
+flush_changes(db *database, const void **entry_list, const size_t entry_size)
 {
 
 
@@ -237,12 +239,12 @@
  * @return NULL if not found.
  */
 void*
-file_search(DB *database, const char *key)
+file_search(db *database, const char *key)
 {
 
     file_entry *entry;
 
-    db_search(entry, sizeof(file_entry), DB, key);
+    db_search(entry, sizeof(file_entry), database, key);
 
     return entry;
 
@@ -254,12 +256,12 @@
  * @return NULL if not found.
  */
 void*
-pkg_search(DB *database, const char *key)
+pkg_search(db *database, const char *key)
 {
 
     pkg_entry *entry;
 
-    db_search(entry, sizeof(pkg_entry), DB, key);
+    db_search(entry, sizeof(pkg_entry), db, key);
 
     return entry;
 
@@ -281,7 +283,7 @@
  * be returned than just 1.
  */
 int
-db_search(void *found_entry, const size_t found_entry_size, DB *database, const char *key)
+db_search(void *found_entry, const size_t found_entry_size, db *database, const char *key)
 {
 
     DBT search_key = { key, sizeof(key) };
@@ -292,13 +294,14 @@
     /**
      * The search subroutine.
      */
-    int db_get_exit_status = database->get(database, &search_key, &db_found_entry, 0);
+    exit_status =
+	database->bdb_obj->get(database->bdb_obj, &search_key, &db_found_entry, 0);
 
     /**
      * Success! We found it and everything passed in
      * creating the DBT objects.
      */
-    if(0 == db_get_exit_status) {
+    if(0 == exit_status) {
 
 	assert(found_entry_size == db_found_entry->size);
 
@@ -325,20 +328,50 @@
 	}
 
     }
-    /**
-     * Else if the key's positive, there was an
-     * unknown BDB error.
-     */
-    else if(0 < db_get_exit_status) {
-	exit_status = 1;
+
+    return exit_status;
+
+}
+
+void*
+initialize_db(const char *filename, __DB_TYPE_e db_type) {
+
+    db *database;
+
+    switch(db_type) {
+
+	case FILEDB:
+
+	    database->__build = build_filedb;
+	    database->__cache_entry = cache_file_entry;
+    	    database->__flush = flush_changes;
+    	    database->__free = free_db;
+    	    database->__search = file_search;
+
+	    break;
+
+	case PKGDB:
+
+	    database->__build = build_pkgdb;
+	    database->__cache_entry = cache_pkg_entry; 
+    	    database->__flush = flush_changes;
+    	    database->__free = free_db;
+    	    database->__search = pkg_search;
+
+	    break;
+
+	default:
+	    errx(-1, "Unknown package database type\n");
+
     }
-    /**
-     * Else, the key wasn't found.
-     */
-    else {
-	exit_status = -1;
-    }
+
+    return database;
+
+}
+
+int
+free_db(db* db_obj)
+{
 
-    return exit_status;
 
 }

==== //depot/projects/soc2007/revised_fbsd_pkgtools/v2/lib/db/api.h#3 (text+ko) ====

@@ -1,6 +1,6 @@
-#ifndef __DB_API_H
+#ifndef __db_API_H
 
-#define __DB_API_H
+#define __db_API_H
 
 #include <sys/types.h>
 #include <db.h>
@@ -9,6 +9,10 @@
 
 #include <string.h>
 
+typedef enum {
+    FILEDB, PKGDB
+} __DB_TYPE_e;
+
 /**
  * The database will appear something like this (abstractly):
  *
@@ -32,39 +36,61 @@
  */
 #define __HEADER_FIELD_TITLE	"__HEADER"
 
+/**
+ * The generalized database object. Used for the file and pkg
+ * databases.
+ */
 typedef struct {
 
-    DB		bdb_obj;
+    /** 'OBJECTS' **/
+    /** The base database store object **/
+    DB		*bdb_obj;
+    /** End 'OBJECTS' **/
+
+
+    /** 'METHODS' **/
+    /** Build the database **/
+    int		(*__build)      (db*, const char*, const char**);
+    /** Cache an entry**/
+    int		(*__cache_entry)(db*, const void*, const size_t);
+    /** Flush all changes **/
+    int		(*__flush)      (db*, const void*, const size_t);
+
+    int         (*__free)       (db*);
+    /** Query the database **/
+    void*	(*__search)     (db*, const char*);
 
-    int		(*build_db)	(DB*, const char*, const char**);
-    int		(*cache_entry)	(DB*, const void*, const size_t);
-    int		(*flush_changes)(DB*, const void*, const size_t);
-    int		(*init_db)	(DB*, const char*);
-    void*	(*search)	(DB*, const char*);
+    /** End 'METHODS' **/
 
 } db;
 
 /** Database building functions **/
-int	build_filedb(DB*, const char*, const char**);
-int	build_pkgdb(DB*, const char*, const char**);
+int	build_filedb(db*, const char*, const char**);
+int	build_pkgdb(db*, const char*, const char**);
+
+int     build_db(db*, const char*, const char *);
 
 /** Cache maintenance functions **/
-int	cache_file_entry(DB*, const void* const size_t);
-int	cache_pkg_entry(DB*, const void*, const size_t);
+int	cache_file_entry(db*, const void* const size_t);
+int	cache_pkg_entry(db*, const void*, const size_t);
 
-int     remove_lru_cache_entry(DB*, const size_t);
+int     remove_lru_cache_entry(db*, const size_t);
 
 /** For initializing required fields in the databases **/
-int     initialize_database_header(DB*);
-int     initialize_entry_cache(DB*);
+int     initialize_database_header(db*);
+int     initialize_entry_cache(db*);
 
 /** Functions for flushing changes **/
-int	flush_changes(DB*, const void*, const size_t);
+int	flush_changes(db*, const void*, const size_t);
 
 /** Functions for search databases **/
-void*	file_search(DB*, const char*);
-void*	pkg_search(DB*, const char*);
+void*	file_search(db*, const char*);
+void*	pkg_search(db*, const char*);
+
+int     db_search(void*, const size_t, db*, const char *);
+
+void*   initialize_db(const char*, __DB_TYPE_e);
 
-int     db_search(void*, const size_t, DB*, const char *);
+int     free_db(db*);
 
 #endif



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200708150643.l7F6hS7O072109>