summaryrefslogtreecommitdiff
path: root/storage/example
diff options
context:
space:
mode:
Diffstat (limited to 'storage/example')
-rw-r--r--storage/example/CMakeLists.txt8
-rw-r--r--storage/example/Makefile.am48
-rw-r--r--storage/example/ha_example.cc186
-rw-r--r--storage/example/ha_example.h18
-rw-r--r--storage/example/plug.in3
5 files changed, 130 insertions, 133 deletions
diff --git a/storage/example/CMakeLists.txt b/storage/example/CMakeLists.txt
index 51d8909e361..53f5d6619a0 100644
--- a/storage/example/CMakeLists.txt
+++ b/storage/example/CMakeLists.txt
@@ -1,5 +1,4 @@
-# Copyright (c) 2006-2008 MySQL AB, 2009 Sun Microsystems, Inc.
-# Use is subject to license terms.
+# Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
#
# 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
@@ -12,8 +11,7 @@
#
# 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-INCLUDE("${PROJECT_SOURCE_DIR}/storage/mysql_storage_engine.cmake")
SET(EXAMPLE_SOURCES ha_example.cc)
-MYSQL_STORAGE_ENGINE(EXAMPLE)
+MYSQL_ADD_PLUGIN(example ${EXAMPLE_SOURCES} STORAGE_ENGINE MODULE_ONLY)
diff --git a/storage/example/Makefile.am b/storage/example/Makefile.am
deleted file mode 100644
index 1ef7d9fc86e..00000000000
--- a/storage/example/Makefile.am
+++ /dev/null
@@ -1,48 +0,0 @@
-# Copyright (c) 2005, 2010, Oracle and/or its affiliates
-#
-# 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; version 2 of the License.
-#
-# 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-
-#called from the top level Makefile
-
-MYSQLDATAdir = $(localstatedir)
-MYSQLSHAREdir = $(pkgdatadir)
-MYSQLBASEdir= $(prefix)
-MYSQLLIBdir= $(pkglibdir)
-pkgplugindir = $(pkglibdir)/plugin
-INCLUDES = -I$(top_srcdir)/include -I$(top_builddir)/include \
- -I$(top_srcdir)/regex \
- -I$(top_srcdir)/sql \
- -I$(srcdir)
-WRAPLIBS=
-
-LDADD =
-
-DEFS = @DEFS@
-
-noinst_HEADERS = ha_example.h
-
-EXTRA_LTLIBRARIES = libexample.la ha_example.la
-pkgplugin_LTLIBRARIES = @plugin_example_shared_target@
-ha_example_la_LDFLAGS = -module -rpath $(pkgplugindir) -L$(top_builddir)/libservices -lmysqlservices
-ha_example_la_CXXFLAGS= -shared $(AM_CXXFLAGS) -DMYSQL_DYNAMIC_PLUGIN
-ha_example_la_SOURCES = ha_example.cc
-
-
-noinst_LTLIBRARIES = @plugin_example_static_target@
-libexample_la_CXXFLAGS = $(AM_CXXFLAGS)
-libexample_la_SOURCES= ha_example.cc
-
-
-EXTRA_DIST = CMakeLists.txt plug.in
-
diff --git a/storage/example/ha_example.cc b/storage/example/ha_example.cc
index 013a92fc780..5101d73d23c 100644
--- a/storage/example/ha_example.cc
+++ b/storage/example/ha_example.cc
@@ -12,8 +12,7 @@
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-*/
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/**
@file ha_example.cc
@@ -93,10 +92,9 @@
#pragma implementation // gcc: Class implementation
#endif
-#define MYSQL_SERVER 1
-#include "mysql_priv.h"
-#include "ha_example.h"
#include <mysql/plugin.h>
+#include "ha_example.h"
+#include "sql_class.h"
static handler *example_create_handler(handlerton *hton,
TABLE_SHARE *table,
@@ -113,7 +111,7 @@ handlerton *example_hton;
static HASH example_open_tables;
/* The mutex used to init the hash; variable for example share methods */
-pthread_mutex_t example_mutex;
+mysql_mutex_t example_mutex;
/**
@@ -203,15 +201,41 @@ static uchar* example_get_key(EXAMPLE_SHARE *share, size_t *length,
return (uchar*) share->table_name;
}
+#ifdef HAVE_PSI_INTERFACE
+static PSI_mutex_key ex_key_mutex_example, ex_key_mutex_EXAMPLE_SHARE_mutex;
+
+static PSI_mutex_info all_example_mutexes[]=
+{
+ { &ex_key_mutex_example, "example", PSI_FLAG_GLOBAL},
+ { &ex_key_mutex_EXAMPLE_SHARE_mutex, "EXAMPLE_SHARE::mutex", 0}
+};
+
+static void init_example_psi_keys()
+{
+ const char* category= "example";
+ int count;
+
+ if (PSI_server == NULL)
+ return;
+
+ count= array_elements(all_example_mutexes);
+ PSI_server->register_mutex(category, all_example_mutexes, count);
+}
+#endif
+
static int example_init_func(void *p)
{
DBUG_ENTER("example_init_func");
+#ifdef HAVE_PSI_INTERFACE
+ init_example_psi_keys();
+#endif
+
example_hton= (handlerton *)p;
- 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);
+ mysql_mutex_init(ex_key_mutex_example, &example_mutex, MY_MUTEX_INIT_FAST);
+ (void) my_hash_init(&example_open_tables,system_charset_info,32,0,0,
+ (my_hash_get_key) example_get_key,0,0);
example_hton->state= SHOW_OPTION_YES;
example_hton->create= example_create_handler;
@@ -230,8 +254,8 @@ static int example_done_func(void *p)
if (example_open_tables.records)
error= 1;
- hash_free(&example_open_tables);
- pthread_mutex_destroy(&example_mutex);
+ my_hash_free(&example_open_tables);
+ mysql_mutex_destroy(&example_mutex);
DBUG_RETURN(error);
}
@@ -251,12 +275,12 @@ static EXAMPLE_SHARE *get_share(const char *table_name, TABLE *table)
uint length;
char *tmp_name;
- pthread_mutex_lock(&example_mutex);
+ mysql_mutex_lock(&example_mutex);
length=(uint) strlen(table_name);
- if (!(share=(EXAMPLE_SHARE*) hash_search(&example_open_tables,
- (uchar*) table_name,
- length)))
+ if (!(share=(EXAMPLE_SHARE*) my_hash_search(&example_open_tables,
+ (uchar*) table_name,
+ length)))
{
if (!(share=(EXAMPLE_SHARE *)
my_multi_malloc(MYF(MY_WME | MY_ZEROFILL),
@@ -264,7 +288,7 @@ static EXAMPLE_SHARE *get_share(const char *table_name, TABLE *table)
&tmp_name, length+1,
NullS)))
{
- pthread_mutex_unlock(&example_mutex);
+ mysql_mutex_unlock(&example_mutex);
return NULL;
}
@@ -275,16 +299,17 @@ static EXAMPLE_SHARE *get_share(const char *table_name, TABLE *table)
if (my_hash_insert(&example_open_tables, (uchar*) share))
goto error;
thr_lock_init(&share->lock);
- pthread_mutex_init(&share->mutex,MY_MUTEX_INIT_FAST);
+ mysql_mutex_init(ex_key_mutex_EXAMPLE_SHARE_mutex,
+ &share->mutex, MY_MUTEX_INIT_FAST);
}
share->use_count++;
- pthread_mutex_unlock(&example_mutex);
+ mysql_mutex_unlock(&example_mutex);
return share;
error:
- pthread_mutex_destroy(&share->mutex);
- my_free(share, MYF(0));
+ mysql_mutex_destroy(&share->mutex);
+ my_free(share);
return NULL;
}
@@ -298,15 +323,15 @@ error:
static int free_share(EXAMPLE_SHARE *share)
{
- pthread_mutex_lock(&example_mutex);
+ mysql_mutex_lock(&example_mutex);
if (!--share->use_count)
{
- hash_delete(&example_open_tables, (uchar*) share);
+ my_hash_delete(&example_open_tables, (uchar*) share);
thr_lock_delete(&share->lock);
- pthread_mutex_destroy(&share->mutex);
- my_free(share, MYF(0));
+ mysql_mutex_destroy(&share->mutex);
+ my_free(share);
}
- pthread_mutex_unlock(&example_mutex);
+ mysql_mutex_unlock(&example_mutex);
return 0;
}
@@ -418,14 +443,14 @@ int ha_example::close(void)
is happening. buf() is a byte array of data. You can use the field
information to extract the data from the native byte array type.
- @details
+ @details
Example of this would be:
- @code
+ @code
for (Field **field=table->field ; *field ; field++)
{
...
}
- @endcode
+ @endcode
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
@@ -437,7 +462,7 @@ int ha_example::close(void)
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.
- @see
+ @see
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
*/
@@ -445,7 +470,13 @@ int ha_example::close(void)
int ha_example::write_row(uchar *buf)
{
DBUG_ENTER("ha_example::write_row");
- DBUG_RETURN(HA_ERR_WRONG_COMMAND);
+ /*
+ Example of a successful write_row. We don't store the data
+ anywhere; they are thrown away. A real implementation will
+ probably need to do something with 'buf'. We report a success
+ here, to pretend that the insert was successful.
+ */
+ DBUG_RETURN(0);
}
@@ -456,19 +487,19 @@ int ha_example::write_row(uchar *buf)
Keep in mind that the server can do updates based on ordering if an ORDER BY
clause was used. Consecutive ordering is not guaranteed.
- @details
+ @details
Currently new_data will not have an updated auto_increament record, or
and updated timestamp field. You can do these for example by doing:
- @code
+ @code
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();
- @endcode
+ @endcode
Called from sql_select.cc, sql_acl.cc, sql_update.cc, and sql_insert.cc.
- @see
+ @see
sql_select.cc, sql_acl.cc, sql_update.cc and sql_insert.cc
*/
int ha_example::update_row(const uchar *old_data, uchar *new_data)
@@ -518,8 +549,10 @@ int ha_example::index_read_map(uchar *buf, const uchar *key,
enum ha_rkey_function find_flag
__attribute__((unused)))
{
+ int rc;
DBUG_ENTER("ha_example::index_read");
- DBUG_RETURN(HA_ERR_WRONG_COMMAND);
+ rc= HA_ERR_WRONG_COMMAND;
+ DBUG_RETURN(rc);
}
@@ -530,8 +563,10 @@ int ha_example::index_read_map(uchar *buf, const uchar *key,
int ha_example::index_next(uchar *buf)
{
+ int rc;
DBUG_ENTER("ha_example::index_next");
- DBUG_RETURN(HA_ERR_WRONG_COMMAND);
+ rc= HA_ERR_WRONG_COMMAND;
+ DBUG_RETURN(rc);
}
@@ -542,8 +577,10 @@ int ha_example::index_next(uchar *buf)
int ha_example::index_prev(uchar *buf)
{
+ int rc;
DBUG_ENTER("ha_example::index_prev");
- DBUG_RETURN(HA_ERR_WRONG_COMMAND);
+ rc= HA_ERR_WRONG_COMMAND;
+ DBUG_RETURN(rc);
}
@@ -551,16 +588,18 @@ int ha_example::index_prev(uchar *buf)
@brief
index_first() asks for the first key in the index.
- @details
+ @details
Called from opt_range.cc, opt_sum.cc, sql_handler.cc, and sql_select.cc.
- @see
+ @see
opt_range.cc, opt_sum.cc, sql_handler.cc and sql_select.cc
*/
int ha_example::index_first(uchar *buf)
{
+ int rc;
DBUG_ENTER("ha_example::index_first");
- DBUG_RETURN(HA_ERR_WRONG_COMMAND);
+ rc= HA_ERR_WRONG_COMMAND;
+ DBUG_RETURN(rc);
}
@@ -568,16 +607,18 @@ int ha_example::index_first(uchar *buf)
@brief
index_last() asks for the last key in the index.
- @details
+ @details
Called from opt_range.cc, opt_sum.cc, sql_handler.cc, and sql_select.cc.
- @see
+ @see
opt_range.cc, opt_sum.cc, sql_handler.cc and sql_select.cc
*/
int ha_example::index_last(uchar *buf)
{
+ int rc;
DBUG_ENTER("ha_example::index_last");
- DBUG_RETURN(HA_ERR_WRONG_COMMAND);
+ rc= HA_ERR_WRONG_COMMAND;
+ DBUG_RETURN(rc);
}
@@ -587,11 +628,11 @@ int ha_example::index_last(uchar *buf)
scan. See the example in the introduction at the top of this file to see when
rnd_init() is called.
- @details
+ @details
Called from filesort.cc, records.cc, sql_handler.cc, sql_select.cc, sql_table.cc,
and sql_update.cc.
- @see
+ @see
filesort.cc, records.cc, sql_handler.cc, sql_select.cc, sql_table.cc and sql_update.cc
*/
int ha_example::rnd_init(bool scan)
@@ -614,17 +655,19 @@ int ha_example::rnd_end()
The Field structure for the table is the key to getting data into buf
in a manner that will allow the server to understand it.
- @details
+ @details
Called from filesort.cc, records.cc, sql_handler.cc, sql_select.cc, sql_table.cc,
and sql_update.cc.
- @see
+ @see
filesort.cc, records.cc, sql_handler.cc, sql_select.cc, sql_table.cc and sql_update.cc
*/
int ha_example::rnd_next(uchar *buf)
{
+ int rc;
DBUG_ENTER("ha_example::rnd_next");
- DBUG_RETURN(HA_ERR_END_OF_FILE);
+ rc= HA_ERR_END_OF_FILE;
+ DBUG_RETURN(rc);
}
@@ -633,11 +676,11 @@ int ha_example::rnd_next(uchar *buf)
position() is called after each call to rnd_next() if the data needs
to be ordered. You can do something like the following to store
the position:
- @code
+ @code
my_store_ptr(ref, ref_length, current_position);
- @endcode
+ @endcode
- @details
+ @details
The server uses ref to store data. ref_length in the above case is
the size needed to store current_position. ref is just a byte array
that the server will maintain. If you are using offsets to mark rows, then
@@ -646,7 +689,7 @@ int ha_example::rnd_next(uchar *buf)
Called from filesort.cc, sql_select.cc, sql_delete.cc, and sql_update.cc.
- @see
+ @see
filesort.cc, sql_select.cc, sql_delete.cc and sql_update.cc
*/
void ha_example::position(const uchar *record)
@@ -663,16 +706,18 @@ void ha_example::position(const uchar *record)
ref. You can use ha_get_ptr(pos,ref_length) to retrieve whatever key
or position you saved when position() was called.
- @details
+ @details
Called from filesort.cc, records.cc, sql_insert.cc, sql_select.cc, and sql_update.cc.
- @see
+ @see
filesort.cc, records.cc, sql_insert.cc, sql_select.cc and sql_update.cc
*/
int ha_example::rnd_pos(uchar *buf, uchar *pos)
{
+ int rc;
DBUG_ENTER("ha_example::rnd_pos");
- DBUG_RETURN(HA_ERR_WRONG_COMMAND);
+ rc= HA_ERR_WRONG_COMMAND;
+ DBUG_RETURN(rc);
}
@@ -681,15 +726,15 @@ int ha_example::rnd_pos(uchar *buf, uchar *pos)
::info() is used to return information to the optimizer. See my_base.h for
the complete description.
- @details
+ @details
Currently this table handler doesn't implement most of the fields really needed.
SHOW also makes use of this data.
You will probably want to have the following in your code:
- @code
+ @code
if (records < 2)
records = 2;
- @endcode
+ @endcode
The reason is that the server will optimize for cases of only a single
record. If, in a table scan, you don't know the number of records, it
will probably be better to set records to two so you can return as many
@@ -708,7 +753,7 @@ int ha_example::rnd_pos(uchar *buf, uchar *pos)
sql_select.cc, sql_select.cc, sql_show.cc, sql_show.cc, sql_show.cc, sql_show.cc,
sql_table.cc, sql_union.cc, and sql_update.cc.
- @see
+ @see
filesort.cc, ha_heap.cc, item_sum.cc, opt_sum.cc, sql_delete.cc, sql_delete.cc,
sql_derived.cc, sql_select.cc, sql_select.cc, sql_select.cc, sql_select.cc,
sql_select.cc, sql_show.cc, sql_show.cc, sql_show.cc, sql_show.cc, sql_table.cc,
@@ -742,14 +787,14 @@ int ha_example::extra(enum ha_extra_function operation)
Used to delete all rows in a table, including cases of truncate and cases where
the optimizer realizes that all rows will be removed as a result of an SQL statement.
- @details
+ @details
Called from item_sum.cc by Item_func_group_concat::clear(),
Item_sum_count_distinct::clear(), and Item_func_group_concat::clear().
Called from sql_delete.cc by mysql_delete().
Called from sql_select.cc by JOIN::reinit().
Called from sql_union.cc by st_select_lex_unit::exec().
- @see
+ @see
Item_func_group_concat::clear(), Item_sum_count_distinct::clear() and
Item_func_group_concat::clear() in item_sum.cc;
mysql_delete() in sql_delete.cc;
@@ -771,11 +816,11 @@ int ha_example::delete_all_rows()
here. Hint: Read the section "locking functions for mysql" in lock.cc to understand
this.
- @details
+ @details
Called from lock.cc by lock_external() and unlock_external(). Also called
from sql_table.cc by copy_data_between_tables().
- @see
+ @see
lock.cc by lock_external() and unlock_external() in lock.cc;
the section "locking functions for mysql" in lock.cc;
copy_data_between_tables() in sql_table.cc.
@@ -793,7 +838,7 @@ int ha_example::external_lock(THD *thd, int lock_type)
should be needed for the table. For updates/deletes/inserts we get WRITE
locks, for SELECT... we get read locks.
- @details
+ @details
Before adding the lock into the table lock handler (see thr_lock.c),
mysqld calls store lock with the requested locks. Store lock can now
modify a write lock to a read lock (or some other lock), ignore the
@@ -816,12 +861,12 @@ int ha_example::external_lock(THD *thd, int lock_type)
Called from lock.cc by get_lock_data().
- @note
+ @note
In this method one should NEVER rely on table->in_use, it may, in fact,
refer to a different thread! (this happens if get_lock_data() is called
from mysql_lock_abort_for_thread() function)
- @see
+ @see
get_lock_data() in lock.cc
*/
THR_LOCK_DATA **ha_example::store_lock(THD *thd,
@@ -842,7 +887,7 @@ THR_LOCK_DATA **ha_example::store_lock(THD *thd,
shared references released). The variable name will just be the name of
the table. You will need to remove any files you have created at this point.
- @details
+ @details
If you do not implement this, the default delete_table() is called from
handler.cc and it will delete all files with the file extensions returned
by bas_ext().
@@ -851,7 +896,7 @@ THR_LOCK_DATA **ha_example::store_lock(THD *thd,
during create if the table_flag HA_DROP_BEFORE_CREATE was specified for
the storage engine.
- @see
+ @see
delete_table and ha_create_table() in handler.cc
*/
int ha_example::delete_table(const char *name)
@@ -1055,7 +1100,7 @@ static int show_func_example(MYSQL_THD thd, struct st_mysql_show_var *var,
var->type= SHOW_CHAR;
var->value= buf; // it's of SHOW_VAR_FUNC_BUFF_SIZE bytes
my_snprintf(buf, SHOW_VAR_FUNC_BUFF_SIZE,
- "enum_var is %u, ulong_var is %lu, %.6b", // %b is MySQL extension
+ "enum_var is %lu, ulong_var is %lu, %.6b", // %b is MySQL extension
srv_enum_var, srv_ulong_var, "really");
return 0;
}
@@ -1079,7 +1124,8 @@ mysql_declare_plugin(example)
0x0001 /* 0.1 */,
func_status, /* status variables */
example_system_variables, /* system variables */
- NULL /* config options */
+ NULL, /* config options */
+ 0, /* flags */
}
mysql_declare_plugin_end;
maria_declare_plugin(example)
diff --git a/storage/example/ha_example.h b/storage/example/ha_example.h
index 79e1c422b95..ca8ca5ff293 100644
--- a/storage/example/ha_example.h
+++ b/storage/example/ha_example.h
@@ -12,8 +12,7 @@
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-*/
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/** @file ha_example.h
@@ -37,6 +36,11 @@
#pragma interface /* gcc class implementation */
#endif
+#include "my_global.h" /* ulonglong */
+#include "thr_lock.h" /* THR_LOCK, THR_LOCK_DATA */
+#include "handler.h" /* handler */
+#include "my_base.h" /* ha_rows */
+
/** @brief
EXAMPLE_SHARE is a structure that will be shared among all open handlers.
This example implements the minimum of what you will probably need.
@@ -44,7 +48,7 @@
typedef struct st_example_share {
char *table_name;
uint table_name_length,use_count;
- pthread_mutex_t mutex;
+ mysql_mutex_t mutex;
THR_LOCK lock;
} EXAMPLE_SHARE;
@@ -85,11 +89,11 @@ public:
ulonglong table_flags() const
{
/*
- We are saying that this engine is just row capable to have an
- engine that can only handle row-based logging. This is used in
- testing.
+ We are saying that this engine is just statement capable to have
+ an engine that can only handle statement-based logging. This is
+ used in testing.
*/
- return HA_BINLOG_ROW_CAPABLE;
+ return HA_BINLOG_STMT_CAPABLE;
}
/** @brief
diff --git a/storage/example/plug.in b/storage/example/plug.in
deleted file mode 100644
index ee6beaac64f..00000000000
--- a/storage/example/plug.in
+++ /dev/null
@@ -1,3 +0,0 @@
-MYSQL_STORAGE_ENGINE(example,, [Example Storage Engine],
- [Example for Storage Engines for developers], [max,max-no-ndb])
-MYSQL_PLUGIN_DYNAMIC(example, [ha_example.la])