[Groonga-mysql-commit] mroonga/mroonga [master] added UDF 'last_insert_grn_id' which read latest grn_id generated when INSERT is executed by current connection.

Back to archive index

null+****@clear***** null+****@clear*****
2010年 11月 20日 (土) 10:12:11 JST


Tetsuro IKEDA	2010-11-20 01:12:11 +0000 (Sat, 20 Nov 2010)

  New Revision: d63585c8889dca08f6d0bd060055628382303ba5

  Log:
    added UDF 'last_insert_grn_id' which read latest grn_id generated when INSERT is executed by current connection.

  Added files:
    test/sql/r/last_insert_grn_id.result
    test/sql/t/last_insert_grn_id.test
  Modified files:
    ha_mroonga.cc
    ha_mroonga.h

  Modified: ha_mroonga.cc (+46 -0)
===================================================================
--- ha_mroonga.cc    2010-11-19 02:17:14 +0000 (29f8fa6)
+++ ha_mroonga.cc    2010-11-20 01:12:11 +0000 (25595bc)
@@ -35,6 +35,7 @@
 #endif
 #include <sql_select.h>
 #include <ft_global.h>
+#include <mysql.h>
 #include <pthread.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -56,6 +57,7 @@ _ft_vft mrn_ft_vft = {
   mrn_ft_get_relevance,
   NULL // mrn_ft_reinit_search
 };
+handlerton *mrn_hton_ptr;
 
 /* status */
 st_mrn_statuses mrn_status_vals;
@@ -122,6 +124,31 @@ struct st_mysql_sys_var *mrn_system_variables[] =
   NULL
 };
 
