Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

gettingstarted_common.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2004-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  */
00007 
00008 #include "gettingstarted_common.h"
00009 
00010 int get_item_name(DB *, const DBT *, const DBT *, DBT *);
00011 
00012 /*
00013  * Used to extract an inventory item's name from an
00014  * inventory database record. This function is used to create
00015  * keys for secondary database records.
00016  */
00017 int
00018 get_item_name(DB *dbp, const DBT *pkey, const DBT *pdata, DBT *skey)
00019 {
00020     u_int offset;
00021 
00022     dbp = NULL;                         /* Not needed, unused. */
00023     pkey = NULL;
00024 
00025     /*
00026      * First, obtain the buffer location where we placed the
00027      * item's name. In this example, the item's name is located
00028      * in the primary data. It is the first string in the
00029      * buffer after the price (a float) and the quantity (an int).
00030      *
00031      * See load_inventory_database() in example_database_load.c
00032      * for how we packed the inventory information into the
00033      * data DBT.
00034      */
00035     offset = sizeof(float) + sizeof(int);
00036 
00037     /* Check to make sure there's data */
00038     if (pdata->size < offset)
00039         return (-1); /* Returning non-zero means that the
00040                       * secondary record is not created/updated.
00041                       */
00042 
00043     /* Now set the secondary key's data to be the item name */
00044     memset(skey, 0, sizeof(DBT));
00045     skey->data = (u_int8_t *)pdata->data + offset;
00046     skey->size = (u_int32_t)strlen(skey->data) + 1;
00047 
00048     return (0);
00049 }
00050 
00051 /* Opens a database */
00052 int
00053 open_database(DB **dbpp, const char *file_name,
00054   const char *program_name, FILE *error_file_pointer,
00055   int is_secondary)
00056 {
00057     DB *dbp;    /* For convenience */
00058     u_int32_t open_flags;
00059     int ret;
00060 
00061     /* Initialize the DB handle */
00062     ret = db_create(&dbp, NULL, 0);
00063     if (ret != 0) {
00064         fprintf(error_file_pointer, "%s: %s\n", program_name,
00065                 db_strerror(ret));
00066         return (ret);
00067     }
00068     /* Point to the memory malloc'd by db_create() */
00069     *dbpp = dbp;
00070 
00071     /* Set up error handling for this database */
00072     dbp->set_errfile(dbp, error_file_pointer);
00073     dbp->set_errpfx(dbp, program_name);
00074 
00075     /*
00076      * If this is a secondary database, then we want to allow
00077      * sorted duplicates.
00078      */
00079     if (is_secondary) {
00080         ret = dbp->set_flags(dbp, DB_DUPSORT);
00081         if (ret != 0) {
00082             dbp->err(dbp, ret, "Attempt to set DUPSORT flags failed.",
00083               file_name);
00084             return (ret);
00085         }
00086     }
00087 
00088     /* Set the open flags */
00089     open_flags = DB_CREATE;    /* Allow database creation */
00090 
00091     /* Now open the database */
00092     ret = dbp->open(dbp,        /* Pointer to the database */
00093                     NULL,       /* Txn pointer */
00094                     file_name,  /* File name */
00095                     NULL,       /* Logical db name */
00096                     DB_BTREE,   /* Database type (using btree) */
00097                     open_flags, /* Open flags */
00098                     0);         /* File mode. Using defaults */
00099     if (ret != 0) {
00100         dbp->err(dbp, ret, "Database '%s' open failed.", file_name);
00101         return (ret);
00102     }
00103 
00104     return (0);
00105 }
00106 
00107 /* opens all databases */
00108 int
00109 databases_setup(STOCK_DBS *my_stock, const char *program_name,
00110   FILE *error_file_pointer)
00111 {
00112     int ret;
00113 
00114     /* Open the vendor database */
00115     ret = open_database(&(my_stock->vendor_dbp),
00116       my_stock->vendor_db_name,
00117       program_name, error_file_pointer,
00118       PRIMARY_DB);
00119     if (ret != 0)
00120         /*
00121          * Error reporting is handled in open_database() so just return
00122          * the return code.
00123          */
00124         return (ret);
00125 
00126     /* Open the inventory database */
00127     ret = open_database(&(my_stock->inventory_dbp),
00128       my_stock->inventory_db_name,
00129       program_name, error_file_pointer,
00130       PRIMARY_DB);
00131     if (ret != 0)
00132         /*
00133          * Error reporting is handled in open_database() so just return
00134          * the return code.
00135          */
00136         return (ret);
00137 
00138     /*
00139      * Open the itemname secondary database. This is used to
00140      * index the product names found in the inventory
00141      * database.
00142      */
00143     ret = open_database(&(my_stock->itemname_sdbp),
00144       my_stock->itemname_db_name,
00145       program_name, error_file_pointer,
00146       SECONDARY_DB);
00147     if (ret != 0)
00148         /*
00149          * Error reporting is handled in open_database() so just return
00150          * the return code.
00151          */
00152         return (0);
00153 
00154     /*
00155      * Associate the itemname db with its primary db
00156      * (inventory db).
00157      */
00158      my_stock->inventory_dbp->associate(
00159        my_stock->inventory_dbp,    /* Primary db */
00160        NULL,                       /* txn id */
00161        my_stock->itemname_sdbp,    /* Secondary db */
00162        get_item_name,              /* Secondary key creator */
00163        0);                         /* Flags */
00164 
00165     printf("databases opened successfully\n");
00166     return (0);
00167 }
00168 
00169 /* Initializes the STOCK_DBS struct.*/
00170 void
00171 initialize_stockdbs(STOCK_DBS *my_stock)
00172 {
00173     my_stock->db_home_dir = DEFAULT_HOMEDIR;
00174     my_stock->inventory_dbp = NULL;
00175     my_stock->vendor_dbp = NULL;
00176     my_stock->itemname_sdbp = NULL;
00177     my_stock->vendor_db_name = NULL;
00178     my_stock->inventory_db_name = NULL;
00179     my_stock->itemname_db_name = NULL;
00180 }
00181 
00182 /* Identify all the files that will hold our databases. */
00183 void
00184 set_db_filenames(STOCK_DBS *my_stock)
00185 {
00186     size_t size;
00187 
00188     /* Create the Inventory DB file name */
00189     size = strlen(my_stock->db_home_dir) + strlen(INVENTORYDB) + 1;
00190     my_stock->inventory_db_name = malloc(size);
00191     snprintf(my_stock->inventory_db_name, size, "%s%s",
00192       my_stock->db_home_dir, INVENTORYDB);
00193 
00194     /* Create the Vendor DB file name */
00195     size = strlen(my_stock->db_home_dir) + strlen(VENDORDB) + 1;
00196     my_stock->vendor_db_name = malloc(size);
00197     snprintf(my_stock->vendor_db_name, size, "%s%s",
00198       my_stock->db_home_dir, VENDORDB);
00199 
00200     /* Create the itemname DB file name */
00201     size = strlen(my_stock->db_home_dir) + strlen(ITEMNAMEDB) + 1;
00202     my_stock->itemname_db_name = malloc(size);
00203     snprintf(my_stock->itemname_db_name, size, "%s%s",
00204       my_stock->db_home_dir, ITEMNAMEDB);
00205 
00206 }
00207 
00208 /* Closes all the databases and secondary databases. */
00209 int
00210 databases_close(STOCK_DBS *my_stock)
00211 {
00212     int ret;
00213     /*
00214      * Note that closing a database automatically flushes its cached data
00215      * to disk, so no sync is required here.
00216      */
00217     if (my_stock->itemname_sdbp != NULL) {
00218         ret = my_stock->itemname_sdbp->close(my_stock->itemname_sdbp, 0);
00219         if (ret != 0)
00220             fprintf(stderr, "Itemname database close failed: %s\n",
00221               db_strerror(ret));
00222     }
00223 
00224     if (my_stock->inventory_dbp != NULL) {
00225         ret = my_stock->inventory_dbp->close(my_stock->inventory_dbp, 0);
00226         if (ret != 0)
00227             fprintf(stderr, "Inventory database close failed: %s\n",
00228               db_strerror(ret));
00229     }
00230 
00231     if (my_stock->vendor_dbp != NULL) {
00232         ret = my_stock->vendor_dbp->close(my_stock->vendor_dbp, 0);
00233         if (ret != 0)
00234             fprintf(stderr, "Vendor database close failed: %s\n",
00235               db_strerror(ret));
00236     }
00237 
00238     printf("databases closed.\n");
00239     return (0);
00240 }

Generated on Sun Dec 25 12:14:25 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2