forked from yuanrongxi/innodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btr0pcur.inl
341 lines (259 loc) · 9.08 KB
/
btr0pcur.inl
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
/*获得pcursor的rel_pos*/
UNIV_INLINE ulint btr_pcur_get_rel_pos(btr_pcur_t* cursor)
{
ut_ad(cursor);
ut_ad(cursor->old_rec);
ut_ad(cursor->old_stored == BTR_PCUR_OLD_STORED);
ut_ad(cursor->pos_state == BTR_PCUR_WAS_POSITIONED || cursor->pos_state == BTR_PCUR_IS_POSITIONED);
return cursor->rel_pos;
}
/*设置mtr*/
UNIV_INLINE void btr_pcur_set_mtr(btr_pcur_t* cursor, mtr_t* mtr)
{
ut_ad(cursor);
cursor->mtr = mtr;
}
/*获取mtr*/
UNIV_INLINE mtr_t* btr_pcur_get_mtr(btr_pcur_t* cursor)
{
ut_ad(cursor);
return cursor->mtr;
}
/*获得btree cursor*/
UNIV_INLINE btr_cur_t* btr_pcur_get_btr_cur(btr_pcur_t* cursor)
{
return &(cursor->btr_cur);
}
/*获得pcursor对应的page cursor*/
UNIV_INLINE page_cur_t* btr_pcur_get_page_cur(btr_pcur_t* cursor)
{
return btr_cur_get_page_cur(&(cursor->btr_cur));
}
/*获得pcursor对应的page*/
UNIV_INLINE page_t* btr_pcur_get_page(btr_pcur_t* cursor)
{
ut_ad(cursor->pos_state == BTR_PCUR_IS_POSITIONED);
return page_cur_get_page(btr_pcur_get_page_cur(cursor));
}
/*获得pcursor对应的记录*/
UNIV_INLINE rec_t* btr_pcur_get_rec(btr_pcur_t* cursor, mtr_t* mtr)
{
ut_ad(cursor->pos_state == BTR_PCUR_IS_POSITIONED);
ut_ad(cursor->latch_mode != BTR_NO_LATCHES);
return page_cur_get_rec(btr_pcur_get_page_cur(cursor));
}
/*获得pcursor的up_match*/
UNIV_INLINE ulint btr_pcur_get_up_match(btr_pcur_t* cursor)
{
btr_cur_t* btr_cursor;
ut_ad((cursor->pos_state == BTR_PCUR_WAS_POSITIONED) || (cursor->pos_state == BTR_PCUR_IS_POSITIONED));
btr_cursor = btr_pcur_get_btr_cur(cursor);
ut_ad(btr_cursor->up_match != ULINT_UNDEFINED);
return btr_cursor->up_match;
}
/*获得pcursor的low_match*/
UNIV_INLINE ulint btr_pcur_get_low_match(btr_pcur_t* cursor)
{
btr_cur_t* btr_cursor;
ut_ad((cursor->pos_state == BTR_PCUR_WAS_POSITIONED) || (cursor->pos_state == BTR_PCUR_IS_POSITIONED));
btr_cursor = btr_pcur_get_btr_cur(cursor);
return btr_cursor->low_match;
}
/*判断pcursor是否指向对应的页的最后一条记录(supremum)*/
UNIV_INLINE ibool btr_pcur_is_after_last_on_page(btr_pcur_t* cursor, mtr_t* mtr)
{
UT_NOT_USED(mtr);
ut_ad(cursor->pos_state == BTR_PCUR_IS_POSITIONED);
ut_ad(cursor->latch_mode != BTR_NO_LATCHES);
return page_cur_is_after_last(btr_pcur_get_page_cur(cursor));
}
/*判断pcursor是否指向对应的页的第一条记录(Infimum)*/
UNIV_INLINE ibool btr_pcur_is_before_first_on_page(btr_pcur_t* cursor, mtr_t* mtr)
{
UT_NOT_USED(mtr);
ut_ad(cursor->pos_state == BTR_PCUR_IS_POSITIONED);
ut_ad(cursor->latch_mode != BTR_NO_LATCHES);
return page_cur_is_before_first(btr_pcur_get_page_cur(cursor));
}
/*pcursor指向对应page的有效记录上,除infimum和supremum之外的记录*/
UNIV_INLINE ibool btr_pcur_is_on_user_rec(btr_pcur_t* cursor, mtr_t* mtr)
{
ut_ad(cursor->pos_state == BTR_PCUR_IS_POSITIONED);
ut_ad(cursor->latch_mode != BTR_NO_LATCHES);
if ((btr_pcur_is_before_first_on_page(cursor, mtr))
|| (btr_pcur_is_after_last_on_page(cursor, mtr)))
return FALSE;
return TRUE;
}
/*判断cursor是否指向的是BTree中所在层的最前面一条记录上*/
UNIV_INLINE ibool btr_pcur_is_before_first_in_tree(btr_pcur_t* cursor, mtr_t* mtr)
{
ut_ad(cursor->pos_state == BTR_PCUR_IS_POSITIONED);
ut_ad(cursor->latch_mode != BTR_NO_LATCHES);
if(btr_page_get_prev(btr_pcur_get_page(cursor), mtr) != FIL_NULL)
return FALSE;
return page_cur_is_before_first(btr_pcur_get_page_cur(cursor));
}
/*判断cursor是否指向的是BTree中所在层的最后面一条记录上*/
UNIV_INLINE ibool btr_pcur_is_after_last_in_tree(btr_pcur_t* cursor, mtr_t* mtr)
{
ut_ad(cursor->pos_state == BTR_PCUR_IS_POSITIONED);
ut_ad(cursor->latch_mode != BTR_NO_LATCHES);
if (btr_page_get_next(btr_pcur_get_page(cursor), mtr) != FIL_NULL)
return FALSE;
return page_cur_is_after_last(btr_pcur_get_page_cur(cursor));
}
/*pcursor向下一条记录移动,并指向下一条记录*/
UNIV_INLINE void btr_pcur_move_to_next_on_page(btr_pcur_t* cursor, mtr_t* mtr)
{
UT_NOT_USED(mtr);
ut_ad(cursor->pos_state == BTR_PCUR_IS_POSITIONED);
ut_ad(cursor->latch_mode != BTR_NO_LATCHES);
page_cur_move_to_next(btr_pcur_get_page_cur(cursor));
cursor->old_stored = BTR_PCUR_OLD_NOT_STORED;
}
/*pcursor向上一条记录移动,并指向上一条记录*/
UNIV_INLINE void btr_pcur_move_to_prev_on_page(btr_pcur_t* cursor, mtr_t* mtr)
{
UT_NOT_USED(mtr);
ut_ad(cursor->pos_state == BTR_PCUR_IS_POSITIONED);
ut_ad(cursor->latch_mode != BTR_NO_LATCHES);
page_cur_move_to_prev(btr_pcur_get_page_cur(cursor));
cursor->old_stored = BTR_PCUR_OLD_NOT_STORED;
}
/*将pcursor移动到下一条记录上,可以夸页,不会指向伪记录(infimum或者supremum)*/
UNIV_INLINE ibool btr_pcur_move_to_next_user_rec(btr_pcur_t* cursor, mtr_t* mtr)
{
ut_ad(cursor->pos_state == BTR_PCUR_IS_POSITIONED);
ut_ad(cursor->latch_mode != BTR_NO_LATCHES);
cursor->old_stored = BTR_PCUR_OLD_NOT_STORED;
loop:
if(btr_pcur_is_after_last_on_page(cursor, mtr)){
if(btr_pcur_is_after_last_in_tree(cursor, mtr)) /*已经到btree cursor指向节点所在层的最后一条记录,不能向后移动了*/
return FALSE;
/*将pcursor移动到下一页上*/
btr_pcur_move_to_next_page(cursor, mtr);
}
else
btr_pcur_move_to_next_on_page(cursor, mtr);
/*判断cursor指向的记录是否是有效记录,如果不是有效记录,就需要移动*/
if(btr_pcur_is_on_user_rec(cursor, mtr))
return TRUE;
goto loop;
}
/*将pcursor移动到下一条记录上,可以夸页,会指向伪记录(infimum或者supremum)*/
UNIV_INLINE ibool btr_pcur_move_to_next(btr_pcur_t* cursor, mtr_t* mtr)
{
ut_ad(cursor->pos_state == BTR_PCUR_IS_POSITIONED);
ut_ad(cursor->latch_mode != BTR_NO_LATCHES);
cursor->old_stored = BTR_PCUR_OLD_NOT_STORED;
if(btr_pcur_is_after_last_on_page(cursor, mtr)){
if(btr_pcur_is_before_first_in_tree(cursor, mtr)) /*已经到末尾了*/
return FALSE;
/*直接移向下一页的infimum上*/
btr_pcur_move_to_next_page(cursor, mtr);
return TRUE;
}
btr_pcur_move_to_next_on_page(cursor, mtr);
return TRUE;
}
UNIV_INLINE void btr_pcur_commit(btr_pcur_t* pcur)
{
ut_a(pcur->pos_state == BTR_PCUR_IS_POSITIONED);
pcur->latch_mode = BTR_NO_LATCHES;
mtr_commit(pcur->mtr);
pcur->pos_state = BTR_PCUR_WAS_POSITIONED;
}
/*与btr_pcur_commit功能相似,唯一不同的是可以指定mtr*/
UNIV_INLINE void btr_pcur_commit_specify_mtr(btr_pcur_t* pcur, mtr_t* mtr)
{
ut_a(pcur->pos_state == BTR_PCUR_IS_POSITIONED);
pcur->latch_mode = BTR_NO_LATCHES;
mtr_commit(mtr);
pcur->pos_state = BTR_PCUR_WAS_POSITIONED;
}
UNIV_INLINE void btr_pcur_detach(btr_pcur_t* pcur)
{
ut_a(pcur->pos_state == BTR_PCUR_IS_POSITIONED);
pcur->latch_mode = BTR_NO_LATCHES;
pcur->pos_state = BTR_PCUR_WAS_POSITIONED;
}
/*判断latch mode是否已经detach*/
UNIV_INLINE ibool btr_pcur_is_detached(btr_pcur_t* pcur)
{
if(pcur->latch_mode == BTR_NO_LATCHES)
return TRUE;
return FALSE;
}
/*初始化一个pcursor*/
UNIV_INLINE void btr_pcur_init(btr_pcur_t* pcur)
{
pcur->old_stored = BTR_PCUR_OLD_NOT_STORED;
pcur->old_rec_buf = NULL;
pcur->old_rec = NULL;
}
/*构建一个pcursor*/
UNIV_INLINE void btr_pcur_open(dict_index_t* index, dtuple_t* tuple, ulint mode,
ulint latch_mode, btr_pcur_t* cursor, mtr_t* mtr)
{
btr_cur_t* btr_cursor;
btr_pcur_init(cursor);
cursor->latch_mode = latch_mode;
cursor->search_mode = mode;
btr_cursor = btr_pcur_get_btr_cur(cursor);
/*定位到tuple的ROW KEY指向的btree cursor*/
btr_cur_search_to_nth_level(index, 0, tuple, mode, latch_mode, btr_cursor, 0, mtr);
cursor->pos_state = BTR_PCUR_IS_POSITIONED;
}
/*不需要进行初始化pcursor,构建一个pcursor,指向tuple在BTREE上的位置*/
UNIV_INLINE void btr_pcur_open_with_no_init(dict_index_t* index, dtuple_t* tuple, ulint mode, ulint latch_mode,
btr_pcur_t* cursor, ulint has_search_latch, mtr_t* mtr)
{
btr_cur_t* btr_cursor;
cursor->latch_mode = latch_mode;
cursor->search_mode = mode;
btr_cursor = btr_pcur_get_btr_cur(cursor);
btr_cur_search_to_nth_level(index, 0, tuple, mode, latch_mode, btr_cursor, has_search_latch, mtr);
cursor->pos_state = BTR_PCUR_IS_POSITIONED;
cursor->old_stored = BTR_PCUR_OLD_NOT_STORED;
}
UNIV_INLINE void btr_pcur_open_at_index_side(ibool from_left, dict_index_t* index, ulint latch_mode,
btr_pcur_t* pcur, ibool do_init, mtr_t* mtr)
{
pcur->latch_mode = latch_mode;
if(from_left)
pcur->search_mode = PAGE_CUR_G; /*大于*/
else
pcur->search_mode = PAGE_CUR_L; /*小于*/
if(do_init)
btr_pcur_init(pcur);
/*根据搜索方向定位对应btree cursor指向的位置*/
btr_cur_open_at_index_side(from_left, index, latch_mode, btr_pcur_get_btr_cur(pcur), mtr);
pcur->pos_state = BTR_PCUR_IS_POSITIONED;
pcur->old_stored = BTR_PCUR_OLD_NOT_STORED;
}
/*将pcursor随机指向btree上的一个位置*/
UNIV_INLINE void btr_pcur_open_at_rnd_pos(dict_index_t* index, ulint latch_mode, btr_pcur_t* cursor, mtr_t* mtr)
{
cursor->latch_mode = latch_mode;
cursor->search_mode = PAGE_CUR_G;
btr_pcur_init(cursor);
/*随机定位到btree上的一个位置*/
btr_cur_open_at_rnd_pos(index, latch_mode, btr_pcur_get_btr_cur(cursor), mtr);
cursor->pos_state = BTR_PCUR_IS_POSITIONED;
cursor->old_stored = BTR_PCUR_OLD_STORED;
}
/*关闭pcursor*/
UNIV_INLINE void btr_pcur_close(btr_pcur_t* cursor)
{
if(cursor->old_rec_buf != NULL){
mem_free(cursor->old_rec_buf);
cursor->old_rec = NULL;
cursor->old_rec_buf = NULL;
}
cursor->btr_cur.page_cur.rec = NULL;
cursor->old_rec = NULL;
cursor->old_stored = BTR_PCUR_OLD_NOT_STORED;
cursor->latch_mode = BTR_NO_LATCHES;
cursor->pos_state = BTR_PCUR_NOT_POSITIONED;
}