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

t.c

00001 #include <sys/types.h>
00002 
00003 #include <errno.h>
00004 #include <fcntl.h>
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include <string.h>
00008 
00009 #include "db_185.h"
00010 
00011 void    err(char *);
00012 int     mycmp(const DBT *, const DBT *);
00013 void    ops(DB *, int);
00014 
00015 int
00016 main()
00017 {
00018         DB *dbp;
00019         HASHINFO h_info;
00020         BTREEINFO b_info;
00021         RECNOINFO r_info;
00022 
00023         printf("\tBtree...\n");
00024         memset(&b_info, 0, sizeof(b_info));
00025         b_info.flags = R_DUP;
00026         b_info.cachesize = 100 * 1024;
00027         b_info.psize = 512;
00028         b_info.lorder = 4321;
00029         b_info.compare = mycmp;
00030         (void)remove("a.db");
00031         if ((dbp =
00032            dbopen("a.db", O_CREAT | O_RDWR, 0664, DB_BTREE, &b_info)) == NULL)
00033                 err("dbopen: btree");
00034         ops(dbp, DB_BTREE);
00035 
00036         printf("\tHash...\n");
00037         memset(&h_info, 0, sizeof(h_info));
00038         h_info.bsize = 512;
00039         h_info.ffactor = 6;
00040         h_info.nelem = 1000;
00041         h_info.cachesize = 100 * 1024;
00042         h_info.lorder = 1234;
00043         (void)remove("a.db");
00044         if ((dbp =
00045             dbopen("a.db", O_CREAT | O_RDWR, 0664, DB_HASH, &h_info)) == NULL)
00046                 err("dbopen: hash");
00047         ops(dbp, DB_HASH);
00048 
00049         printf("\tRecno...\n");
00050         memset(&r_info, 0, sizeof(r_info));
00051         r_info.flags = R_FIXEDLEN;
00052         r_info.cachesize = 100 * 1024;
00053         r_info.psize = 1024;
00054         r_info.reclen = 37;
00055         (void)remove("a.db");
00056         if ((dbp =
00057            dbopen("a.db", O_CREAT | O_RDWR, 0664, DB_RECNO, &r_info)) == NULL)
00058                 err("dbopen: recno");
00059         ops(dbp, DB_RECNO);
00060 
00061         return (0);
00062 }
00063 
00064 int
00065 mycmp(a, b)
00066         const DBT *a, *b;
00067 {
00068         size_t len;
00069         u_int8_t *p1, *p2;
00070 
00071         len = a->size > b->size ? b->size : a->size;
00072         for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2)
00073                 if (*p1 != *p2)
00074                         return ((long)*p1 - (long)*p2);
00075         return ((long)a->size - (long)b->size);
00076 }
00077 
00078 void
00079 ops(dbp, type)
00080         DB *dbp;
00081         int type;
00082 {
00083         FILE *outfp;
00084         DBT key, data;
00085         recno_t recno;
00086         int i, ret;
00087         char buf[64];
00088 
00089         memset(&key, 0, sizeof(key));
00090         memset(&data, 0, sizeof(data));
00091 
00092         for (i = 1; i < 100; ++i) {             /* Test DB->put. */
00093                 sprintf(buf, "abc_%d_efg", i);
00094                 if (type == DB_RECNO) {
00095                         recno = i;
00096                         key.data = &recno;
00097                         key.size = sizeof(recno);
00098                 } else {
00099                         key.data = data.data = buf;
00100                         key.size = data.size = strlen(buf);
00101                 }
00102 
00103                 data.data = buf;
00104                 data.size = strlen(buf);
00105                 if (dbp->put(dbp, &key, &data, 0))
00106                         err("DB->put");
00107         }
00108 
00109         if (type == DB_RECNO) {                 /* Test DB->get. */
00110                 recno = 97;
00111                 key.data = &recno;
00112                 key.size = sizeof(recno);
00113         } else {
00114                 key.data = buf;
00115                 key.size = strlen(buf);
00116         }
00117         sprintf(buf, "abc_%d_efg", 97);
00118         if (dbp->get(dbp, &key, &data, 0) != 0)
00119                 err("DB->get");
00120         if (memcmp(data.data, buf, strlen(buf)))
00121                 err("DB->get: wrong data returned");
00122 
00123         if (type == DB_RECNO) {                 /* Test DB->put no-overwrite. */
00124                 recno = 42;
00125                 key.data = &recno;
00126                 key.size = sizeof(recno);
00127         } else {
00128                 key.data = buf;
00129                 key.size = strlen(buf);
00130         }
00131         sprintf(buf, "abc_%d_efg", 42);
00132         if (dbp->put(dbp, &key, &data, R_NOOVERWRITE) == 0)
00133                 err("DB->put: no-overwrite succeeded");
00134 
00135         if (type == DB_RECNO) {                 /* Test DB->del. */
00136                 recno = 35;
00137                 key.data = &recno;
00138                 key.size = sizeof(recno);
00139         } else {
00140                 sprintf(buf, "abc_%d_efg", 35);
00141                 key.data = buf;
00142                 key.size = strlen(buf);
00143         }
00144         if (dbp->del(dbp, &key, 0))
00145                 err("DB->del");
00146 
00147                                                 /* Test DB->seq. */
00148         if ((outfp = fopen("output", "w")) == NULL)
00149                 err("fopen: output");
00150         while ((ret = dbp->seq(dbp, &key, &data, R_NEXT)) == 0) {
00151                 if (type == DB_RECNO)
00152                         fprintf(outfp, "%d\n", *(int *)key.data);
00153                 else
00154                         fprintf(outfp,
00155                             "%.*s\n", (int)key.size, (char *)key.data);
00156                 fprintf(outfp, "%.*s\n", (int)data.size, (char *)data.data);
00157         }
00158         if (ret != 1)
00159                 err("DB->seq");
00160         fclose(outfp);
00161         switch (type) {
00162         case DB_BTREE:
00163                 ret = system("cmp output O.BH");
00164                 break;
00165         case DB_HASH:
00166                 ret = system("sort output | cmp - O.BH");
00167                 break;
00168         case DB_RECNO:
00169                 ret = system("cmp output O.R");
00170                 break;
00171         }
00172         if (ret != 0)
00173                 err("output comparison failed");
00174 
00175         if (dbp->sync(dbp, 0))                  /* Test DB->sync. */
00176                 err("DB->sync");
00177 
00178         if (dbp->close(dbp))                    /* Test DB->close. */
00179                 err("DB->close");
00180 }
00181 
00182 void
00183 err(s)
00184         char *s;
00185 {
00186         fprintf(stderr, "\t%s: %s\n", s, strerror(errno));
00187         exit (1);
00188 }

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