PERFORCE change 125164 for review
Garrett Cooper
gcooper at FreeBSD.org
Tue Aug 14 22:59:35 PDT 2007
http://perforce.freebsd.org/chv.cgi?CH=125164
Change 125164 by gcooper at optimus-revised_pkgtools on 2007/08/15 05:59:15
Filling in more blanks with the BDB DB API.
Affected files ...
.. //depot/projects/soc2007/revised_fbsd_pkgtools/v2/lib/db/api.c#2 edit
.. //depot/projects/soc2007/revised_fbsd_pkgtools/v2/lib/db/api.h#2 edit
Differences ...
==== //depot/projects/soc2007/revised_fbsd_pkgtools/v2/lib/db/api.c#2 (text+ko) ====
@@ -1,42 +1,229 @@
#include "lib/db/api.h"
+/**
+ * @brief: Merge +CONTENTS information or similar output into file database
+ * @return 0 on success
+ * @return 1 on failure
+ */
+int
+build_filedb(DB *database, const char *filedb_filename, const char **file_manifest_filenames)
+{
+
+ int build_db_exit_status;
+
+ /**
+ * Create temporary store for output conversion,
+ * and convert.
+ */
+ char *file_data;
+
+ /**
+ * Convert all entries to strings.
+ */
+ build_db_exit_status = build_db(database, filedb_filename, file_data);
+
+ return build_db_exit_status;
+
+}
+
+/**
+ * @brief Merge INDEX information into pkg database.
+ * @return 0 on success
+ * @return 1 on failure
+ */
int
-build_filedb(DB *database, const char *filedb_filename, const char *file_manifest_filename)
+build_pkgdb(DB *database, const char *pkgdb_filename, const char **index_filenames)
{
+ int build_db_exit_status;
+
+ /**
+ * Create temporary store for output conversion,
+ * and convert.
+ */
+ char *pkg_data;
+ /**
+ * Convert all entries to strings.
+ */
+ build_db_exit_status = build_db(database, pkgdb_filename, pkg_data);
+ return build_db_exit_status;
}
+/**
+ * @brief Create a database by first creating the header, then the
+ * cache, and return the database.
+ * @return 0 on success
+ * @return 1 on failure
+ */
int
-build_pkgdb(DB *database, const char *pkgdb_filename, const char *index_filename)
+build_db(DB *database, const char *db_filename, const char *data)
{
+ int build_db_exit_status;
+
+ build_db_exit_status =
+ dbopen(db_filename, O_CREAT | O_EXLOCK | O_EXCL | O_WRLOCK, 0644, DB_HASH, NULL);
+
+ /**
+ * dbopen was successful.
+ */
+ if(0 == build_db_exit_status) {
+
+ /**
+ * Try initializing the database header.
+ */
+ build_db_exit_status = initialize_database_header(database);
+
+ /**
+ * Database header creation was good. Continue by trying to
+ * create the cache.
+ */
+ if(0 == build_db_exit_status) {
+
+ build_db_exit_status = initialize_entry_cache(database);
+
+ /**
+ * Database is fully built if build_db_exit_status == 0.
+ */
+
+ }
+
+ }
+ /**
+ * Else dbopen was bunk -- return exit status.
+ */
+ return build_db_exit_status;
+
}
+/**
+ * @brief Cache recent entry into file database, substituting the
+ * current entry for the least recently cached one.
+ * @return 0 on success
+ * @return 1 on failure
+ */
int
cache_file_entry(DB *database, const void *entry, const size_t entry_size)
{
+ int cache_exit_status;
+
+
+ remove_lru_cache_entry(database, entry_size);
+ return cache_exit_status;
}
+/**
+ * @brief Cache recent entry into pkg database, substituting
+ * the current entry for the least recently cached one.
+ * @return 0 on success
+ * @return 1 on failure
+ */
int
cache_pkg_entry(DB *database, const void *entry, const size_t entry_size)
{
+ int cache_exit_status;
+
+
+ remove_lru_cache_entry(database, entry_size);
+ return cache_exit_status;
}
+/**
+ * @brief Remove least recently used cache entry to make room
+ * for another entry (the most recently used entry).
+ * @return -1 on no change
+ * @return 0 on success
+ * @return 1 on failure
+ */
int
-flush_changes(DB *database, const void *entry_list, const size_t entry_size)
+remove_lru_cache_entry(DB *database, const size_t entry_size)
+{
+
+ DBT
+ /**
+ * Cache updating related items.
+ *
+ * - cache_retrieved is the initial information.
+ * - cache_displaced is the information which will
+ * be copied and replace the old cache info.
+ */
+ cache_query,
+ cache_retrieved,
+ cache_displaced,
+ /**
+ * Header related items.
+ *
+ * - header_query is the query info for the
+ * header.
+ * - header_retrieved is the returned header from
+ * the database
+ */
+ header_query = { __HEADER_TITLE, sizeof(__HEADER_TITLE) },
+ header_retrieved;
+
+ int db_exit_status = database->get(, 0);
+
+ /**
+ * Ok, there is a cache. Let's proceed.
+ */
+ if(0 == db_exit_status) {
+
+
+
+ }
+
+ return db_exit_status;
+
+}
+
+/**
+ * @brief Setup the database header; this should only be
+ * executed after building the database.
+ * @return 0 on success
+ * @return 1 on failure
+ */
+int
+initialize_database_header(DB *database)
+{
+
+
+
+}
+
+/**
+ * @brief Setup the entry cache; this should only be executed
+ * after building the database.
+ * @return 0 on success
+ * @return 1 on failure
+ */
+int
+initialize_entry_cache(DB *database)
+{
+
+
+
+}
+
+/**
+ * @brief Cache recent entry into pkg database.
+ * @return 0 on success
+ * @return 1 on failure
+ */
+int
+flush_changes(DB *database, const void **entry_list, const size_t entry_size)
{
@@ -44,20 +231,114 @@
}
+/**
+ * @brief Search for file in file database.
+ * @return file_entry if found.
+ * @return NULL if not found.
+ */
void*
file_search(DB *database, const char *key)
{
+ file_entry *entry;
+ db_search(entry, sizeof(file_entry), DB, key);
+ return entry;
}
+/**
+ * @brief Search for package in pkg database.
+ * @return the pkg_entry if found.
+ * @return NULL if not found.
+ */
void*
pkg_search(DB *database, const char *key)
{
+ pkg_entry *entry;
+
+ db_search(entry, sizeof(pkg_entry), DB, key);
+ return entry;
+
+}
+/**
+ * @brief Generic search method through a database, common
+ * to both searching the file and pkg databases.
+ * @return 1 on successful find
+ * @return 0 on successful find.
+ * @return 1 on lack of resources (memset / malloc failed)
+ * or unknown BDB failure (check errno?)
+ *
+ */
+
+/**
+ * Should the entries be globbable/pattern searchable? If
+ * so, then there would be more entires that would need to
+ * be returned than just 1.
+ */
+int
+db_search(void *found_entry, const size_t found_entry_size, DB *database, const char *key)
+{
+
+ DBT search_key = { key, sizeof(key) };
+ DBT db_found_entry = calloc(1, sizeof(DBT));
+
+ int exit_status;
+
+ /**
+ * The search subroutine.
+ */
+ int db_get_exit_status = database->get(database, &search_key, &db_found_entry, 0);
+
+ /**
+ * Success! We found it and everything passed in
+ * creating the DBT objects.
+ */
+ if(0 == db_get_exit_status) {
+
+ assert(found_entry_size == db_found_entry->size);
+
+ exit_status = 1;
+
+ /**
+ * If the malloc was successful, continue on
+ * with the memcpy.
+ */
+ if( NULL != (found_entry = malloc(found_entry_size)) ) {
+
+ /**
+ * If the memset was successful, then the
+ * copy was good and the retrieval was
+ * complete.
+ */
+ if( NULL != (found_entry = memset( found_entry,
+ db_found_entry->data,
+ db_found_entry->size))
+ ) {
+ exit_status = 0;
+ }
+
+ }
+
+ }
+ /**
+ * Else if the key's positive, there was an
+ * unknown BDB error.
+ */
+ else if(0 < db_get_exit_status) {
+ exit_status = 1;
+ }
+ /**
+ * Else, the key wasn't found.
+ */
+ else {
+ exit_status = -1;
+ }
+
+ return exit_status;
}
==== //depot/projects/soc2007/revised_fbsd_pkgtools/v2/lib/db/api.h#2 (text+ko) ====
@@ -7,11 +7,36 @@
#include <fcntl.h>
#include <limits.h>
+#include <string.h>
+
+/**
+ * The database will appear something like this (abstractly):
+ *
+ * __HEADER
+ * __DATA_STORE (keyed)
+ * __ENTRY_CACHE (keyed)
+ *
+ * Where the __HEADER is like the following:
+ *
+ *
+ */
+
+/**
+ *
+ * The title for the cache field.
+ */
+#define __CACHE_FIELD_TITLE "__CACHE"
+
+/**
+ * The title for the header field.
+ */
+#define __HEADER_FIELD_TITLE "__HEADER"
+
typedef struct {
DB bdb_obj;
- int (*build_db) (DB*, const char*, 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*);
@@ -19,19 +44,27 @@
} db;
-/** BUILD DATABASES FROM FILES **/
-int build_filedb(DB*, const char*, const char*);
-int build_pkgdb(DB*, const char*, const char*);
+/** Database building functions **/
+int build_filedb(DB*, const char*, const char**);
+int build_pkgdb(DB*, const char*, const char**);
-/** CACHE ENTRIES **/
+/** Cache maintenance functions **/
int cache_file_entry(DB*, const void* const size_t);
int cache_pkg_entry(DB*, const void*, const size_t);
-/** FLUSH CHANGES **/
+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*);
+
+/** Functions for flushing changes **/
int flush_changes(DB*, const void*, const size_t);
-/** SEARCH **/
+/** Functions for search databases **/
void* file_search(DB*, const char*);
void* pkg_search(DB*, const char*);
+int db_search(void*, const size_t, DB*, const char *);
+
#endif
More information about the p4-projects
mailing list