📄 ha_example.cc
字号:
/* Copyright (C) 2003 MySQL AB This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//* ha_example is a stubbed storage engine. It does nothing at this point. It will let you create/open/delete tables but that is all. You can enable it in your buld by doing the following during your build process: ./configure --with-example-storage-engine Once this is done mysql will let you create tables with: CREATE TABLE A (...) ENGINE=EXAMPLE; The example is setup to use table locks. It implements an example "SHARE" that is inserted into a hash by table name. You can use this to store information of state that any example handler object will be able to see if it is using the same table. Please read the object definition in ha_example.h before reading the rest if this file. To get an idea of what occurs here is an example select that would do a scan of an entire table: ha_example::store_lock ha_example::external_lock ha_example::info ha_example::rnd_init ha_example::extra ENUM HA_EXTRA_CACHE Cash record in HA_rrnd() ha_example::rnd_next ha_example::rnd_next ha_example::rnd_next ha_example::rnd_next ha_example::rnd_next ha_example::rnd_next ha_example::rnd_next ha_example::rnd_next ha_example::rnd_next ha_example::extra ENUM HA_EXTRA_NO_CACHE End cacheing of records (def) ha_example::external_lock ha_example::extra ENUM HA_EXTRA_RESET Reset database to after open In the above example has 9 row called before rnd_next signalled that it was at the end of its data. In the above example the table was already opened (or you would have seen a call to ha_example::open(). Calls to ha_example::extra() are hints as to what will be occuring to the request. Happy coding! -Brian*/#ifdef USE_PRAGMA_IMPLEMENTATION#pragma implementation // gcc: Class implementation#endif#include "../mysql_priv.h"#ifdef HAVE_EXAMPLE_DB#include "ha_example.h"handlerton example_hton= { "EXAMPLE", SHOW_OPTION_YES, "Example storage engine", DB_TYPE_EXAMPLE_DB, NULL, /* We do need to write one! */ 0, /* slot */ 0, /* savepoint size. */ NULL, /* close_connection */ NULL, /* savepoint */ NULL, /* rollback to savepoint */ NULL, /* release savepoint */ NULL, /* commit */ NULL, /* rollback */ NULL, /* prepare */ NULL, /* recover */ NULL, /* commit_by_xid */ NULL, /* rollback_by_xid */ NULL, /* create_cursor_read_view */ NULL, /* set_cursor_read_view */ NULL, /* close_cursor_read_view */ HTON_CAN_RECREATE};/* Variables for example share methods */static HASH example_open_tables; // Hash used to track open tablespthread_mutex_t example_mutex; // This is the mutex we use to init the hashstatic int example_init= 0; // Variable for checking the init state of hash/* Function we use in the creation of our hash to get key.*/static byte* example_get_key(EXAMPLE_SHARE *share,uint *length, my_bool not_used __attribute__((unused))){ *length=share->table_name_length; return (byte*) share->table_name;}/* Example of simple lock controls. The "share" it creates is structure we will pass to each example handler. Do you have to have one of these? Well, you have pieces that are used for locking, and they are needed to function.*/static EXAMPLE_SHARE *get_share(const char *table_name, TABLE *table){ EXAMPLE_SHARE *share; uint length; char *tmp_name; /* So why does this exist? There is no way currently to init a storage engine. Innodb and BDB both have modifications to the server to allow them to do this. Since you will not want to do this, this is probably the next best method. */ if (!example_init) { /* Hijack a mutex for init'ing the storage engine */ pthread_mutex_lock(&LOCK_mysql_create_db); if (!example_init) { example_init++; VOID(pthread_mutex_init(&example_mutex,MY_MUTEX_INIT_FAST)); (void) hash_init(&example_open_tables,system_charset_info,32,0,0, (hash_get_key) example_get_key,0,0); } pthread_mutex_unlock(&LOCK_mysql_create_db); } pthread_mutex_lock(&example_mutex); length=(uint) strlen(table_name); if (!(share=(EXAMPLE_SHARE*) hash_search(&example_open_tables, (byte*) table_name, length))) { if (!(share=(EXAMPLE_SHARE *) my_multi_malloc(MYF(MY_WME | MY_ZEROFILL), &share, sizeof(*share), &tmp_name, length+1, NullS))) { pthread_mutex_unlock(&example_mutex); return NULL; } share->use_count=0; share->table_name_length=length; share->table_name=tmp_name; strmov(share->table_name,table_name); if (my_hash_insert(&example_open_tables, (byte*) share)) goto error; thr_lock_init(&share->lock); pthread_mutex_init(&share->mutex,MY_MUTEX_INIT_FAST); } share->use_count++; pthread_mutex_unlock(&example_mutex); return share;error: pthread_mutex_destroy(&share->mutex); pthread_mutex_unlock(&example_mutex); my_free((gptr) share, MYF(0)); return NULL;}/* Free lock controls. We call this whenever we close a table. If the table had the last reference to the share then we free memory associated with it.*/static int free_share(EXAMPLE_SHARE *share){ pthread_mutex_lock(&example_mutex); if (!--share->use_count) { hash_delete(&example_open_tables, (byte*) share); thr_lock_delete(&share->lock); pthread_mutex_destroy(&share->mutex); my_free((gptr) share, MYF(0)); } pthread_mutex_unlock(&example_mutex); return 0;}ha_example::ha_example(TABLE *table_arg) :handler(&example_hton, table_arg){}/* If frm_error() is called then we will use this to to find out what file extentions exist for the storage engine. This is also used by the default rename_table and delete_table method in handler.cc.*/static const char *ha_example_exts[] = { NullS};const char **ha_example::bas_ext() const{ return ha_example_exts;}/* Used for opening tables. The name will be the name of the file. A table is opened when it needs to be opened. For instance when a request comes in for a select on the table (tables are not open and closed for each request, they are cached). Called from handler.cc by handler::ha_open(). The server opens all tables by calling ha_open() which then calls the handler specific open().*/int ha_example::open(const char *name, int mode, uint test_if_locked){ DBUG_ENTER("ha_example::open"); if (!(share = get_share(name, table))) DBUG_RETURN(1); thr_lock_data_init(&share->lock,&lock,NULL); DBUG_RETURN(0);}/* Closes a table. We call the free_share() function to free any resources that we have allocated in the "shared" structure. Called from sql_base.cc, sql_select.cc, and table.cc. In sql_select.cc it is only used to close up temporary tables or during the process where a temporary table is converted over to being a myisam table. For sql_base.cc look at close_data_tables().*/int ha_example::close(void){ DBUG_ENTER("ha_example::close"); DBUG_RETURN(free_share(share));}/* write_row() inserts a row. No extra() hint is given currently if a bulk load is happeneding. buf() is a byte array of data. You can use the field information to extract the data from the native byte array type. Example of this would be: for (Field **field=table->field ; *field ; field++) { ... } See ha_tina.cc for an example of extracting all of the data as strings. ha_berekly.cc has an example of how to store it intact by "packing" it for ha_berkeley's own native storage type. See the note for update_row() on auto_increments and timestamps. This case also applied to write_row(). Called from item_sum.cc, item_sum.cc, sql_acl.cc, sql_insert.cc, sql_insert.cc, sql_select.cc, sql_table.cc, sql_udf.cc, and sql_update.cc.*/int ha_example::write_row(byte * buf){ DBUG_ENTER("ha_example::write_row"); DBUG_RETURN(HA_ERR_WRONG_COMMAND);}/* Yes, update_row() does what you expect, it updates a row. old_data will have the previous row record in it, while new_data will have the newest data in it. Keep in mind that the server can do updates based on ordering if an ORDER BY clause was used. Consecutive ordering is not guarenteed. Currently new_data will not have an updated auto_increament record, or and updated timestamp field. You can do these for example by doing these: if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_UPDATE) table->timestamp_field->set_time(); if (table->next_number_field && record == table->record[0]) update_auto_increment(); Called from sql_select.cc, sql_acl.cc, sql_update.cc, and sql_insert.cc.*/int ha_example::update_row(const byte * old_data, byte * new_data){ DBUG_ENTER("ha_example::update_row"); DBUG_RETURN(HA_ERR_WRONG_COMMAND);}/* This will delete a row. buf will contain a copy of the row to be deleted. The server will call this right after the current row has been called (from either a previous rnd_nexT() or index call). If you keep a pointer to the last row or can access a primary key it will make doing the deletion quite a bit easier. Keep in mind that the server does no guarentee consecutive deletions. ORDER BY clauses can be used. Called in sql_acl.cc and sql_udf.cc to manage internal table information. Called in sql_delete.cc, sql_insert.cc, and sql_select.cc. In sql_select it is used for removing duplicates while in insert it is used for REPLACE calls.*/int ha_example::delete_row(const byte * buf){ DBUG_ENTER("ha_example::delete_row"); DBUG_RETURN(HA_ERR_WRONG_COMMAND);}/* Positions an index cursor to the index specified in the handle. Fetches the row if available. If the key value is null, begin at the first key of the index.*/int ha_example::index_read(byte * buf, const byte * key, uint key_len __attribute__((unused)), enum ha_rkey_function find_flag __attribute__((unused))){ DBUG_ENTER("ha_example::index_read"); DBUG_RETURN(HA_ERR_WRONG_COMMAND);}/* Positions an index cursor to the index specified in key. Fetches the row if any. This is only used to read whole keys.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -