| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <db.h>
- #include <fcntl.h>
- #include <limits.h>
- #include <unistd.h>
- #include <err.h>
- #include <string.h>
- #include <sys/types.h>
- #include <malloc_np.h>
- #include <getopt.h>
- #include <sys/stat.h>
- #include "bcompare.h"
- /* MALLOC_CONF=stats_print:true,junk:true */
- static DB * btreedb;
- #define KEYDATA_MAX 64
- static struct option longopts[] = {
- { "db", required_argument, NULL, 'd' },
- { "size", required_argument, NULL, 's' },
- { NULL, 0, NULL, 0 }
- };
- __attribute__((noreturn)) static int usage(void)
- {
- fputs("btest [--db filename]\n", stderr);
- exit (EXIT_SUCCESS);
- }
- int main(int argc, char *argv[])
- {
- char *dbname = NULL;
- size_t loops = 100;
- char buf[PATH_MAX];
- BTREEINFO type = {};
- DBT key = {};
- key.data = malloc(sizeof (size_t));
- DBT data = {};
- data.data = malloc(KEYDATA_MAX);
- size_t j;
- key.data = &j;
- key.size = sizeof j;
- int ch;
- while ((ch = getopt_long(argc, argv, "s:d:", longopts, NULL)) != -1) {
- switch (ch) {
- case 's':
- loops = strtoul(optarg, NULL, 10);
- break;
- case 'd':
- dbname = optarg;
- break;
- case 0:
- /* long option */
- break;
- default:
- usage();
- }
- }
- if (optind != argc)
- usage();
- if (dbname == NULL) {
- const char *t = getenv("TMPDIR");
- if (t == NULL)
- t = "/tmp";
- (void)snprintf(buf, sizeof(buf), "%s/david", t);
- dbname = buf;
- }
- type.compare = compare;
- btreedb= dbopen(dbname, O_CREAT | O_RDWR | O_SYNC , S_IRUSR | S_IWUSR, DB_BTREE, &type);
- if (!btreedb)
- err(EXIT_FAILURE, "Database unreachable:");
- for (j = 0; j < loops; ++j) {
- data.size = (size_t)snprintf(data.data, KEYDATA_MAX, "coucou %zu", j);
- if (btreedb->put(btreedb, &key, &data, R_NOOVERWRITE)) {
- warnx("Failed to insert data: %zu", j);
- }
- }
- free(data.data);
- btreedb->close(btreedb);
- printf("Data stored into %s.\n", dbname);
- }
|