diff options
Diffstat (limited to 'sql/sql_udf.cc')
-rw-r--r-- | sql/sql_udf.cc | 119 |
1 files changed, 62 insertions, 57 deletions
diff --git a/sql/sql_udf.cc b/sql/sql_udf.cc index 431f8a13d28..a4461beed29 100644 --- a/sql/sql_udf.cc +++ b/sql/sql_udf.cc @@ -33,6 +33,7 @@ #endif #include "mysql_priv.h" +#include <my_pthread.h> #ifdef HAVE_DLOPEN extern "C" @@ -70,10 +71,10 @@ extern "C" static bool initialized = 0; static MEM_ROOT mem; static HASH udf_hash; -static pthread_mutex_t THR_LOCK_udf; +static rw_lock_t THR_LOCK_udf; -static udf_func *add_udf(char *name, Item_result ret, char *dl, +static udf_func *add_udf(LEX_STRING *name, Item_result ret, char *dl, Item_udftype typ); static void del_udf(udf_func *udf); static void *find_udf_dl(const char *dl); @@ -83,18 +84,21 @@ static void init_syms(udf_func *tmp) { char nm[MAX_FIELD_NAME+16],*end; - tmp->func = dlsym(tmp->dlhandle, tmp->name); - end=strmov(nm,tmp->name); + tmp->func = dlsym(tmp->dlhandle, tmp->name.str); + end=strmov(nm,tmp->name.str); (void) strmov(end,"_init"); tmp->func_init = dlsym(tmp->dlhandle, nm); (void) strmov(end,"_deinit"); tmp->func_deinit = dlsym(tmp->dlhandle, nm); if (tmp->type == UDFTYPE_AGGREGATE) { - (void)strmov( end, "_reset" ); - tmp->func_reset = dlsym( tmp->dlhandle, nm ); + (void)strmov( end, "_clear" ); + tmp->func_clear = dlsym( tmp->dlhandle, nm ); (void)strmov( end, "_add" ); tmp->func_add = dlsym( tmp->dlhandle, nm ); + /* Give error if _clear and _add doesn't exists */ + if (!tmp->func_clear || ! tmp->func_add) + tmp->func= 0; } } @@ -102,8 +106,8 @@ extern "C" byte* get_hash_key(const byte *buff,uint *length, my_bool not_used __attribute__((unused))) { udf_func *udf=(udf_func*) buff; - *length=(uint) udf->name_length; - return (byte*) udf->name; + *length=(uint) udf->name.length; + return (byte*) udf->name.str; } /* @@ -123,12 +127,12 @@ void udf_init() if (initialized) DBUG_VOID_RETURN; - pthread_mutex_init(&THR_LOCK_udf,MY_MUTEX_INIT_SLOW); - + my_rwlock_init(&THR_LOCK_udf,NULL); + init_sql_alloc(&mem, UDF_ALLOC_BLOCK_SIZE, 0); THD *new_thd = new THD; if (!new_thd || - hash_init(&udf_hash,32,0,0,get_hash_key, NULL, HASH_CASE_INSENSITIVE)) + hash_init(&udf_hash,system_charset_info,32,0,0,get_hash_key, NULL, 0)) { sql_print_error("Can't allocate memory for udf structures"); hash_free(&udf_hash); @@ -146,7 +150,7 @@ void udf_init() tables.lock_type = TL_READ; tables.db=new_thd->db; - if (open_and_lock_tables(new_thd, &tables)) + if (simple_open_n_lock_tables(new_thd, &tables)) { DBUG_PRINT("error",("Can't open udf table")); sql_print_error("Can't open the mysql/func table. Please run the mysql_install_db script to create it."); @@ -158,14 +162,16 @@ void udf_init() while (!(error = read_record_info.read_record(&read_record_info))) { DBUG_PRINT("info",("init udf record")); - char *name=get_field(&mem, table, 0); - char *dl_name= get_field(&mem, table, 2); + LEX_STRING name; + name.str=get_field(&mem, table->field[0]); + name.length = strlen(name.str); + char *dl_name= get_field(&mem, table->field[2]); bool new_dl=0; Item_udftype udftype=UDFTYPE_FUNCTION; if (table->fields >= 4) // New func table udftype=(Item_udftype) table->field[3]->val_int(); - if (!(tmp = add_udf(name,(Item_result) table->field[1]->val_int(), + if (!(tmp = add_udf(&name,(Item_result) table->field[1]->val_int(), dl_name, udftype))) { sql_print_error("Can't alloc memory for udf function: name"); @@ -233,7 +239,7 @@ void udf_free() if (initialized) { initialized= 0; - pthread_mutex_destroy(&THR_LOCK_udf); + rwlock_destroy(&THR_LOCK_udf); } DBUG_VOID_RETURN; } @@ -254,10 +260,10 @@ static void del_udf(udf_func *udf) The functions will be automaticly removed when the least threads doesn't use it anymore */ - char *name= udf->name; - uint name_length=udf->name_length; - udf->name=(char*) "*"; - udf->name_length=1; + char *name= udf->name.str; + uint name_length=udf->name.length; + udf->name.str=(char*) "*"; + udf->name.length=1; hash_update(&udf_hash,(byte*) udf,(byte*) name,name_length); } DBUG_VOID_RETURN; @@ -267,7 +273,7 @@ static void del_udf(udf_func *udf) void free_udf(udf_func *udf) { DBUG_ENTER("free_udf"); - pthread_mutex_lock(&THR_LOCK_udf); + rw_wrlock(&THR_LOCK_udf); if (!--udf->usage_count) { /* @@ -279,7 +285,7 @@ void free_udf(udf_func *udf) if (!find_udf_dl(udf->dl)) dlclose(udf->dlhandle); } - pthread_mutex_unlock(&THR_LOCK_udf); + rw_unlock(&THR_LOCK_udf); DBUG_VOID_RETURN; } @@ -292,7 +298,7 @@ udf_func *find_udf(const char *name,uint length,bool mark_used) DBUG_ENTER("find_udf"); /* TODO: This should be changed to reader locks someday! */ - pthread_mutex_lock(&THR_LOCK_udf); + rw_rdlock(&THR_LOCK_udf); if ((udf=(udf_func*) hash_search(&udf_hash,(byte*) name, length ? length : (uint) strlen(name)))) { @@ -301,7 +307,7 @@ udf_func *find_udf(const char *name,uint length,bool mark_used) else if (mark_used) udf->usage_count++; } - pthread_mutex_unlock(&THR_LOCK_udf); + rw_unlock(&THR_LOCK_udf); DBUG_RETURN(udf); } @@ -326,7 +332,7 @@ static void *find_udf_dl(const char *dl) /* Assume that name && dl is already allocated */ -static udf_func *add_udf(char *name, Item_result ret, char *dl, +static udf_func *add_udf(LEX_STRING *name, Item_result ret, char *dl, Item_udftype type) { if (!name || !dl || !(uint) type || (uint) type > (uint) UDFTYPE_AGGREGATE) @@ -335,13 +341,12 @@ static udf_func *add_udf(char *name, Item_result ret, char *dl, if (!tmp) return 0; bzero((char*) tmp,sizeof(*tmp)); - tmp->name = name; - tmp->name_length=(uint) strlen(tmp->name); + tmp->name = *name; //dup !! tmp->dl = dl; tmp->returns = ret; tmp->type = type; tmp->usage_count=1; - if (hash_insert(&udf_hash,(byte*) tmp)) + if (my_hash_insert(&udf_hash,(byte*) tmp)) return 0; using_udf_functions=1; return tmp; @@ -360,7 +365,7 @@ int mysql_create_function(THD *thd,udf_func *udf) if (!initialized) { - send_error(&thd->net, ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES)); + send_error(thd, ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES)); DBUG_RETURN(1); } @@ -371,19 +376,19 @@ int mysql_create_function(THD *thd,udf_func *udf) */ if (strchr(udf->dl, '/')) { - send_error(&thd->net, ER_UDF_NO_PATHS,ER(ER_UDF_NO_PATHS)); + send_error(thd, ER_UDF_NO_PATHS,ER(ER_UDF_NO_PATHS)); DBUG_RETURN(1); } - if (udf->name_length > NAME_LEN) + if (udf->name.length > NAME_LEN) { - net_printf(&thd->net, ER_TOO_LONG_IDENT,udf->name); + net_printf(thd, ER_TOO_LONG_IDENT,udf->name); DBUG_RETURN(1); } - pthread_mutex_lock(&THR_LOCK_udf); - if ((hash_search(&udf_hash,(byte*) udf->name, udf->name_length))) + rw_wrlock(&THR_LOCK_udf); + if ((hash_search(&udf_hash,(byte*) udf->name.str, udf->name.length))) { - net_printf(&thd->net, ER_UDF_EXISTS, udf->name); + net_printf(thd, ER_UDF_EXISTS, udf->name); goto err; } if (!(dl = find_udf_dl(udf->dl))) @@ -392,7 +397,7 @@ int mysql_create_function(THD *thd,udf_func *udf) { DBUG_PRINT("error",("dlopen of %s failed, error: %d (%s)", udf->dl,errno,dlerror())); - net_printf(&thd->net, ER_CANT_OPEN_LIBRARY, udf->dl, errno, dlerror()); + net_printf(thd, ER_CANT_OPEN_LIBRARY, udf->dl, errno, dlerror()); goto err; } new_dl=1; @@ -402,21 +407,21 @@ int mysql_create_function(THD *thd,udf_func *udf) if (udf->func == NULL) { - net_printf(&thd->net, ER_CANT_FIND_DL_ENTRY, udf->name); + net_printf(thd, ER_CANT_FIND_DL_ENTRY, udf->name); goto err; } - udf->name=strdup_root(&mem,udf->name); + udf->name.str=strdup_root(&mem,udf->name.str); udf->dl=strdup_root(&mem,udf->dl); - if (!(u_d=add_udf(udf->name,udf->returns,udf->dl,udf->type))) + if (!(u_d=add_udf(&udf->name,udf->returns,udf->dl,udf->type))) { - send_error(&thd->net,0); // End of memory + send_error(thd,0); // End of memory goto err; } u_d->dlhandle = dl; u_d->func=udf->func; u_d->func_init=udf->func_init; u_d->func_deinit=udf->func_deinit; - u_d->func_reset=udf->func_reset; + u_d->func_clear=udf->func_clear; u_d->func_add=udf->func_add; /* create entry in mysql/func table */ @@ -428,10 +433,10 @@ int mysql_create_function(THD *thd,udf_func *udf) if (!(table = open_ltable(thd,&tables,TL_WRITE))) goto err; - restore_record(table,2); // Get default values for fields - table->field[0]->store(u_d->name, u_d->name_length); + restore_record(table,default_values); // Default values for fields + table->field[0]->store(u_d->name.str, u_d->name.length, system_charset_info); table->field[1]->store((longlong) u_d->returns); - table->field[2]->store(u_d->dl,(uint) strlen(u_d->dl)); + table->field[2]->store(u_d->dl,(uint) strlen(u_d->dl), system_charset_info); if (table->fields >= 4) // If not old func format table->field[3]->store((longlong) u_d->type); error = table->file->write_row(table->record[0]); @@ -439,22 +444,22 @@ int mysql_create_function(THD *thd,udf_func *udf) close_thread_tables(thd); if (error) { - net_printf(&thd->net, ER_ERROR_ON_WRITE, "func@mysql",error); + net_printf(thd, ER_ERROR_ON_WRITE, "func@mysql",error); del_udf(u_d); goto err; } - pthread_mutex_unlock(&THR_LOCK_udf); + rw_unlock(&THR_LOCK_udf); DBUG_RETURN(0); err: if (new_dl) dlclose(dl); - pthread_mutex_unlock(&THR_LOCK_udf); + rw_unlock(&THR_LOCK_udf); DBUG_RETURN(1); } -int mysql_drop_function(THD *thd,const char *udf_name) +int mysql_drop_function(THD *thd,const LEX_STRING *udf_name) { TABLE *table; TABLE_LIST tables; @@ -462,14 +467,14 @@ int mysql_drop_function(THD *thd,const char *udf_name) DBUG_ENTER("mysql_drop_function"); if (!initialized) { - send_error(&thd->net, ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES)); + send_error(thd, ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES)); DBUG_RETURN(1); } - pthread_mutex_lock(&THR_LOCK_udf); - if (!(udf=(udf_func*) hash_search(&udf_hash,(byte*) udf_name, - (uint) strlen(udf_name)))) + rw_wrlock(&THR_LOCK_udf); + if (!(udf=(udf_func*) hash_search(&udf_hash,(byte*) udf_name->str, + (uint) udf_name->length))) { - net_printf(&thd->net, ER_FUNCTION_NOT_DEFINED, udf_name); + net_printf(thd, ER_FUNCTION_NOT_DEFINED, udf_name); goto err; } del_udf(udf); @@ -485,8 +490,8 @@ int mysql_drop_function(THD *thd,const char *udf_name) tables.real_name= tables.alias= (char*) "func"; if (!(table = open_ltable(thd,&tables,TL_WRITE))) goto err; - if (!table->file->index_read_idx(table->record[0],0,(byte*) udf_name, - (uint) strlen(udf_name), + if (!table->file->index_read_idx(table->record[0],0,(byte*) udf_name->str, + (uint) udf_name->length, HA_READ_KEY_EXACT)) { int error; @@ -495,10 +500,10 @@ int mysql_drop_function(THD *thd,const char *udf_name) } close_thread_tables(thd); - pthread_mutex_unlock(&THR_LOCK_udf); + rw_unlock(&THR_LOCK_udf); DBUG_RETURN(0); err: - pthread_mutex_unlock(&THR_LOCK_udf); + rw_unlock(&THR_LOCK_udf); DBUG_RETURN(1); } |