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

ex_dbclient.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1996-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: ex_dbclient.c,v 12.1 2005/06/16 20:22:01 bostic Exp $
00008  */
00009 
00010 #include <sys/types.h>
00011 
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <string.h>
00015 #include <unistd.h>
00016 
00017 #include <db.h>
00018 
00019 #define DATABASE_HOME   "database"
00020 
00021 #define DATABASE        "access.db"
00022 
00023 int     db_clientrun __P((DB_ENV *, const char *));
00024 int     ex_dbclient __P((const char *));
00025 int     ex_dbclient_run __P((const char *, FILE *, const char *, const char *));
00026 int     main __P((int, char *[]));
00027 
00028 /*
00029  * An example of a program creating/configuring a Berkeley DB environment.
00030  */
00031 int
00032 main(argc, argv)
00033         int argc;
00034         char *argv[];
00035 {
00036         const char *home;
00037 
00038         if (argc != 2) {
00039                 fprintf(stderr, "Usage: %s hostname\n", argv[0]);
00040                 return (EXIT_FAILURE);
00041         }
00042 
00043         /*
00044          * All of the shared database files live in DATABASE_HOME, but
00045          * data files will live in CONFIG_DATA_DIR.
00046          */
00047         home = DATABASE_HOME;
00048         return (ex_dbclient_run(home,
00049             stderr, argv[1], argv[0]) == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
00050 }
00051 
00052 int
00053 ex_dbclient(host)
00054         const char *host;
00055 {
00056         const char *home;
00057         const char *progname = "ex_dbclient";           /* Program name. */
00058         int ret;
00059 
00060         /*
00061          * All of the shared database files live in DATABASE_HOME, but
00062          * data files will live in CONFIG_DATA_DIR.
00063          */
00064         home = DATABASE_HOME;
00065 
00066         if ((ret = ex_dbclient_run(home, stderr, host, progname)) != 0)
00067                 return (ret);
00068 
00069         return (0);
00070 }
00071 
00072 int
00073 ex_dbclient_run(home, errfp, host, progname)
00074         const char *home, *host, *progname;
00075         FILE *errfp;
00076 {
00077         DB_ENV *dbenv;
00078         int ret, retry;
00079 
00080         /*
00081          * Create an environment object and initialize it for error
00082          * reporting.
00083          */
00084         if ((ret = db_env_create(&dbenv, DB_RPCCLIENT)) != 0) {
00085                 fprintf(errfp, "%s: %s\n", progname, db_strerror(ret));
00086                 return (1);
00087         }
00088         retry = 0;
00089 retry:
00090         while (retry < 5) {
00091                 /*
00092                  * Set the server host we are talking to.
00093                  */
00094                 if ((ret = dbenv->set_rpc_server(dbenv, NULL, host, 10000,
00095                     10000, 0)) != 0) {
00096                         fprintf(stderr, "Try %d: DB_ENV->set_rpc_server: %s\n",
00097                             retry, db_strerror(ret));
00098                         retry++;
00099                         sleep(15);
00100                 } else
00101                         break;
00102         }
00103 
00104         if (retry >= 5) {
00105                 fprintf(stderr,
00106                     "DB_ENV->set_rpc_server: %s\n", db_strerror(ret));
00107                 dbenv->close(dbenv, 0);
00108                 return (1);
00109         }
00110         /*
00111          * We want to specify the shared memory buffer pool cachesize,
00112          * but everything else is the default.
00113          */
00114         if ((ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0)) != 0) {
00115                 dbenv->err(dbenv, ret, "set_cachesize");
00116                 dbenv->close(dbenv, 0);
00117                 return (1);
00118         }
00119         /*
00120          * We have multiple processes reading/writing these files, so
00121          * we need concurrency control and a shared buffer pool, but
00122          * not logging or transactions.
00123          */
00124         if ((ret = dbenv->open(dbenv, home,
00125             DB_CREATE | DB_INIT_LOCK | DB_INIT_MPOOL, 0)) != 0) {
00126                 dbenv->err(dbenv, ret, "environment open: %s", home);
00127                 dbenv->close(dbenv, 0);
00128                 if (ret == DB_NOSERVER)
00129                         goto retry;
00130                 return (1);
00131         }
00132 
00133         ret = db_clientrun(dbenv, progname);
00134         printf("db_clientrun returned %d\n", ret);
00135         if (ret == DB_NOSERVER)
00136                 goto retry;
00137 
00138         /* Close the handle. */
00139         if ((ret = dbenv->close(dbenv, 0)) != 0) {
00140                 fprintf(stderr, "DB_ENV->close: %s\n", db_strerror(ret));
00141                 return (1);
00142         }
00143         return (0);
00144 }
00145 
00146 int
00147 db_clientrun(dbenv, progname)
00148         DB_ENV *dbenv;
00149         const char *progname;
00150 {
00151         DB *dbp;
00152         DBT key, data;
00153         u_int32_t len;
00154         int ret;
00155         char *p, *t, buf[1024], rbuf[1024];
00156 
00157         /* Remove the previous database. */
00158 
00159         /* Create and initialize database object, open the database. */
00160         if ((ret = db_create(&dbp, dbenv, 0)) != 0) {
00161                 fprintf(stderr,
00162                     "%s: db_create: %s\n", progname, db_strerror(ret));
00163                 return (ret);
00164         }
00165         if ((ret = dbp->set_pagesize(dbp, 1024)) != 0) {
00166                 dbp->err(dbp, ret, "set_pagesize");
00167                 goto err1;
00168         }
00169         if ((ret = dbp->open(dbp,
00170             NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
00171                 dbp->err(dbp, ret, "%s: open", DATABASE);
00172                 goto err1;
00173         }
00174 
00175         /*
00176          * Insert records into the database, where the key is the user
00177          * input and the data is the user input in reverse order.
00178          */
00179         memset(&key, 0, sizeof(DBT));
00180         memset(&data, 0, sizeof(DBT));
00181         for (;;) {
00182                 printf("input> ");
00183                 fflush(stdout);
00184                 if (fgets(buf, sizeof(buf), stdin) == NULL)
00185                         break;
00186                 if ((len = strlen(buf)) <= 1)
00187                         continue;
00188                 for (t = rbuf, p = buf + (len - 2); p >= buf;)
00189                         *t++ = *p--;
00190                 *t++ = '\0';
00191 
00192                 key.data = buf;
00193                 data.data = rbuf;
00194                 data.size = key.size = len - 1;
00195 
00196                 switch (ret =
00197                     dbp->put(dbp, NULL, &key, &data, DB_NOOVERWRITE)) {
00198                 case 0:
00199                         break;
00200                 default:
00201                         dbp->err(dbp, ret, "DB->put");
00202                         if (ret != DB_KEYEXIST)
00203                                 goto err1;
00204                         break;
00205                 }
00206                 memset(&data, 0, sizeof(DBT));
00207                 switch (ret = dbp->get(dbp, NULL, &key, &data, 0)) {
00208                 case 0:
00209                         printf("%.*s : %.*s\n",
00210                             (int)key.size, (char *)key.data,
00211                             (int)data.size, (char *)data.data);
00212                         break;
00213                 default:
00214                         dbp->err(dbp, ret, "DB->get");
00215                         break;
00216                 }
00217         }
00218         if ((ret = dbp->close(dbp, 0)) != 0) {
00219                 fprintf(stderr,
00220                     "%s: DB->close: %s\n", progname, db_strerror(ret));
00221                 return (1);
00222         }
00223         return (0);
00224 
00225 err1:   (void)dbp->close(dbp, 0);
00226         return (ret);
00227 }

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