summaryrefslogtreecommitdiff
path: root/src/btree/bt_dump.c
blob: 503f06c24b6c22309608232a462c13e596db35a9 (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2008-2011 WiredTiger, Inc.
 *	All rights reserved.
 *
 * $Id$
 */

#include "wt_internal.h"

typedef struct {
	void (*p)				/* Print function */
	    (uint8_t *, uint32_t, FILE *);
	FILE *stream;				/* Dump stream */

	void (*f)(const char *, uint64_t);	/* Progress callback */
	uint64_t fcnt;				/* Progress counter */

	DBT *dupkey;				/* Offpage duplicate tree key */
} WT_DSTUFF;

static int  __wt_dump_page(WT_TOC *, WT_PAGE *, void *);
static void __wt_dump_page_col_fix(WT_TOC *, WT_PAGE *, WT_DSTUFF *);
static int  __wt_dump_page_col_rle(WT_TOC *, WT_PAGE *, WT_DSTUFF *);
static int  __wt_dump_page_col_var(WT_TOC *, WT_PAGE *, WT_DSTUFF *);
static int  __wt_dump_page_dup_leaf(WT_TOC *, WT_PAGE *, WT_DSTUFF *);
static int  __wt_dump_page_row_leaf(WT_TOC *, WT_PAGE *, WT_DSTUFF *);
static void __wt_print_byte_string_hex(uint8_t *, uint32_t, FILE *);
static void __wt_print_byte_string_nl(uint8_t *, uint32_t, FILE *);

/*
 * __wt_db_dump --
 *	Db.dump method.
 */
int
__wt_db_dump(WT_TOC *toc,
    FILE *stream, void (*f)(const char *, uint64_t), uint32_t flags)
{
	WT_DSTUFF dstuff;
	int ret;

	if (LF_ISSET(WT_DEBUG)) {
		/*
		 * We use the verification code to do debugging dumps because
		 * if we're dumping in debugging mode, we want to confirm the
		 * page is OK before blindly reading it.
		 */
		return (__wt_verify(toc, f, stream));
	}

	dstuff.p = flags == WT_PRINTABLES ?
	    __wt_print_byte_string_nl : __wt_print_byte_string_hex;
	dstuff.stream = stream;
	dstuff.f = f;
	dstuff.fcnt = 0;
	dstuff.dupkey = NULL;

	/*
	 * Note we do not have a hazard reference for the root page, and that's
	 * safe -- root pages are pinned into memory when a database is opened,
	 * and never re-written until the database is closed.
	 */
	fprintf(stream, "VERSION=1\n");
	fprintf(stream, "HEADER=END\n");
	ret = __wt_tree_walk(toc, NULL, 0, __wt_dump_page, &dstuff);
	fprintf(stream, "DATA=END\n");

	/* Wrap up reporting. */
	if (f != NULL)
		f(toc->name, dstuff.fcnt);

	return (ret);
}

/*
 * __wt_dump_page --
 *	Depth-first recursive walk of a btree.
 */
static int
__wt_dump_page(WT_TOC *toc, WT_PAGE *page, void *arg)
{
	DB *db;
	WT_DSTUFF *dp;

	db = toc->db;
	dp = arg;

	switch (page->dsk->type) {
	case WT_PAGE_COL_INT:
	case WT_PAGE_DUP_INT:
	case WT_PAGE_ROW_INT:
		break;
	case WT_PAGE_COL_FIX:
		__wt_dump_page_col_fix(toc, page, dp);
		break;
	case WT_PAGE_COL_RLE:
		WT_RET(__wt_dump_page_col_rle(toc, page, dp));
		break;
	case WT_PAGE_COL_VAR:
		WT_RET(__wt_dump_page_col_var(toc, page, dp));
		break;
	case WT_PAGE_DUP_LEAF:
		WT_RET(__wt_dump_page_dup_leaf(toc, page, dp));
		break;
	case WT_PAGE_ROW_LEAF:
		WT_RET(__wt_dump_page_row_leaf(toc, page, dp));
		break;
	WT_ILLEGAL_FORMAT(db);
	}

	/* Report progress every 10 pages. */
	if (dp->f != NULL && ++dp->fcnt % 10 == 0)
		dp->f(toc->name, dp->fcnt);

	return (0);
}

/*
 * __wt_dump_page_col_fix --
 *	Dump a WT_PAGE_COL_FIX page.
 */
static void
__wt_dump_page_col_fix(WT_TOC *toc, WT_PAGE *page, WT_DSTUFF *dp)
{
	DB *db;
	WT_COL *cip;
	WT_REPL *repl;
	uint32_t i;

	db = toc->db;

	/* Walk the page, dumping data items. */
	WT_INDX_FOREACH(page, cip, i) {
		if ((repl = WT_COL_REPL(page, cip)) == NULL) {
			if (!WT_FIX_DELETE_ISSET(cip->data))
				dp->p(cip->data, db->fixed_len, dp->stream);
		} else
			if (!WT_REPL_DELETED_ISSET(repl))
				dp->p(WT_REPL_DATA(repl),
				    db->fixed_len, dp->stream);
	}
}

/*
 * __wt_dump_page_col_rle --
 *	Dump a WT_PAGE_COL_RLE page.
 */
static int
__wt_dump_page_col_rle(WT_TOC *toc, WT_PAGE *page, WT_DSTUFF *dp)
{
	DB *db;
	ENV *env;
	WT_COL *cip;
	WT_RLE_EXPAND *exp, **expsort, **expp;
	WT_REPL *repl;
	uint64_t recno;
	uint32_t i, n_expsort;
	uint16_t n_repeat;

	db = toc->db;
	env = toc->env;
	expsort = NULL;
	n_expsort = 0;

	recno = page->dsk->start_recno;
	WT_INDX_FOREACH(page, cip, i) {
		/*
		 * Get a sorted list of any expansion entries we've created for
		 * this set of records.  The sort function returns a NULL-
		 * terminated array of references to WT_RLE_EXPAND structures,
		 * sorted by record number.
		 */
		WT_RET(__wt_rle_expand_sort(
		    env, page, cip, &expsort, &n_expsort));

		/*
		 * Dump the records.   We use the WT_REPL entry for records in
		 * in the WT_RLE_EXPAND array, and original data otherwise.
		 */
		for (expp = expsort,
		    n_repeat = WT_RLE_REPEAT_COUNT(cip->data);
		    n_repeat > 0; --n_repeat, ++recno)
			if ((exp = *expp) != NULL && exp->recno == recno) {
				++expp;
				repl = exp->repl;
				if (WT_REPL_DELETED_ISSET(repl))
					continue;
				dp->p(
				    WT_REPL_DATA(repl), repl->size, dp->stream);
			} else
				dp->p(WT_RLE_REPEAT_DATA(cip->data),
				    db->fixed_len, dp->stream);
	}
	/* Free the sort array. */
	if (expsort != NULL)
		__wt_free(env, expsort, n_expsort * sizeof(WT_RLE_EXPAND *));

	return (0);
}

/*
 * __wt_dump_page_col_var --
 *	Dump a WT_PAGE_COL_VAR page.
 */
static int
__wt_dump_page_col_var(WT_TOC *toc, WT_PAGE *page, WT_DSTUFF *dp)
{
	DB *db;
	DBT *tmp;
	WT_COL *cip;
	WT_ITEM *item;
	WT_REPL *repl;
	uint32_t i;
	int ret;
	void *huffman;

	db = toc->db;
	huffman = db->idb->huffman_data;
	ret = 0;

	WT_RET(__wt_scr_alloc(toc, 0, &tmp));
	WT_INDX_FOREACH(page, cip, i) {
		/* Check for replace or deletion. */
		if ((repl = WT_COL_REPL(page, cip)) != NULL) {
			if (!WT_REPL_DELETED_ISSET(repl))
				dp->p(
				    WT_REPL_DATA(repl), repl->size, dp->stream);
			continue;
		}

		/* Process the original data. */
		item = cip->data;
		switch (WT_ITEM_TYPE(item)) {
		case WT_ITEM_DATA:
			if (huffman == NULL) {
				dp->p(WT_ITEM_BYTE(item),
				    WT_ITEM_LEN(item), dp->stream);
				break;
			}
			/* FALLTHROUGH */
		case WT_ITEM_DATA_OVFL:
			WT_ERR(__wt_item_process(toc, item, tmp));
			dp->p(tmp->data, tmp->size, dp->stream);
			break;
		case WT_ITEM_DEL:
			break;
		WT_ILLEGAL_FORMAT_ERR(db, ret);
		}
	}

err:	__wt_scr_release(&tmp);
	return (ret);
}

/*
 * __wt_dump_page_dup_leaf --
 *	Dump a WT_PAGE_DUP_LEAF page.
 */
static int
__wt_dump_page_dup_leaf(WT_TOC *toc, WT_PAGE *page, WT_DSTUFF *dp)
{
	DB *db;
	DBT *dupkey, *tmp;
	WT_ITEM *item;
	WT_REPL *repl;
	WT_ROW *rip;
	uint32_t i;
	int ret;
	void *huffman;

	db = toc->db;
	dupkey = dp->dupkey;
	huffman = db->idb->huffman_data;
	ret = 0;

	WT_ERR(__wt_scr_alloc(toc, 0, &tmp));
	WT_INDX_FOREACH(page, rip, i) {
		/* Check for deletion. */
		if ((repl = WT_ROW_REPL(
		    page, rip)) != NULL && WT_REPL_DELETED_ISSET(repl))
			continue;

		/* Output the key, we're going to need it. */
		dp->p(dupkey->data, dupkey->size, dp->stream);

		/* Output the replacement item. */
		if (repl != NULL) {
			dp->p(WT_REPL_DATA(repl), repl->size, dp->stream);
			continue;
		}

		/* Process the original data. */
		item = rip->data;
		switch (WT_ITEM_TYPE(item)) {
		case WT_ITEM_DATA_DUP:
			if (huffman == NULL) {
				dp->p(WT_ITEM_BYTE(item),
				    WT_ITEM_LEN(item), dp->stream);
				break;
			}
			/* FALLTHROUGH */
		case WT_ITEM_DATA_DUP_OVFL:
			WT_ERR(__wt_item_process(toc, item, tmp));
			dp->p(tmp->data, tmp->size, dp->stream);
			break;
		WT_ILLEGAL_FORMAT_ERR(db, ret);
		}
	}

err:	__wt_scr_release(&tmp);
	return (ret);
}

/*
 * __wt_dump_page_row_leaf --
 *	Dump a WT_PAGE_ROW_LEAF page.
 */
static int
__wt_dump_page_row_leaf(WT_TOC *toc, WT_PAGE *page, WT_DSTUFF *dp)
{
	DB *db;
	DBT *key, *data, *key_tmp, *data_tmp, key_local, data_local;
	WT_ITEM *item;
	WT_OFF_RECORD *off_record;
	WT_REF *ref;
	WT_REPL *repl;
	WT_ROW *rip;
	uint32_t i;
	int ret;
	void *huffman;

	db = toc->db;
	key = data = key_tmp = data_tmp = NULL;
	huffman = db->idb->huffman_data;
	ret = 0;

	WT_ERR(__wt_scr_alloc(toc, 0, &key_tmp));
	WT_ERR(__wt_scr_alloc(toc, 0, &data_tmp));
	WT_CLEAR(key_local);
	WT_CLEAR(data_local);

	WT_INDX_FOREACH(page, rip, i) {
		/* Check for deletion. */
		if ((repl = WT_ROW_REPL(
		    page, rip)) != NULL && WT_REPL_DELETED_ISSET(repl))
			continue;

		/*
		 * The key and data variables reference the DBT's we'll print.
		 * Set the key.
		 */
		if (__wt_key_process(rip)) {
			WT_ERR(__wt_item_process(toc, rip->key, key_tmp));
			key = key_tmp;
		} else
			key = (DBT *)rip;

		/*
		 * If the item was ever replaced, we're done: it can't be an
		 * off-page tree, and we don't care what kind of item it was
		 * originally.  Dump the data from the replacement entry.
		 *
		 * XXX
		 * This is wrong -- if an off-page dup tree is reconciled,
		 * the off-page reference will change underfoot.
		 */
		if (repl != NULL) {
			dp->p(key->data, key->size, dp->stream);
			dp->p(WT_REPL_DATA(repl), repl->size, dp->stream);
			continue;
		}

		/* Set data to reference the data we'll dump. */
		item = rip->data;
		switch (WT_ITEM_TYPE(item)) {
		case WT_ITEM_DATA:
		case WT_ITEM_DATA_DUP:
			if (huffman == NULL) {
				data_local.data = WT_ITEM_BYTE(item);
				data_local.size = WT_ITEM_LEN(item);
				data = &data_local;
				break;
			}
			/* FALLTHROUGH */
		case WT_ITEM_DATA_DUP_OVFL:
		case WT_ITEM_DATA_OVFL:
			WT_ERR(__wt_item_process(toc, item, data_tmp));
			data = data_tmp;
			break;
		case WT_ITEM_OFF_RECORD:
			/*
			 * Set the key and recursively call the tree-walk code
			 * for any off-page duplicate trees.  (Check for any
			 * off-page duplicate trees locally because we already
			 * have to walk the page, so it's faster than walking
			 * the page both here and in the tree-walk function.)
			 */
			dp->dupkey = key;

			ref = WT_ROW_DUP(page, rip);
			off_record = WT_ROW_OFF_RECORD(rip);
			WT_RET(__wt_page_in(toc, page, ref, off_record, 0));
			ret = __wt_tree_walk(toc, ref, 0, __wt_dump_page, dp);
			__wt_hazard_clear(toc, ref->page);
			if (ret != 0)
				goto err;
			continue;
		WT_ILLEGAL_FORMAT_ERR(db, ret);
		}

		dp->p(key->data, key->size, dp->stream);
		dp->p(data->data, data->size, dp->stream);
	}

err:	/* Discard any space allocated to hold off-page key/data items. */
	if (key_tmp != NULL)
		__wt_scr_release(&key_tmp);
	if (data_tmp != NULL)
		__wt_scr_release(&data_tmp);

	return (ret);
}

static const char hex[] = "0123456789abcdef";

/*
 * __wt_print_byte_string_nl --
 *	Output a single byte stringin printable characters, where possible.
 *	In addition, terminate with a <newline> character, unless the entry
 *	is itself terminated with a <newline> character.
 */
static void
__wt_print_byte_string_nl(uint8_t *data, uint32_t size, FILE *stream)
{
	if (data[size - 1] == '\n')
		--size;
	__wt_print_byte_string(data, size, stream);
	fprintf(stream, "\n");
}

/*
 * __wt_print_byte_string --
 *	Output a single byte string in printable characters, where possible.
 */
void
__wt_print_byte_string(uint8_t *data, uint32_t size, FILE *stream)
{
	int ch;

	for (; size > 0; --size, ++data) {
		ch = data[0];
		if (isprint(ch))
			fprintf(stream, "%c", ch);
		else
			fprintf(stream, "%x%x",
			    hex[(data[0] & 0xf0) >> 4], hex[data[0] & 0x0f]);
	}
}

/*
 * __wt_print_byte_string_hex --
 *	Output a single byte string in hexadecimal characters.
 */
static void
__wt_print_byte_string_hex(uint8_t *data, uint32_t size, FILE *stream)
{
	for (; size > 0; --size, ++data)
		fprintf(stream, "%x%x",
		    hex[(data[0] & 0xf0) >> 4], hex[data[0] & 0x0f]);
	fprintf(stream, "\n");
}