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

ex_btrec.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1997-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: ex_btrec.c,v 12.1 2005/06/16 20:22:00 bostic Exp $
00008  */
00009 
00010 #include <sys/types.h>
00011 
00012 #include <errno.h>
00013 #include <stdlib.h>
00014 #include <string.h>
00015 
00016 #include <db.h>
00017 
00018 #define DATABASE        "access.db"
00019 #define WORDLIST        "../test/wordlist"
00020 int     main __P((void));
00021 
00022 int     ex_btrec __P((void));
00023 void    show __P((const char *, DBT *, DBT *));
00024 
00025 int
00026 main()
00027 {
00028         return (ex_btrec() == 1 ? EXIT_FAILURE : EXIT_SUCCESS);
00029 }
00030 
00031 int
00032 ex_btrec()
00033 {
00034         DB *dbp;
00035         DBC *dbcp;
00036         DBT key, data;
00037         DB_BTREE_STAT *statp;
00038         FILE *fp;
00039         db_recno_t recno;
00040         size_t len;
00041         int cnt, ret;
00042         char *p, *t, buf[1024], rbuf[1024];
00043         const char *progname = "ex_btrec";              /* Program name. */
00044 
00045         /* Open the word database. */
00046         if ((fp = fopen(WORDLIST, "r")) == NULL) {
00047                 fprintf(stderr, "%s: open %s: %s\n",
00048                     progname, WORDLIST, db_strerror(errno));
00049                 return (1);
00050         }
00051 
00052         /* Remove the previous database. */
00053         (void)remove(DATABASE);
00054 
00055         /* Create and initialize database object, open the database. */
00056         if ((ret = db_create(&dbp, NULL, 0)) != 0) {
00057                 fprintf(stderr,
00058                     "%s: db_create: %s\n", progname, db_strerror(ret));
00059                 return (1);
00060         }
00061         dbp->set_errfile(dbp, stderr);
00062         dbp->set_errpfx(dbp, progname);                 /* 1K page sizes. */
00063         if ((ret = dbp->set_pagesize(dbp, 1024)) != 0) {
00064                 dbp->err(dbp, ret, "set_pagesize");
00065                 return (1);
00066         }                                               /* Record numbers. */
00067         if ((ret = dbp->set_flags(dbp, DB_RECNUM)) != 0) {
00068                 dbp->err(dbp, ret, "set_flags: DB_RECNUM");
00069                 return (1);
00070         }
00071         if ((ret = dbp->open(dbp,
00072             NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
00073                 dbp->err(dbp, ret, "open: %s", DATABASE);
00074                 return (1);
00075         }
00076 
00077         /*
00078          * Insert records into the database, where the key is the word
00079          * preceded by its record number, and the data is the same, but
00080          * in reverse order.
00081          */
00082         memset(&key, 0, sizeof(DBT));
00083         memset(&data, 0, sizeof(DBT));
00084         for (cnt = 1; cnt <= 1000; ++cnt) {
00085                 (void)sprintf(buf, "%04d_", cnt);
00086                 if (fgets(buf + 4, sizeof(buf) - 4, fp) == NULL)
00087                         break;
00088                 len = strlen(buf);
00089                 for (t = rbuf, p = buf + (len - 2); p >= buf;)
00090                         *t++ = *p--;
00091                 *t++ = '\0';
00092 
00093                 key.data = buf;
00094                 data.data = rbuf;
00095                 data.size = key.size = (u_int32_t)len - 1;
00096 
00097                 if ((ret =
00098                     dbp->put(dbp, NULL, &key, &data, DB_NOOVERWRITE)) != 0) {
00099                         dbp->err(dbp, ret, "DB->put");
00100                         if (ret != DB_KEYEXIST)
00101                                 goto err1;
00102                 }
00103         }
00104 
00105         /* Close the word database. */
00106         (void)fclose(fp);
00107 
00108         /* Print out the number of records in the database. */
00109         if ((ret = dbp->stat(dbp, NULL, &statp, 0)) != 0) {
00110                 dbp->err(dbp, ret, "DB->stat");
00111                 goto err1;
00112         }
00113         printf("%s: database contains %lu records\n",
00114             progname, (u_long)statp->bt_ndata);
00115         free(statp);
00116 
00117         /* Acquire a cursor for the database. */
00118         if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) {
00119                 dbp->err(dbp, ret, "DB->cursor");
00120                 goto err1;
00121         }
00122 
00123         /*
00124          * Prompt the user for a record number, then retrieve and display
00125          * that record.
00126          */
00127         for (;;) {
00128                 /* Get a record number. */
00129                 printf("recno #> ");
00130                 fflush(stdout);
00131                 if (fgets(buf, sizeof(buf), stdin) == NULL)
00132                         break;
00133                 recno = atoi(buf);
00134 
00135                 /*
00136                  * Reset the key each time, the dbp->c_get() routine returns
00137                  * the key and data pair, not just the key!
00138                  */
00139                 key.data = &recno;
00140                 key.size = sizeof(recno);
00141                 if ((ret = dbcp->c_get(dbcp, &key, &data, DB_SET_RECNO)) != 0)
00142                         goto get_err;
00143 
00144                 /* Display the key and data. */
00145                 show("k/d\t", &key, &data);
00146 
00147                 /* Move the cursor a record forward. */
00148                 if ((ret = dbcp->c_get(dbcp, &key, &data, DB_NEXT)) != 0)
00149                         goto get_err;
00150 
00151                 /* Display the key and data. */
00152                 show("next\t", &key, &data);
00153 
00154                 /*
00155                  * Retrieve the record number for the following record into
00156                  * local memory.
00157                  */
00158                 data.data = &recno;
00159                 data.size = sizeof(recno);
00160                 data.ulen = sizeof(recno);
00161                 data.flags |= DB_DBT_USERMEM;
00162                 if ((ret = dbcp->c_get(dbcp, &key, &data, DB_GET_RECNO)) != 0) {
00163 get_err:                dbp->err(dbp, ret, "DBcursor->get");
00164                         if (ret != DB_NOTFOUND && ret != DB_KEYEMPTY)
00165                                 goto err2;
00166                 } else
00167                         printf("retrieved recno: %lu\n", (u_long)recno);
00168 
00169                 /* Reset the data DBT. */
00170                 memset(&data, 0, sizeof(data));
00171         }
00172 
00173         if ((ret = dbcp->c_close(dbcp)) != 0) {
00174                 dbp->err(dbp, ret, "DBcursor->close");
00175                 goto err1;
00176         }
00177         if ((ret = dbp->close(dbp, 0)) != 0) {
00178                 fprintf(stderr,
00179                     "%s: DB->close: %s\n", progname, db_strerror(ret));
00180                 return (1);
00181         }
00182 
00183         return (0);
00184 
00185 err2:   (void)dbcp->c_close(dbcp);
00186 err1:   (void)dbp->close(dbp, 0);
00187         return (ret);
00188 
00189 }
00190 
00191 /*
00192  * show --
00193  *      Display a key/data pair.
00194  */
00195 void
00196 show(msg, key, data)
00197         const char *msg;
00198         DBT *key, *data;
00199 {
00200         printf("%s%.*s : %.*s\n", msg,
00201             (int)key->size, (char *)key->data,
00202             (int)data->size, (char *)data->data);
00203 }

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