summaryrefslogtreecommitdiff
path: root/utilities/db_load/util_load.c
blob: 6ededed7c282290d3d752e4583e73d47ad2d2fd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
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);
}