+/* UDF - last_insert_grn_id() */
+my_bool last_insert_grn_id_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
+{
+  if (args->arg_count != 0) {
+    strcpy(message, "last_insert_grn_id must not have arguments");
+    return 1;
+  }
+  initid->maybe_null = 0;
+  return 0;
+}
+
+int last_insert_grn_id(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
+{
+  THD *thd = current_thd;
+  st_mrn_slot_data *slot_data = (st_mrn_slot_data*) thd_get_ha_data(thd, mrn_hton_ptr);
+  if (slot_data == NULL) {
+    return 0;
+  }
+  int last_insert_rid = (int) slot_data->last_insert_rid;
+  return last_insert_rid;
+}
+
+void last_insert_grn_id_deinit(UDF_INIT *initid)
+{
+}
 
 /* Groonga information schema */
 int GROONGA_VERSION_SHORT = 0x0001;
@@ -239,6 +266,8 @@ int mrn_init(void *p)
   hton->create = mrn_handler_create;
   hton->flags = 0;
   hton->drop_database = mrn_drop_db;
+  hton->close_connection = mrn_close_connection;
+  mrn_hton_ptr = hton;
 
   // init groonga
   if (grn_init() != GRN_SUCCESS) {
@@ -329,6 +358,14 @@ void mrn_drop_db(handlerton *hton, char *path)
   grn_ctx_fin(ctx);
 }
 
+int mrn_close_connection(handlerton *hton, THD *thd)
+{
+  void *p = thd_get_ha_data(thd, mrn_hton_ptr);
+  if (p) free(p);
+  thd_set_ha_data(thd, hton, NULL);
+  return 0;
+} 
+
 grn_builtin_type mrn_get_type(grn_ctx *ctx, int mysql_field_type)
 {
   switch (mysql_field_type) {
@@ -1350,6 +1387,15 @@ int ha_mroonga::write_row(uchar *buf)
   dbug_tmp_restore_column_map(table->read_set, tmp_map);
 #endif
   grn_obj_unlink(ctx, &colbuf);
+
+  // for UDF last_insert_grn_id()
+  st_mrn_slot_data *slot_data = (st_mrn_slot_data*) thd_get_ha_data(thd, mrn_hton_ptr);
+  if (slot_data == NULL) {
+    slot_data = (st_mrn_slot_data*) malloc(sizeof(st_mrn_slot_data)); 
+  }
+  slot_data->last_insert_rid = row_id;
+  thd_set_ha_data(thd, mrn_hton_ptr, slot_data);
+
   DBUG_RETURN(0);
 }
 

  Modified: ha_mroonga.h (+6 -0)
===================================================================
--- ha_mroonga.h    2010-11-19 02:17:14 +0000 (1df9d82)
+++ ha_mroonga.h    2010-11-20 01:12:11 +0000 (daa00ee)
@@ -45,11 +45,17 @@ struct st_mrn_ft_info
   grn_id rid;
 };
 
+struct st_mrn_slot_data
+{
+  grn_id last_insert_rid;
+};
+
 /* functions */
 int mrn_init(void *hton);
 int mrn_deinit(void *hton);
 handler *mrn_handler_create(handlerton *hton, TABLE_SHARE *share, MEM_ROOT *root);
 void mrn_drop_db(handlerton *hton, char *path);
+int mrn_close_connection(handlerton *hton, THD *thd);
 float mrn_ft_find_relevance(FT_INFO *handler, uchar *record, uint length);
 float mrn_ft_get_relevance(FT_INFO *handler);
 void mrn_ft_close_search(FT_INFO *handler);

  Added: test/sql/r/last_insert_grn_id.result (+33 -0) 100644
===================================================================
--- /dev/null
+++ test/sql/r/last_insert_grn_id.result    2010-11-20 01:12:11 +0000 (23572b0)
@@ -0,0 +1,33 @@
+install plugin groonga soname 'ha_groonga.so';
+set storage_engine=groonga;
+drop table if exists t1;
+create function last_insert_grn_id returns integer soname 'ha_groonga.so';
+create table t1 (_id int, c1 int);
+select last_insert_grn_id();
+last_insert_grn_id()
+0
+insert into t1 values(null,100);
+insert into t1 values(null,100);
+select last_insert_grn_id();
+last_insert_grn_id()
+2
+insert into t1 values(null,100);
+insert into t1 values(null,100);
+select last_insert_grn_id();
+last_insert_grn_id()
+4
+insert into t1 values(null,100);
+insert into t1 values(null,100);
+select last_insert_grn_id();
+last_insert_grn_id()
+6
+insert into t1 values(null,100);
+insert into t1 values(null,100);
+select last_insert_grn_id();
+last_insert_grn_id()
+8
+select last_insert_grn_id(1);
+ERROR HY000: Can't initialize function 'last_insert_grn_id'; last_insert_grn_id must not have arguments
+drop table t1;
+drop function last_insert_grn_id;
+uninstall plugin groonga;

  Added: test/sql/t/last_insert_grn_id.test (+49 -0) 100644
===================================================================
--- /dev/null
+++ test/sql/t/last_insert_grn_id.test    2010-11-20 01:12:11 +0000 (4d47d54)
@@ -0,0 +1,49 @@
+# Copyright(C) 2010 Tetsuro IKEDA
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library 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
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+--disable_warnings
+install plugin groonga soname 'ha_groonga.so';
+set storage_engine=groonga;
+drop table if exists t1;
+--enable_warnings
+
+create function last_insert_grn_id returns integer soname 'ha_groonga.so';
+
+create table t1 (_id int, c1 int);
+select last_insert_grn_id();
+insert into t1 values(null,100);
+insert into t1 values(null,100);
+select last_insert_grn_id();
+insert into t1 values(null,100);
+insert into t1 values(null,100);
+select last_insert_grn_id();
+insert into t1 values(null,100);
+insert into t1 values(null,100);
+select last_insert_grn_id();
+insert into t1 values(null,100);
+insert into t1 values(null,100);
+select last_insert_grn_id();
+
+--error 1123
+select last_insert_grn_id(1);
+
+drop table t1;
+
+drop function last_insert_grn_id;
+
+--disable_warnings
+uninstall plugin groonga;
+--enable_warnings




Groonga-mysql-commit メーリングリストの案内
Back to archive index