当前位置:Gxlcms > 数据库问题 > MySQL的多存储引擎架构

MySQL的多存储引擎架构

时间:2021-07-01 10:21:17 帮助过:54人阅读

File System

所有的数据,数据库、表的定义,表的每一行的内容,索引,都是存在文件系统上,以文件的方式存在的。当然有些存储引擎比如InnoDB,也支持不使用文件系统直接管理裸设备,但现代文件系统的实现使得这样做没有必要了。

在文件系统之下,可以使用本地磁盘,可以使用DAS、NAS、SAN等各种存储系统。

存储引擎API

MySQL定义了一系列存储引擎API,以支持插件式存储引擎架构。API以Handler类的虚函数的方式存在,可在代码库下的./sql/handler.h中查看详细信息,可在handler类的注释中看到描述:


/**
  The handler class is the interface for dynamically loadable
  storage engines. Do not add ifdefs and take care when adding or
  changing virtual functions to avoid vtable confusion

  Functions in this class accept and return table columns data. Two data
  representation formats are used:
  1. TableRecordFormat - Used to pass [partial] table records to/from
     storage engine

  2. KeyTupleFormat - used to pass index search tuples (aka "keys") to
     storage engine. See opt_range.cc for description of this format.

  TableRecordFormat
  =================
  [Warning: this description is work in progress and may be incomplete]
  The table record is stored in a fixed-size buffer:

    record: null_bytes, column1_data, column2_data, ...

  //篇幅原因,略去部分内容。

*/
class handler :public Sql_alloc
{
    //篇幅原因,不列出具体代码。读者可直接在源码文件./sql/handler.h中找到具体内容。
}

下面我将分类描述部分存储引擎API。

创建、打开和关闭表

通过函数create来创建一个table:


/**
  *name:要创建的表的名字
  *from:一个TABLE类型的结构,要创建的表的定义,跟MySQL Server已经创建好的tablename.frm文件内容是匹配的
  *info:一个HA_CREATE_INFO类型的结构,包含了客户端输入的CREATE TABLE语句的信息
*/

int create(const char *name, TABLE *form, HA_CREATE_INFO *info);

通过函数open来打开一个table:


/**
  mode包含以下两种
  O_RDONLY  -  Open read only
  O_RDWR    -  Open read/write
*/
int open(const char *name, int mode, int test_if_locked);

通过函数close来关闭一个table:


int close(void);

对表加锁

当客户端调用LOCK TABLE时,通过external_lock函数加锁:


int ha_example::external_lock(THD *thd, int lock_type)

全表扫描


//初始化全表扫描
virtual int rnd_init (bool scan);

//从表中读取下一行
virtual int rnd_next (byte* buf);

通过索引访问table内容


//使用索引前调用该方法
int ha_foo::index_init(uint keynr, bool sorted) 


//使用索引后调用该方法
int ha_foo::index_end(uint keynr, bool sorted)

//读取索引第一条内容
int ha_index_first(uchar * buf);

//读取索引下一条内容
int ha_index_next(uchar * buf);

//读取索引前一条内容
int ha_index_prev(uchar * buf);

//读取索引最后一条内容
int ha_index_last(uchar * buf);

//给定一个key基于索引读取内容
int index_read(uchar * buf, const uchar * key, uint key_len,
                         enum ha_rkey_function find_flag)

事务处理


//开始一个事务
int my_handler::start_stmt(THD *thd, thr_lock_type lock_type)

//回滚一个事务
int (*rollback)(THD *thd, bool all); 

//提交一个事务
int (*commit)(THD *thd, bool all);

如何编写自己的存储引擎

在MySQL的官方文档上,有对于编写自己的存储引擎的指导文档,链接如下。

作为编写自己存储引擎的开始,你可以查看MySQL源码库中的一个EXAMPLE存储引擎,它实现了必须要实现的存储引擎API,可以通过复制它们作为编写我们自己存储引擎的开始:


sed -e s/EXAMPLE/FOO/g -e s/example/foo/g ha_example.h > ha_foo.h
sed -e s/EXAMPLE/FOO/g -e s/example/foo/g ha_example.cc > ha_foo.cc

 

MySQL的多存储引擎架构

标签:数据库   mysql 5.7   步骤   hive   用户   back   work   data   commit   

人气教程排行