C語言版Mysql儲存塊(page)格式讀取工具(原始碼)

來源:互聯網
上載者:User

標籤:mysql   原始碼   

本文原創為freas_1990,轉載請標明出處:http://blog.csdn.net/freas_1990/article/details/45843615


C版本的Mysql儲存塊格式讀取工具原始碼如下:

其中mysqlBlock Storage採用大端位元組,所以需要做一定的轉換,C語言指標強力轉換方式如下:

#include <stdio.h>#include <stdlib.h>int readpage(unsigned char * page_hdr,int page_off,FILE *fd) {int ret = -1;if((ret = fseek(fd,page_off,SEEK_SET)) != 0) {//fseek error may not be caught successfully hereprintf("fseek failed\n");exit(-1);}printf("now from our calclation page_off is %d\n",page_off);if(!(ret = fread(page_hdr,62,1,fd))) {printf("read page failed! ret is %d\n",ret);return -1;}return 1;}int main() {unsigned char page_hdr_tmp[62];unsigned char * tmp;unsigned int page_offset = 0;unsigned short page_type = 0;unsigned int tab_space_id = 0;unsigned short page_level = 0;int ret = -1;int page_count = 0;int filesize = 0;FILE *fd;if(!freopen("C:\\MinGW\\bin\\log.txt", "w", stdout)) {printf("log file created failed!\n");}if(!(fd = fopen("C:\\MinGW\\bin\\payment.ibd","rb"))) {printf("fopen failed!\n");return -1;} if((ret = fseek(fd,0,SEEK_END)) != 0) {printf("fseek failed\n");exit(-1);}filesize = ftell(fd);printf("filesize is %d\n",filesize);while (page_count < filesize/1024/16) {printf("processing the %d [rd] page\n",page_count);if(ret = readpage(page_hdr_tmp, page_count*16*1024, fd)) {page_count++;} else {printf("readpage failed!\n");system("pause");exit -1;}//for page_offsettmp = (unsigned char *)&page_offset;tmp[0] = page_hdr_tmp[7];tmp[1] = page_hdr_tmp[6];tmp[2] = page_hdr_tmp[5];tmp[3] = page_hdr_tmp[4];//for page_typetmp = (unsigned char *)&page_type;tmp[0] = page_hdr_tmp[25];tmp[1] = page_hdr_tmp[24];//for tab_space_idtmp = (unsigned char *)&tab_space_id;tmp[0] = page_hdr_tmp[37];tmp[1] = page_hdr_tmp[36];tmp[2] = page_hdr_tmp[35];tmp[3] = page_hdr_tmp[34];//for page_leveltmp = (unsigned char *)&page_level;tmp[0] = page_hdr_tmp[61];tmp[1] = page_hdr_tmp[60];printf("page_offset is %d\n",page_offset);printf("page_type is %x\n",page_type);printf("tab_space_id is %x\n",tab_space_id);printf("page_level is %x\n\n",page_level);}fclose(fd);system("pause");return 0;}

我們開啟程式輸出的log來看,資訊如下:

filesize is 10485760processing the 0 [rd] pagenow from our calclation page_off is 0page_offset is 0page_type is 8tab_space_id is 11page_level is bprocessing the 1 [rd] pagenow from our calclation page_off is 16384page_offset is 1page_type is 5tab_space_id is 11page_level is 0processing the 2 [rd] pagenow from our calclation page_off is 32768page_offset is 2page_type is 3tab_space_id is 11page_level is 0processing the 3 [rd] pagenow from our calclation page_off is 49152page_offset is 3page_type is 45bftab_space_id is 11page_level is 0

其中page_type的45bf代表的是索引節點。而page_level是0,意思是,索引節點的層級為0。

到了第75 [rd]塊的時候,所有塊都是空白塊了。

processing the 74 [rd] pagenow from our calclation page_off is 1212416page_offset is 74page_type is 45bftab_space_id is 11page_level is 0processing the 75 [rd] pagenow from our calclation page_off is 1228800page_offset is 0page_type is 0tab_space_id is 0page_level is 0processing the 76 [rd] pagenow from our calclation page_off is 1245184page_offset is 0page_type is 0tab_space_id is 0page_level is 0

測試的ibd檔案是mysql 5.6內建的sakila庫下的payment.ibd。



C語言版Mysql儲存塊(page)格式讀取工具(原始碼)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.