-
Notifications
You must be signed in to change notification settings - Fork 29
/
an_block.h
569 lines (495 loc) · 18.1 KB
/
an_block.h
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/*************************************************************************
> File Name: an_block.h
> Author: gaopeng QQ:22389860 all right reserved
> Mail: [email protected]
> Created Time: Thu 28 Sep 2017 04:36:46 PM CST
************************************************************************/
#ifndef _AN_BLK
#define _AN_BLK
#include<iostream>
#include<list>
#include"myfine.h"
#include"mysqldefine.h"
#include"tool.h"
using namespace std;
#endif
int an_block(byte*, size_t);
/***
index struct support
**/
struct indbinfo
{
ulint offset;//本块块号
ulint next_offset; //下一个块的块号
ulint prvs_offset; //上一个块的块号
ulint level;//本块level
};
struct indexinfo
{
ulint index_id;
list<indbinfo> level0;
list<indbinfo> level1;
list<indbinfo> level2;
list<indbinfo> level3;
list<indbinfo> level4;
list<indbinfo> level5;
~indexinfo()
{
level0.clear();
level1.clear();
level2.clear();
level3.clear();
level4.clear();
level5.clear();
}
};
class index_scan_abc
{
public:
virtual int scan_head(FILE* fd,size_t bsize) = 0;
virtual int printindv(int i) = 0;
virtual int scan_sort() = 0;
virtual int check_file(FILE* fd,size_t bsize) = 0;
private:
virtual ulint get_size(FILE* fd) = 0;
//virtual void level_add(indexinfo* index,indbinfo block) = 0;
//virtual void index_add(indexinfo index) = 0;
};
class index_scan:public index_scan_abc
{
private:
list<indexinfo> indexchain;
list<indexinfo> sort_indexchain;
ulint total_size;
public:
~index_scan()
{
indexchain.clear();
sort_indexchain.clear();
}
int check_file(FILE *fd,size_t bsize)
{
int ret = 0;
ulint file_space_id = 0;
ulint fsp_space_id = 0;
ulint h_chksum = 0;
ulint f_chksum = 0;
byte* frame;
if(!(frame = (byte*)aligned_malloc(bsize,bsize))) //对其blocksize分配内存
{
ERRPRINT("aligned_malloc error:");
ret = -1;
return ret;
}
if( fread(frame, bsize, 1, fd) == 0)
{
perror("fread error:");
ERRPRINT("fread error:");
ret = -1;
return ret;
}
frame = (byte*)ut_align_down(frame,bsize);
file_space_id = mach_read_from_4(frame+FIL_PAGE_SPACE_ID);
fsp_space_id = mach_read_from_4(frame+FSP_SPACE_ID+FIL_PAGE_DATA);
h_chksum = mach_read_from_4(frame+FIL_PAGE_SPACE_OR_CHKSUM);
f_chksum = mach_read_from_4(frame+bsize-FAIL_CHK);
#ifdef DEBUG
cout<<"file_space_id:" <<file_space_id <<" fsp_space_id:"<<fsp_space_id <<endl;
cout<<"h_chksum:" <<h_chksum <<" f_chksum:"<<f_chksum<<endl;
#endif
//|| h_chksum != f_chksum 5.6.25 BUG or h_chksum != f_chksum
if(fsp_space_id != file_space_id )
{
ERRPRINT("innodb file check filed fsp_space_id != file_space_id \n");
ret = -1;
return ret;
}
fseek(fd,0,SEEK_SET);
aligned_free(frame);
return ret;
}
void index_add(indexinfo index) //将indexinfo对象将入到一级链表中
{
indexchain.push_back(index);
}
void level_add(indexinfo& index,indbinfo block)//将合适的level 加入到二级 链表中 引用是必须的 第二个参数只能为变量
{
if(block.level == 0)
{
index.level0.push_back(block);
}
if(block.level == 1)
{
index.level1.push_back(block);
}
if(block.level == 2)
{
index.level2.push_back(block);
}
if(block.level == 3)
{
index.level3.push_back(block);
}
if(block.level == 4)
{
index.level4.push_back(block);
}
if(block.level == 5)
{
index.level5.push_back(block);
}
}
ulint get_size(FILE* fd)
{
fseek(fd,0,SEEK_END);
ulint total_size = ftell(fd);
fseek(fd,0,SEEK_SET);
return total_size;
}
/*-1 no offset=next offset error */
int sort_index(list<indbinfo>& sour_level,list<indbinfo>& dest_level)
{
list<indbinfo>::iterator indx_level;
indbinfo blockinfo;
ulint temp_offset = 0;
while(sour_level.size() !=0)
{
indx_level =sour_level.begin(); //每次循环都必须将指针移动到头部
for(;indx_level!=sour_level.end();++indx_level )//进行循环,每次循环都会去掉sour中的一个元素
{
if(indx_level->next_offset == FIL_NULL)
{
#ifdef DEBUG
printf("find last block\n");
#endif
temp_offset = indx_level->prvs_offset; //获取最后的下一个块地址 5.6单链表?
blockinfo = *indx_level; //完成复制到blockinfo
dest_level.push_front(blockinfo);//在tempinfo的leveln链表中加入这个元素 末尾扫描头插
sour_level.erase(indx_level);//在原有leveln中删除这个元素
break;
}
else
{
if(indx_level->offset == temp_offset) //如果不是最后块,但是offset等于上一次取得的next_offset
{
temp_offset = indx_level->prvs_offset;//获取元素的下一个块地址
blockinfo = *indx_level;//完成复制到blockinfo
dest_level.push_front(blockinfo);//在tempinfo的leveln链表中加入这个元素 末尾扫描头插
sour_level.erase(indx_level);//在原有leveln中删除这个元素
break;
}
else
{
ERRPRINT("sort_index err no offset find: %lu\n",temp_offset);
return -1;
}
}
}
}
return 0;
}
public:
/* -1 error 进程排序并且在indexchain->indexinfo 中删除相应的level信息*/
int scan_sort()
{
list<indexinfo>::iterator indx;
for(indx = indexchain.begin(); indx != indexchain.end(); ++indx)//顺序方位各个索引indexinfo
{
indexinfo tempinfo; //建立临时indexinfo
tempinfo.index_id = indx->index_id; //获取index_id
//插入一级索引id链表 下面开始依次处理本索引各个level block
//level 5:
if(sort_index(indx->level5,tempinfo.level5) == -1)
{
return -1;
}
//level 4:
if(sort_index(indx->level4,tempinfo.level4) == -1)
{
return -1;
}
//level 3:
if(sort_index(indx->level3,tempinfo.level3) == -1)
{
return -1;
}
//level 2:
if(sort_index(indx->level2,tempinfo.level2) == -1)
{
return -1;
}
//level 1:
if(sort_index(indx->level1,tempinfo.level1) == -1)
{
return -1;
}
//level 0:
if(sort_index(indx->level0,tempinfo.level0) == -1)
{
return -1;
}
sort_indexchain.push_back(tempinfo);
}
return 0;
}
/* -1 error 扫描一个真个文件将索引信息分类如果需要排序按照offset需要调用 scan_sort()*/
int scan_head(FILE* fd,size_t bsize)
{
list<indexinfo>::iterator it;
ulint total_size = 0;
ulint cur_size = bsize;
ulint cnt = 1;
int ret = 0;
int is_new = 0;
byte* frame = NULL;
total_size = get_size(fd);
setbuf(stdout,NULL);
if(!(frame = (byte*)aligned_malloc(bsize,bsize))) //对其blocksize分配内存
{
ERRPRINT("aligned_malloc error:");
ret = -1;
return ret;
}
printf("Datafile Total Size:%lu\n",total_size);
while(cur_size <= total_size)//第一次循环整个数据文件
{
is_new=0;
#ifdef DEBUG
printf("cnt:%lu bzize:%lu totalsize:%lu cursize:%lu\n",cnt,bsize,total_size,cur_size);
#endif
memset(frame,0,bsize);
if( fread(frame, bsize, 1, fd) == 0)
{
perror("fread error:");
ERRPRINT("fread error:");
ret = -1;
return ret;
}
if(mach_read_from_2( frame+FIL_PAGE_TYPE) == FIL_PAGE_INDEX )//判断是否是index_page
{
frame = (byte*)ut_align_down(frame,bsize);
//if(mach_read_from_4(frame+FIL_PAGE_PREV) == FIL_NULL)//判断是否为某个level的第一个块
//不再进行判断直接全部进入链表,在内存中进行重组避免文件的多次访问
{
indexinfo tempinfo;
indbinfo blockinfo;
frame = (byte*)ut_align_down(frame,bsize);
blockinfo.offset = mach_read_from_4(frame+FIL_PAGE_OFFSET);
frame = (byte*)ut_align_down(frame,bsize);
blockinfo.next_offset = mach_read_from_4(frame+FIL_PAGE_NEXT);
frame = (byte*)ut_align_down(frame,bsize);
blockinfo.prvs_offset = mach_read_from_4(frame+FIL_PAGE_PREV);
frame = (byte*)ut_align_down(frame,bsize);
blockinfo.level = mach_read_from_2(frame+PAGE_HEADER+PAGE_LEVEL);
frame = (byte*)ut_align_down(frame,bsize);
tempinfo.index_id = mach_read_from_8(frame+PAGE_HEADER+PAGE_INDEX_ID);
for(it = indexchain.begin(); it != indexchain.end(); ++it)
{
if(tempinfo.index_id == it->index_id ) //如果找到相同的index_id已经挂载直接
{
is_new = 1;
#ifdef DEBUG
cout<<"hint1"<<endl;
printf("index_id:%lu level:%lu next_offset:%lu offset:%lu\n",tempinfo.index_id,blockinfo.level,blockinfo.next_offset,blockinfo.offset);
#endif
level_add(*it,blockinfo);
}
}
if(is_new == 0) //如果没有找到将tempinfo加入到一级链表
{
#ifdef DEBUG
cout<<"hint2"<<endl;
printf("index_id:%lu level:%lu next_offset:%lu offset:%lu\n",tempinfo.index_id,blockinfo.level,blockinfo.next_offset,blockinfo.offset);
#endif
index_add(tempinfo);
indexinfo& tmp = indexchain.back();
level_add(tmp,blockinfo); //将头块加入二级链表
}
}
}
cnt++;
cur_size = bsize*cnt;
}
fseek(fd,0,SEEK_SET);
aligned_free(frame);
return 0;
}
void formatprint(list<indbinfo>& level)
{
ulint cnt=0;
list<indbinfo>::iterator indx_level;
for(indx_level =level.begin();indx_level!=level.end();++indx_level )
{
printf("block_no:%10lu,level:%4lu|*|",indx_level->offset,indx_level->level);
cnt++;
if(cnt%3 == 0)
{
printf("\n");
}
}
printf("\n");
}
/* 0 :打印排序前
1 :打印排序后,排序后排序前的list为空
*/
int printindv(int i)
{
if(i==0)
{
printind(indexchain);
}
else if(i==1)
{
printind(sort_indexchain);
}
else
{
ERRPRINT("UNKWON FLAG\n");
return -1;
}
return 0;
}
void printind(list<indexinfo>& indexchain)
{
list<indexinfo>::iterator indx;
for(indx = indexchain.begin(); indx != indexchain.end(); ++indx)
{
printf("===INDEX_ID:%lu\n",indx->index_id );
{
if(indx->level5.size() != 0 )
{
printf("level5 total block is (%lu)\n",(ulint)(indx->level5.size()));
formatprint(indx->level5);
}
if(indx->level4.size() != 0 )
{
printf("level4 total block is (%lu)\n",(ulint)(indx->level4.size()));
formatprint(indx->level4);
}
if(indx->level3.size() != 0 )
{
printf("level3 total block is (%lu)\n",(ulint)(indx->level3.size()));
formatprint(indx->level3);
}
if(indx->level2.size() != 0 )
{
printf("level2 total block is (%lu)\n",(ulint)(indx->level2.size()));
formatprint(indx->level2);
}
if(indx->level1.size() != 0 )
{
printf("level1 total block is (%lu)\n",(ulint)(indx->level1.size()));
formatprint(indx->level1);
}
if(indx->level0.size() != 0 )
{
printf("level0 total block is (%lu)\n",(ulint)(indx->level0.size()));
formatprint(indx->level0);
}
}
}
}
};
/* support block all info*/
struct block_info
{
/*38*/
ulint h_chksum; //FIL_PAGE_SPACE_OR_CHKSUM 4
ulint block_no; //FIL_PAGE_OFFSET 4
ulint b_lsn;//FIL_PAGE_LSN 8
ulint space_id;//FIL_PAGE_SPACE_ID 4
ulint page_type;//FIL_PAGE_TYPE 2
/*56*/
ulint slot_n;//PAGE_N_DIR_SLOTS 2
ulint heap_top;//PAGE_HEAP_TOP 2
ulint n_heap;//PAGE_N_HEAP 2
ulint del_bytes;//PAGE_GARBAGE 2
ulint last_ins_offset;//PAGE_LAST_INSERT 2
ulint page_dir;//PAGE_DIRECTION 2
ulint page_n_dir;//PAGE_N_DIRECTION 2
ulint n_rows;//PAGE_N_RECS 2
ulint max_trx_id;//PAGE_MAX_TRX_ID 8
ulint page_level;//PAGE_LEVEL 2
ulint index_id;//PAGE_INDEX_ID 8
/*no leaf inode list*/
ulint no_leaf_inode_space;//FSEG_HDR_SPACE 4
ulint no_leaf_inode_pageno;//FSEG_HDR_PAGE_NO 4
ulint no_leaf_inode_offset;//FSEG_HDR_OFFSET 2
/*leaf inode list*/
ulint leaf_inode_space;//FSEG_HDR_SPACE 4
ulint leaf_inode_pageno;//FSEG_HDR_PAGE_NO 4
ulint leaf_inode_offset;//FSEG_HDR_OFFSET 2
/**/
ulint f_chksum;//FAIL_CHK 4
ulint f_b_lsn;//FAIL_LSN 4
int get_block_info(byte* frame,ulint bsize)
{
h_chksum = mach_read_from_4(frame+FIL_PAGE_SPACE_OR_CHKSUM);
block_no = mach_read_from_4(frame+FIL_PAGE_OFFSET);
b_lsn = mach_read_from_8(frame+FIL_PAGE_LSN);
space_id = mach_read_from_4(frame+FIL_PAGE_SPACE_ID);
page_type = mach_read_from_2(frame+FIL_PAGE_TYPE);
slot_n = mach_read_from_2(frame+FIL_PAGE_DATA+PAGE_N_DIR_SLOTS);
heap_top = mach_read_from_2(frame+FIL_PAGE_DATA+PAGE_HEAP_TOP);
n_heap = mach_read_from_2(frame+FIL_PAGE_DATA+PAGE_N_HEAP);
del_bytes = mach_read_from_2(frame+FIL_PAGE_DATA+PAGE_GARBAGE);
last_ins_offset = mach_read_from_2(frame+FIL_PAGE_DATA+PAGE_LAST_INSERT);
page_dir = mach_read_from_2(frame+FIL_PAGE_DATA+PAGE_DIRECTION);
page_n_dir = mach_read_from_2(frame+FIL_PAGE_DATA+PAGE_N_DIRECTION);
n_rows = mach_read_from_2(frame+FIL_PAGE_DATA+PAGE_N_RECS);
max_trx_id = mach_read_from_8(frame+FIL_PAGE_DATA+PAGE_MAX_TRX_ID);
page_level = mach_read_from_2(frame+FIL_PAGE_DATA+PAGE_LEVEL);
index_id = mach_read_from_8(frame+FIL_PAGE_DATA+PAGE_INDEX_ID);
f_chksum = mach_read_from_4(frame+bsize-FAIL_CHK);
f_b_lsn = mach_read_from_4(frame+bsize-FAIL_LSN);
leaf_inode_space = mach_read_from_4(frame+FIL_PAGE_DATA+PAGE_BTR_SEG_LEAF);
leaf_inode_pageno = mach_read_from_4(frame+FIL_PAGE_DATA+PAGE_BTR_SEG_LEAF+FSEG_HDR_PAGE_NO);
leaf_inode_offset = mach_read_from_2(frame+FIL_PAGE_DATA+PAGE_BTR_SEG_LEAF+FSEG_HDR_OFFSET);
no_leaf_inode_space = mach_read_from_4(frame+FIL_PAGE_DATA+PAGE_BTR_SEG_TOP);
no_leaf_inode_pageno = mach_read_from_4(frame+FIL_PAGE_DATA+PAGE_BTR_SEG_TOP+FSEG_HDR_PAGE_NO);
no_leaf_inode_offset = mach_read_from_2(frame+FIL_PAGE_DATA+PAGE_BTR_SEG_TOP+FSEG_HDR_OFFSET);
return 0;
}
void print()
{
printf("==== Block base info ====\n");
printf("block_no:%-10lu space_id:%-12lu index_id:%-12lu\n",block_no,space_id,index_id);
printf("slot_nums:%-9lu heaps_rows:%-10lu n_rows:%-10lu\n",slot_n,(n_heap&HEAP_N),n_rows);
printf("heap_top:%-10lu del_bytes:%-11lu last_ins_offset:%-11lu\n",heap_top,del_bytes,last_ins_offset);
printf("page_dir:%-10lu page_n_dir:%-11lu\n",page_dir,page_n_dir);
printf("leaf_inode_space:%-10lu leaf_inode_pag_no:%-10lu\n",leaf_inode_space,leaf_inode_pageno);
printf("leaf_inode_offset:%-10lu\n",leaf_inode_offset);
printf("no_leaf_inode_space:%-7lu no_leaf_inode_pag_no:%-10lu\n",no_leaf_inode_space,no_leaf_inode_pageno);
printf("no_leaf_inode_offset:%-10lu\n",no_leaf_inode_offset);
printf("last_modify_lsn:%lu\n",b_lsn);
printf("page_type:B+_TREE level:%-10lu\n",page_level);
}
int check_block()
{
int ret = 0;
/*
if(h_chksum != f_chksum)
{
ret = -1;
ERRPRINT("bloch check error CHKSUM!");
return ret;
}
*/
#ifdef DEBUG
printf("b_lsn & LSN_HIGH: %lu f_b_lsn %lu\n",(b_lsn & LSN_HIGH),f_b_lsn);
#endif
if((b_lsn & LSN_HIGH) != f_b_lsn)
{
ret = -1;
ERRPRINT("bloch check error LSN!");
return ret;
}
if(page_type != FIL_PAGE_INDEX)
{
ret = -1;
ERRPRINT("bloch check error not b+ block!");
return ret;
}
return ret;
}
};