diff options
Diffstat (limited to 'src/utilities')
-rw-r--r-- | src/utilities/db_dump/util_dump.c | 83 | ||||
-rw-r--r-- | src/utilities/db_load/util_load.c | 292 | ||||
-rw-r--r-- | src/utilities/db_stat/util_stat.c | 67 | ||||
-rw-r--r-- | src/utilities/db_verify/util_verify.c | 74 |
4 files changed, 516 insertions, 0 deletions
diff --git a/src/utilities/db_dump/util_dump.c b/src/utilities/db_dump/util_dump.c new file mode 100644 index 00000000000..68cc6d69061 --- /dev/null +++ b/src/utilities/db_dump/util_dump.c @@ -0,0 +1,83 @@ +/* + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2009 WiredTiger Software. + * All rights reserved. + * + * $Id$ + */ + +#include "wt_internal.h" +#include "util.h" + +const char *progname; + +int usage(void); + +int +main(int argc, char *argv[]) +{ + extern char *optarg; + extern int optind; + DB *db; + u_int32_t flags; + int ch, ret, tret; + + WT_UTILITY_INTRO(progname, argv); + + flags = 0; + while ((ch = getopt(argc, argv, "df:p")) != EOF) + switch (ch) { + case 'd': + flags = WT_DEBUG; + break; + case 'f': /* output file */ + if (freopen(optarg, "w", stdout) == NULL) { + fprintf(stderr, "%s: %s: reopen: %s\n", + progname, optarg, strerror(errno)); + return (EXIT_FAILURE); + } + break; + case 'p': + flags = WT_PRINTABLES; + break; + case 'V': /* version */ + printf("%s\n", wiredtiger_version(NULL, NULL, NULL)); + return (EXIT_SUCCESS); + case '?': + default: + return (usage()); + } + argc -= optind; + argv += optind; + + /* The remaining argument is the database name. */ + if (argc != 1) + return (usage()); + + if ((ret = wiredtiger_simple_setup(progname, &db, 0, 0)) == 0) { + if ((ret = db->open(db, *argv, 0, 0)) != 0) { + db->err(db, ret, "Db.open: %s", *argv); + goto err; + } + if ((ret = db->dump(db, stdout, NULL, flags)) != 0) { + db->err(db, ret, "Db.dump"); + goto err; + } + } + + if (0) { +err: ret = 1; + } + if ((tret = wiredtiger_simple_teardown(progname, db)) != 0 && ret == 0) + ret = tret; + return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE); +} + +int +usage() +{ + (void)fprintf(stderr, + "usage: %s [-dpV] [-f output-file] database\n", progname); + return (EXIT_FAILURE); +} diff --git a/src/utilities/db_load/util_load.c b/src/utilities/db_load/util_load.c new file mode 100644 index 00000000000..6ededed7c28 --- /dev/null +++ b/src/utilities/db_load/util_load.c @@ -0,0 +1,292 @@ +/* + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2009 WiredTiger Software. + * All rights reserved. + * + * $Id$ + */ + +#include "wt_internal.h" +#include "util.h" + +const char *progname; + +int bulk_callback(DB *, DBT **, DBT **); +int bulk_read(DBT *dbt, int); +int config_read(char **); +int config_read_single(char *); +int config_set(DB *); +int usage(void); + +struct { + int pagesize_set; + u_long allocsize, intlmin, intlmax, leafmin, leafmax; +} config; + +int +main(int argc, char *argv[]) +{ + extern char *optarg; + extern int optind; + DB *db; + int ch, ret, text_input, tret, verbose; + char **config_list, **config_next; + + WT_UTILITY_INTRO(progname, argv); + + /* + * We can't handle configuration-line information until we've opened + * the DB handle, so we need a place to store it for now. + */ + if ((config_next = + config_list = calloc(argc + 1, sizeof(char *))) == NULL) { + fprintf(stderr, "%s: %s\n", progname, strerror(errno)); + return (EXIT_FAILURE); + } + + text_input = verbose = 0; + while ((ch = getopt(argc, argv, "c:f:TVv")) != EOF) + switch (ch) { + case 'c': /* command-line option */ + *config_next++ = optarg; + break; + case 'f': /* input file */ + if (freopen(optarg, "r", stdin) == NULL) { + fprintf(stderr, "%s: %s: reopen: %s\n", + progname, optarg, strerror(errno)); + return (EXIT_FAILURE); + } + break; + case 'T': + text_input = 1; + break; + case 'V': /* version */ + printf("%s\n", wiredtiger_version(NULL, NULL, NULL)); + return (EXIT_SUCCESS); + case 'v': + verbose = 1; + break; + case '?': + default: + return (usage()); + } + argc -= optind; + argv += optind; + + /* The remaining argument is the database name. */ + if (argc != 1) + return (usage()); + + /* + * Read through the command-line configuration options and convert + * to the config structure. + */ + if (config_read(config_list) != 0) + goto err; + + /* + * Right now, we only support text input -- require the T option to + * match Berkeley DB's API. + */ + if (text_input == 0) { + fprintf(stderr, + "%s: the -T option is currently required\n", progname); + return (EXIT_FAILURE); + } + + if ((ret = wiredtiger_simple_setup(progname, &db, 0, 0)) == 0) { + if (config_set(db) != 0) + goto err; + + (void)remove(*argv); + + if ((ret = db->open(db, *argv, 0600, WT_CREATE)) != 0) { + db->err(db, ret, "Db.open: %s", *argv); + goto err; + } + + if ((ret = db->bulk_load(db, WT_DUPLICATES, + verbose ? __wt_progress : NULL, bulk_callback)) != 0) { + db->err(db, ret, "Db.bulk_load"); + goto err; + } + if (verbose) + printf("\n"); + } + + if (0) { +err: ret = 1; + } + if ((tret = wiredtiger_simple_teardown(progname, db)) != 0 && ret == 0) + ret = tret; + return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE); +} + +/* + * config_read -- + * Convert command-line options into the config structure. + */ +int +config_read(char **list) +{ + int ret; + + for (; *list != NULL; ++list) + if ((ret = config_read_single(*list)) != 0) + return (ret); + return (0); +} + +/* + * config_read_single -- + * Process a single command-line configuration option, converting it into + * the config structure. + */ +int +config_read_single(char *opt) +{ + u_long v; + char *p, *ep; + + /* Get pointers to the two parts of an X=Y format string. */ + if ((p = strchr(opt, '=')) == NULL || p[1] == '\0') + goto format; + *p++ = '\0'; + v = strtoul(p, &ep, 10); + if (v == ULONG_MAX && errno == ERANGE) { +format: fprintf(stderr, + "%s: -c option %s is not correctly formatted\n", + progname, opt); + return (1); + } + if (strcmp(opt, "allocsize") == 0) { + config.allocsize = v; + config.pagesize_set = 1; + return (0); + } + if (strcmp(opt, "intlmin") == 0) { + config.intlmin = v; + config.pagesize_set = 1; + return (0); + } + if (strcmp(opt, "intlmax") == 0) { + config.intlmax = v; + config.pagesize_set = 1; + return (0); + } + if (strcmp(opt, "leafmin") == 0) { + config.leafmin = v; + config.pagesize_set = 1; + return (0); + } + if (strcmp(opt, "leafmax") == 0) { + config.leafmax = v; + config.pagesize_set = 1; + return (0); + } + + fprintf(stderr, + "%s: -c option %s has an unknown keyword\n", progname, opt); + return (1); +} + +/* + * config_set -- + * Set the command-line configuration options on the database handle. + */ +int +config_set(DB *db) +{ + u_int32_t allocsize, intlmin, intlmax, leafmin, leafmax; + int ret; + + if (config.pagesize_set) { + if ((ret = db->btree_pagesize_get(db, + &allocsize, &intlmin, &intlmax, &leafmin, &leafmax)) != 0) { + db->err(db, ret, "Db.btree_pagesize_get"); + return (1); + } + if (config.allocsize != 0) + allocsize = config.allocsize; + if (config.intlmin != 0) + intlmin = config.intlmin; + if (config.intlmax != 0) + intlmax = config.intlmax; + if (config.leafmin != 0) + leafmin = config.leafmin; + if (config.leafmax != 0) + leafmax = config.leafmax; + if ((ret = db->btree_pagesize_set(db, + allocsize, intlmin, intlmax, leafmin, leafmax)) != 0) { + db->err(db, ret, "Db.btree_pagesize_set"); + return (1); + } + } + + return (0); +} + +/* + * bulk_read -- + * Read a line from stdin into a DBT. + */ +int +bulk_read(DBT *dbt, int iskey) +{ + static u_int64_t line = 0; + size_t len; + int ch; + + ++line; + for (len = 0;; ++len) { + if ((ch = getchar()) == EOF) { + if (iskey && len == 0) + return (1); + fprintf(stderr, "%s: corrupted input at line %llu\n", + progname, line); + return (WT_ERROR); + } + if (ch == '\n') + break; + if (len >= dbt->mem_size) { + if ((dbt->data = realloc(dbt->data, len + 128)) == NULL) + return (errno); + dbt->mem_size = len + 128; + } + ((u_int8_t *)(dbt->data))[len] = ch; + } + dbt->size = len; + return (0); +} + +/* + * bulk_callback -- + * Bulk-load callback function. + */ +int +bulk_callback(DB *db, DBT **keyp, DBT **datap) +{ + static DBT key, data; + int ret; + + WT_CC_QUIET(db, NULL); + + if ((ret = bulk_read(&key, 1)) != 0) + return (ret); + if ((ret = bulk_read(&data, 0)) != 0) + return (ret); + + *keyp = &key; + *datap = &data; + return (0); +} + +int +usage() +{ + (void)fprintf(stderr, + "usage: %s [-TVv] [-c configuration] [-f input-file] database\n", + progname); + return (EXIT_FAILURE); +} diff --git a/src/utilities/db_stat/util_stat.c b/src/utilities/db_stat/util_stat.c new file mode 100644 index 00000000000..afb2f94cba8 --- /dev/null +++ b/src/utilities/db_stat/util_stat.c @@ -0,0 +1,67 @@ +/* + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2009 WiredTiger Software. + * All rights reserved. + * + * $Id$ + */ + +#include "wt_internal.h" +#include "util.h" + +const char *progname; + +int usage(void); + +int +main(int argc, char *argv[]) +{ + extern char *optarg; + extern int optind; + DB *db; + int ch, ret, tret; + + WT_UTILITY_INTRO(progname, argv); + + while ((ch = getopt(argc, argv, "V")) != EOF) + switch (ch) { + case 'V': /* version */ + printf("%s\n", wiredtiger_version(NULL, NULL, NULL)); + return (EXIT_SUCCESS); + case '?': + default: + return (usage()); + } + argc -= optind; + argv += optind; + + /* The remaining argument is the database name. */ + if (argc != 1) + return (usage()); + + if ((ret = wiredtiger_simple_setup(progname, &db, 0, 0)) == 0) { + if ((ret = db->open(db, *argv, 0, 0)) != 0) { + db->err(db, ret, "Db.open: %s", *argv); + goto err; + } + if ((ret = db->stat_print(db, stdout, 0)) != 0) { + db->err(db, ret, "Db.stat: %s", *argv); + goto err; + } + } + + if (0) { +err: ret = 1; + } + if ((tret = wiredtiger_simple_teardown(progname, db)) != 0 && ret == 0) + ret = tret; + return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE); +} + +int +usage() +{ + (void)fprintf(stderr, "usage: %s [-V] database\n", progname); + return (EXIT_FAILURE); +} diff --git a/src/utilities/db_verify/util_verify.c b/src/utilities/db_verify/util_verify.c new file mode 100644 index 00000000000..5c5bd02407f --- /dev/null +++ b/src/utilities/db_verify/util_verify.c @@ -0,0 +1,74 @@ +/* + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2009 WiredTiger Software. + * All rights reserved. + * + * $Id$ + */ + +#include "wt_internal.h" +#include "util.h" + +const char *progname; + +int usage(void); + +int +main(int argc, char *argv[]) +{ + extern char *optarg; + extern int optind; + DB *db; + int ch, ret, tret, verbose; + + WT_UTILITY_INTRO(progname, argv); + + verbose = 0; + while ((ch = getopt(argc, argv, "Vv")) != EOF) + switch (ch) { + case 'v': /* verbose */ + verbose = 1; + break; + case 'V': /* version */ + printf("%s\n", wiredtiger_version(NULL, NULL, NULL)); + return (EXIT_SUCCESS); + case '?': + default: + return (usage()); + } + argc -= optind; + argv += optind; + + /* The remaining argument is the database name. */ + if (argc != 1) + return (usage()); + + if ((ret = wiredtiger_simple_setup(progname, &db, 0, 0)) == 0) { + if ((ret = db->open(db, *argv, 0, 0)) != 0) { + db->err(db, ret, "Db.open: %s", *argv); + goto err; + } + if ((ret = + db->verify(db, verbose ? __wt_progress : NULL, 0)) != 0) { + db->err(db, ret, "Db.verify: %s", *argv); + goto err; + } + if (verbose) + printf("\n"); + } + + if (0) { +err: ret = 1; + } + if ((tret = wiredtiger_simple_teardown(progname, db)) != 0 && ret == 0) + ret = tret; + return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE); +} + +int +usage() +{ + (void)fprintf(stderr, "usage: %s [-Vv] database\n", progname); + return (EXIT_FAILURE); +} |