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
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2008-2011 WiredTiger, Inc.
* All rights reserved.
*/
#include "wt_internal.h"
/*
* There are two tree-walk implementations: a textbook, depth-first recursive
* tree walk in __wt_tree_walk(), and a non-recursive, depth-first tree walk
* in __wt_walk_{begin,end,next}().
*
* The simple recursive walk is sufficient in most cases -- a hazard reference
* is obtained on each page in turn, a worker function is called on the page,
* then the hazard reference is released.
*
* The complicated tree walk routine was added because the cache eviction code
* needs:
* + to walk the tree a few pages at a time, that is, periodically wake,
* visit some pages, then go back to sleep, which requires enough state
* to restart the traversal at any point,
* + to only visit pages that currently appear in the cache,
* + to return the WT_REF structure (not the WT_PAGE structure),
* + to walk files not associated with the current SESSION's BTREE handle,
* + and finally, it doesn't require a hazard reference.
*
* My guess is we'll generalize a more complicated walk at some point, which
* means some or all of those behaviors will become configurable, and that's
* why the code lives here instead of in the eviction code.
*/
/*
* __wt_tree_walk --
* Depth-first recursive walk of a btree, calling a worker function on
* each page.
*/
int
__wt_tree_walk(SESSION *session, WT_PAGE *page,
uint32_t flags, int (*work)(SESSION *, WT_PAGE *, void *), void *arg)
{
BTREE *btree;
WT_COL_REF *cref;
WT_ROW_REF *rref;
WT_REF *ref;
uint32_t i;
int ret;
WT_CONN_FCHK(
S2C(session), "__wt_tree_walk", flags, WT_APIMASK_BT_TREE_WALK);
btree = session->btree;
/* A NULL page starts at the top of the tree -- it's a convenience. */
if (page == NULL)
page = btree->root_page.page;
/* Walk internal pages, descending through any off-page references. */
switch (page->dsk->type) {
case WT_PAGE_COL_INT:
WT_COL_REF_FOREACH(page, cref, i) {
/* cref references the subtree containing the record */
switch (WT_COL_REF_STATE(cref)) {
case WT_REF_CACHE:
break;
case WT_REF_DELETED:
continue;
case WT_REF_DISK:
case WT_REF_EVICT:
if (LF_ISSET(WT_WALK_CACHE))
continue;
break;
}
ref = &cref->ref;
switch (ret = __wt_page_in(session, page, ref, 0)) {
case 0: /* Valid page */
ret = __wt_tree_walk(
session, ref->page, flags, work, arg);
__wt_hazard_clear(session, ref->page);
break;
case WT_PAGE_DELETED:
ret = 0; /* Skip deleted pages */
break;
}
if (ret != 0)
return (ret);
}
break;
case WT_PAGE_ROW_INT:
WT_ROW_REF_FOREACH(page, rref, i) {
/* rref references the subtree containing the record */
switch (WT_ROW_REF_STATE(rref)) {
case WT_REF_CACHE:
break;
case WT_REF_DELETED:
continue;
case WT_REF_DISK:
case WT_REF_EVICT:
if (LF_ISSET(WT_WALK_CACHE))
continue;
break;
}
ref = &rref->ref;
switch (ret = __wt_page_in(session, page, ref, 0)) {
case 0: /* Valid page */
ret = __wt_tree_walk(
session, ref->page, flags, work, arg);
__wt_hazard_clear(session, ref->page);
break;
case WT_PAGE_DELETED:
ret = 0; /* Skip deleted pages */
break;
}
if (ret != 0)
return (ret);
}
break;
}
/*
* Don't call the worker function for any page until all of its children
* have been visited. This allows the walker function to be used for
* the close/sync methods, where reconciling a modified child page will
* modify its parent.
*/
WT_RET(work(session, page, arg));
return (0);
}
/*
* __wt_walk_begin --
* Start a tree walk.
*/
int
__wt_walk_begin(SESSION *session, WT_REF *ref, WT_WALK *walk)
{
/*
* The caller may be restarting a walk, so the structure may already
* be allocated. Allocate 20 slots: it's always going to be enough.
*/
if (walk->tree_len == 0)
WT_RET(__wt_realloc(session, &walk->tree_len,
20 * sizeof(WT_WALK_ENTRY), &walk->tree));
walk->tree_slot = 0;
walk->tree[0].ref = ref;
walk->tree[0].indx = 0;
walk->tree[0].visited = 0;
return (0);
}
/*
* __wt_walk_end --
* End a tree walk.
*/
void
__wt_walk_end(SESSION *session, WT_WALK *walk)
{
__wt_free(session, walk->tree, walk->tree_len);
}
/*
* __wt_walk_next --
* Return the next WT_REF/WT_PAGE in the tree, in a non-recursive way.
*/
int
__wt_walk_next(SESSION *session, WT_WALK *walk, uint32_t flags, WT_REF **refp)
{
WT_COL_REF *cref;
WT_PAGE *page;
WT_REF *ref;
WT_ROW_REF *rref;
WT_WALK_ENTRY *e;
u_int elem;
int ret;
e = &walk->tree[walk->tree_slot];
page = e->ref->page;
/*
* Coming into this function we have a tree internal page and we're
* walking the array of children.
*
* If we've reached the end of this page, and haven't yet returned it,
* do that now. If the page has been returned, traversal is finished:
* pop the stack and call ourselve recursively, unless the entire tree
* has been traversed, in which case we return NULL.
*/
if (e->visited) {
if (walk->tree_slot == 0) {
*refp = NULL;
return (0);
} else {
--walk->tree_slot;
return (__wt_walk_next(session, walk, flags, refp));
}
} else
if (e->indx == page->indx_count) {
eop: e->visited = 1;
*refp = e->ref;
return (0);
}
/*
* Check to see if the page has sub-trees associated with it, in which
* case we traverse those pages.
*/
switch (page->dsk->type) {
case WT_PAGE_COL_INT:
/* Find the next subtree present in the cache. */
for (;;) {
cref = &page->u.col_int.t[e->indx];
ref = &cref->ref;
/* We only care about pages in the cache. */
if (ref->state == WT_REF_CACHE)
break;
else if (ref->state != WT_REF_DELETED &&
!LF_ISSET(WT_WALK_CACHE)) {
if ((ret = __wt_page_in(session, page, ref, 0)) == 0)
break; /* Valid page */
else if (ret == WT_PAGE_DELETED)
ret = 0;
else
return (ret);
}
/*
* If we don't find another WT_REF entry, do the
* post-order visit.
*/
if (++e->indx == page->indx_count)
goto eop;
}
break;
case WT_PAGE_ROW_INT:
/* Find the next subtree present in the cache. */
for (;;) {
rref = &page->u.row_int.t[e->indx];
ref = &rref->ref;
/* We only care about pages in the cache. */
if (ref->state == WT_REF_CACHE)
break;
else if (ref->state != WT_REF_DELETED &&
!LF_ISSET(WT_WALK_CACHE)) {
if ((ret = __wt_page_in(session, page, ref, 0)) == 0)
break; /* Valid page */
else if (ret == WT_PAGE_DELETED)
ret = 0;
else
return (ret);
}
/*
* If we don't find another WT_REF entry, do the
* post-order visit.
*/
if (++e->indx == page->indx_count)
goto eop;
}
break;
}
switch (ref->page->dsk->type) {
case WT_PAGE_COL_INT:
case WT_PAGE_ROW_INT:
/* The page has children; first, move past this child. */
++e->indx;
/*
* Check to see if we grew past the end of our stack, then push
* the child onto the stack and recursively descend the tree.
*/
elem = (u_int)(walk->tree_len / WT_SIZEOF32(WT_WALK_ENTRY));
if (walk->tree_slot >= elem)
WT_RET(__wt_realloc(session, &walk->tree_len,
(elem + 20) * sizeof(WT_WALK_ENTRY), &walk->tree));
e = &walk->tree[++walk->tree_slot];
e->ref = ref;
e->indx = 0;
e->visited = 0;
return (__wt_walk_next(session, walk, flags, refp));
}
/* Return the child page, it's not interesting for further traversal. */
++e->indx;
*refp = ref;
return (0);
}
|