1/* 2 * $Id: b_del.c,v 1.15 2008/01/31 17:01:22 bostic Exp $ 3 */ 4#include "bench.h" 5 6static int usage(void); 7 8int 9b_del(int argc, char *argv[]) 10{ 11 extern char *optarg; 12 extern int optind; 13 DB *dbp; 14 DBC *dbc; 15 DBT key, data; 16 DBTYPE type; 17 db_recno_t recno; 18 u_int32_t cachesize; 19 int ch, i, count, ret, use_cursor; 20 char *ts, buf[32]; 21 22 type = DB_BTREE; 23 cachesize = MEGABYTE; 24 count = 100000; 25 use_cursor = 0; 26 ts = "Btree"; 27 while ((ch = getopt(argc, argv, "C:c:t:w")) != EOF) 28 switch (ch) { 29 case 'C': 30 cachesize = (u_int32_t)atoi(optarg); 31 break; 32 case 'c': 33 count = atoi(optarg); 34 break; 35 case 't': 36 switch (optarg[0]) { 37 case 'B': case 'b': 38 ts = "Btree"; 39 type = DB_BTREE; 40 break; 41 case 'H': case 'h': 42 if (b_util_have_hash()) 43 return (0); 44 ts = "Hash"; 45 type = DB_HASH; 46 break; 47 case 'Q': case 'q': 48 if (b_util_have_queue()) 49 return (0); 50 ts = "Queue"; 51 type = DB_QUEUE; 52 break; 53 case 'R': case 'r': 54 ts = "Recno"; 55 type = DB_RECNO; 56 break; 57 default: 58 return (usage()); 59 } 60 break; 61 case 'w': 62 use_cursor = 1; 63 break; 64 case '?': 65 default: 66 return (usage()); 67 } 68 argc -= optind; 69 argv += optind; 70 if (argc != 0) 71 return (usage()); 72 73 /* Create the database. */ 74 DB_BENCH_ASSERT(db_create(&dbp, NULL, 0) == 0); 75 DB_BENCH_ASSERT(dbp->set_cachesize(dbp, 0, cachesize, 0) == 0); 76 dbp->set_errfile(dbp, stderr); 77 78 /* Set record length for Queue. */ 79 if (type == DB_QUEUE) 80 DB_BENCH_ASSERT(dbp->set_re_len(dbp, 20) == 0); 81 82#if DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR >= 1 83 DB_BENCH_ASSERT( 84 dbp->open(dbp, NULL, TESTFILE, NULL, type, DB_CREATE, 0666) == 0); 85#else 86 DB_BENCH_ASSERT( 87 dbp->open(dbp, TESTFILE, NULL, type, DB_CREATE, 0666) == 0); 88#endif 89 90 /* Initialize the data. */ 91 memset(&key, 0, sizeof(key)); 92 memset(&data, 0, sizeof(data)); 93 data.data = "01234567890123456789"; 94 data.size = 20; 95 96 /* Store a key/data pair. */ 97 switch (type) { 98 case DB_BTREE: 99 case DB_HASH: 100 key.data = buf; 101 key.size = 10; 102 break; 103 case DB_QUEUE: 104 case DB_RECNO: 105 key.data = &recno; 106 key.size = sizeof(recno); 107 break; 108 case DB_UNKNOWN: 109 b_util_abort(); 110 break; 111 } 112 113 /* Insert count in-order key/data pairs. */ 114 if (type == DB_BTREE || type == DB_HASH) 115 for (i = 0; i < count; ++i) { 116 (void)snprintf(buf, sizeof(buf), "%010d", i); 117 DB_BENCH_ASSERT( 118 dbp->put(dbp, NULL, &key, &data, 0) == 0); 119 } 120 else 121 for (i = 0, recno = 1; i < count; ++i, ++recno) 122 DB_BENCH_ASSERT( 123 dbp->put(dbp, NULL, &key, &data, 0) == 0); 124 125 /* Delete the records. */ 126 TIMER_START; 127 if (use_cursor) { 128 DB_BENCH_ASSERT(dbp->cursor(dbp, NULL, &dbc, 0) == 0); 129 while ((ret = dbc->c_get(dbc, &key, &data, DB_NEXT)) == 0) 130 DB_BENCH_ASSERT(dbc->c_del(dbc, 0) == 0); 131 DB_BENCH_ASSERT (ret == DB_NOTFOUND); 132 } else 133 if (type == DB_BTREE || type == DB_HASH) 134 for (i = 0; i < count; ++i) { 135 (void)snprintf(buf, sizeof(buf), "%010d", i); 136 DB_BENCH_ASSERT( 137 dbp->del(dbp, NULL, &key, 0) == 0); 138 } 139 else 140 for (i = 0, recno = 1; i < count; ++i, ++recno) 141 DB_BENCH_ASSERT( 142 dbp->del(dbp, NULL, &key, 0) == 0); 143 144 TIMER_STOP; 145 146 printf( 147 "# %d %s database in-order delete of 10/20 byte key/data pairs using %s\n", 148 count, ts, use_cursor ? "a cursor" : "the key"); 149 TIMER_DISPLAY(count); 150 151 DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0); 152 153 return (0); 154} 155 156static int 157usage() 158{ 159 (void)fprintf(stderr, 160 "usage: b_del [-w] [-C cachesz] [-c count] [-t type]\n"); 161 return (EXIT_FAILURE); 162} 163