summaryrefslogtreecommitdiff
path: root/db-4.8.30/examples_c/csv/load.c
blob: 34d8cc1895f52b2e1671a1203d2915dd3b0f01f3 (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2005-2009 Oracle.  All rights reserved.
 *
 * $Id$
 */

#include "csv.h"
#include "csv_local.h"
#include "csv_extern.h"

typedef enum { GL_OK, GL_EOF, GL_FAIL } getline_status;

static int input_field_count(const char *, size_t, u_int32_t *);
static getline_status
	   input_getline(char **, size_t *, size_t *);
static int input_put_alloc(u_int32_t **, size_t *, size_t, u_int32_t);
static int input_set_offset(u_int32_t *, char *, size_t, u_int32_t);

static input_fmt ifmt;			/* Input format. */
static u_long	 record_count = 0;	/* Input record count for errors. */
static u_long	 version;		/* Version we're loading. */

/*
 * input_load --
 *	Read the input file and load new records into the database.
 */
int
input_load(input_fmt ifmt_arg, u_long version_arg)
{
	getline_status gtl_status;
	DBT key, data;
	DBC *cursor;
	u_int32_t field_count, primary_key, *put_line;
	size_t input_len, len, put_len;
	int is_first, ret;
	char *input_line;

	field_count = 0;			/* Shut the compiler up. */

	/* ifmt and version are global to this file. */
	ifmt = ifmt_arg;
	version = version_arg;

	/*
	 * The primary key for the database is a unique number.  Find out the
	 * last unique number allocated in this database by opening a cursor
	 * and fetching the last record.
	 */
	if ((ret = db->cursor(db, NULL, &cursor, 0)) != 0) {
		dbenv->err(dbenv, ret, "DB->cursor");
		return (1);
	}
	memset(&key, 0, sizeof(key));
	memset(&data, 0, sizeof(data));
	if ((ret = cursor->c_get(cursor, &key, &data, DB_LAST)) != 0)
		if (ret == DB_NOTFOUND)
			primary_key = 0;
		else {
			dbenv->err(dbenv, ret, "DB->cursor: DB_LAST");
			return (1);
		}
	else
		memcpy(&primary_key, key.data, sizeof(primary_key));
	if ((ret = cursor->c_close(cursor)) != 0) {
		dbenv->err(dbenv, ret, "DBC->close");
		return (1);
	}
	if (verbose)
		dbenv->errx(dbenv,
		    "maximum existing record in the database is %lu",
		    (u_long)primary_key);

	key.data = &primary_key;
	key.size = sizeof(primary_key);
	input_line = NULL;
	put_line = NULL;
	input_len = put_len = 0;

	/*
	 * See the README file for a description of the file input format.
	 */
	for (is_first = 1; (gtl_status =
	    input_getline(&input_line, &input_len, &len)) == GL_OK;) {
		++record_count;
		if (verbose > 1)
			dbenv->errx(dbenv, "reading %lu", (u_long)record_count);

		/* The first non-blank line of the input is a column map. */
		if (is_first) {
			is_first = 0;

			/* Count the fields we're expecting in the input. */
			if (input_field_count(
			    input_line, len, &field_count) != 0)
				return (1);

		}

		/* Allocate room for the table of offsets. */
		if (input_put_alloc(
		    &put_line, &put_len, len, field_count) != 0)
			return (1);

		/*
		 * Build the offset table and create the record we're
		 * going to store.
		 */
		if (input_set_offset(put_line,
		    input_line, len, field_count) != 0)
			return (1);

		++primary_key;

		memcpy(put_line + (field_count + 2), input_line, len);
		data.data = put_line;
		data.size = (field_count + 2) * sizeof(u_int32_t) + len;

		if (verbose > 1)
			(void)entry_print(
			    data.data, data.size, field_count);

		/* Load the key/data pair into the database. */
		if ((ret = db->put(db, NULL, &key, &data, 0)) != 0) {
			dbenv->err(dbenv, ret,
			    "DB->put: %lu", (u_long)primary_key);
			return (1);
		}
	}

	if (gtl_status != GL_EOF)
		return (1);

	if (verbose)
		dbenv->errx(dbenv,
		    "%lu records read from the input file into the database",
		    record_count);

	/*
	 * This program isn't transactional, limit the window for corruption.
	 */
	if ((ret = db->sync(db, 0)) != 0) {
		dbenv->err(dbenv, ret, "DB->sync");
		return (1);
	}

	return (0);
}

/*
 * input_getline --
 *	Read in a line of input into a buffer.
 */
static getline_status
input_getline(char **input_linep, size_t *input_lenp, size_t *lenp)
{
	size_t input_len, len;
	int ch;
	char *input_line, *p, *endp;

	input_line = *input_linep;
	input_len = *input_lenp;

	p = input_line;
	endp = input_line + input_len;

	for (len = 0; (ch = getchar()) != EOF;) {
		if (ch == '\0')		/* Strip <nul> (\000) bytes. */
			continue;
		switch (ifmt) {
		case FORMAT_NL:
			if (ch == '\n')
				goto end;
			break;
		case FORMAT_EXCEL:
			/* Strip <nl> (\012) bytes. */
			if (ch == '\n')
				continue;
			/*
			 * <cr> (\015) bytes terminate lines.
			 * Skip blank lines.
			 */
			if (ch == '\015') {
				if (len == 0)
					continue;
				goto end;
			}
		}
		if (input_line == endp) {
			input_len += 256;
			input_len *= 2;
			if ((input_line =
			    realloc(input_line, input_len)) == NULL) {
				dbenv->err(dbenv, errno,
				    "unable to allocate %lu bytes for record",
				    (u_long)input_len);
				return (GL_FAIL);
			}
			p = input_line;
			endp = p + input_len;
		}

		if (isprint(ch)) {	/* Strip unprintables. */
			*p++ = (char)ch;
			++len;
		}
	}

end:	if (len == 0)
		return (GL_EOF);

	*lenp = len;
	*input_linep = input_line;
	*input_lenp = input_len;

	return (GL_OK);
}

/*
 * input_field_count --
 *	Count the fields in the line.
 */
static int
input_field_count(const char *line, size_t len, u_int32_t *field_countp)
{
	u_int32_t field_count;
	int quoted;

	field_count = 1;

	/*
	 * There are N-1 separators for N fields, that is, "a,b,c" is three
	 * fields, with two comma separators.
	 */
	switch (ifmt) {
	case FORMAT_EXCEL:
		quoted = 0;
		for (field_count = 1; len > 0; ++line, --len)
			if (*line == '"')
				quoted = !quoted;
			else if (*line == ',' && !quoted)
				++field_count;
		break;
	case FORMAT_NL:
		for (field_count = 1; len > 0; ++line, --len)
			if (*line == ',')
				++field_count;
		break;
	}
	*field_countp = field_count;

	if (verbose)
		dbenv->errx(dbenv,
		    "input file made up of %lu fields", (u_int)field_count);

	return (0);
}

/*
 * input_put_alloc --
 *	Allocate room for the offset table plus the input.
 */
static int
input_put_alloc(u_int32_t **put_linep,
    size_t *put_lenp, size_t len, u_int32_t field_count)
{
	size_t total;

	total = (field_count + 2) * sizeof(u_int32_t) + len;
	if (total > *put_lenp &&
	    (*put_linep = realloc(*put_linep, *put_lenp += total)) == NULL) {
		dbenv->err(dbenv, errno,
		    "unable to allocate %lu bytes for record",
		    (u_long)*put_lenp);
		return (1);
	}
	return (0);
}

/*
 * input_set_offset --
 *	Build an offset table and record combination.
 */
static int
input_set_offset(u_int32_t *put_line,
    char *input_line, size_t len, u_int32_t field_count)
{
	u_int32_t *op;
	int quoted;
	char *p, *endp;

	op = put_line;

	/* The first field is the version number. */
	*op++ = version;

	/*
	 * Walk the input line, looking for comma separators.  It's an error
	 * to have too many or too few fields.
	 */
	*op++ = 0;
	quoted = 0;
	for (p = input_line, endp = input_line + len;; ++p) {
		if (ifmt == FORMAT_EXCEL && p < endp) {
			if (*p == '"')
				quoted = !quoted;
			if (quoted)
				continue;
		}
		if (*p == ',' || p == endp) {
			if (field_count == 0) {
				dbenv->errx(dbenv,
				    "record %lu: too many fields in the record",
				    record_count);
				return (1);
			}
			--field_count;

			*op++ = (u_int32_t)(p - input_line) + 1;

			if (verbose > 1)
				dbenv->errx(dbenv,
				    "offset %lu: {%.*s}", op[-1],
				    OFFSET_LEN(op, -2), input_line + op[-2]);

			/*
			 * Don't insert a new field if the input lines ends
			 * in a comma.
			 */
			if (p == endp || p + 1 == endp)
				break;
		}
	}
	*op++ = (u_int32_t)(p - input_line);

	if (field_count != 0) {
		dbenv->errx(dbenv,
		    "record %lu: not enough fields in the record",
		    record_count);
		return (1);
	}
	memcpy(op, input_line, len);

	return (0);
}