首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫

如何編寫 MySQL 儲存引擎:MySQL 5.1 插件架構下的 Handler 接口實現指南

     編輯:MySQL 資料庫相關知識

儲存引擎本質:接口實現

MySQL 5.1 開發儲存引擎相對便捷。本質上是依 MySQL 約定實現特定接口:插入記錄調用 write_row、索引檢索調用 index_read/index_next 等。核心接口由兩大結構定義:sql/handler.h 中的 handlerton 結構與 handler 類。

核心接口架構

  • handlerton: 事務操作接口(commit、rollback、savepoint 等)
  • handler 類: 表、索引、記錄操作接口(create、open、read、write、update、delete、index_init 等)

開發流程

1. 非事務引擎:僅需繼承 handler 並重載所需方法

2. 事務引擎:額外實現 handlerton 指定的事務接口

引擎註冊聲明

定義 st_mysql_storage_engine 變數

struct st_mysql_storage_engine foo_storage_engine =
{ MYSQL_HANDLERTON_INTERFACE_VERSION };

使用 MySQL_declare_plugin 宏聲明


mysql_declare_plugin(foo)
{
    MYSQL_STORAGE_ENGINE_PLUGIN,
    &foo_storage_engine,
    "FOO",                      // SHOW ENGINES 顯示名
    "Author Here",              // 作者
    "Foo storage engine",       // 說明
    PLUGIN_LICENSE_GPL,
    init_func,                  // 載入時調用
    done_func,                  // 卸載時調用
    0x0001,                     // 版本 0.1
    NULL, NULL, NULL            // 狀態/系統/配置變數
}
mysql_declare_plugin_end;

初始化流程

  1. 載入時調用 init_func,傳入 handlerton,引擎需設定 state、db_type、create、flags 等屬性(create 為函數指標,稍後被調用)。事務引擎還需設定事務接口函數指標。
  2. MySQL 調用 create 函數,引擎構建 handler 派生類實例並返回。
  3. 初始化完成,接口確定,後續 MySQL 在相應時機調用約定方法。

參考範例

原始碼 storage/example 目錄提供最小引擎 EXAMPLE 實現(僅能載入不可存儲)。新引擎開發最便捷方式:複製 example 代碼修改。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved