24 #include "mysys_priv.h"
71 DBUG_ENTER(
"free_assign_entry");
80 my_bool not_used __attribute__((unused)))
82 *length=entry->length;
83 return (uchar*) entry->key;
105 static my_bool safe_hash_init(
SAFE_HASH *hash, uint elements,
106 uchar *default_value)
108 DBUG_ENTER(
"safe_hash");
109 if (my_hash_init(&hash->hash, &my_charset_bin, elements,
110 0, 0, (my_hash_get_key) safe_hash_entry_get,
111 (void (*)(
void*)) safe_hash_entry_free, 0))
113 hash->default_value= 0;
116 my_rwlock_init(&hash->mutex, 0);
117 hash->default_value= default_value;
130 static void safe_hash_free(
SAFE_HASH *hash)
136 if (hash->default_value)
138 my_hash_free(&hash->hash);
139 rwlock_destroy(&hash->mutex);
140 hash->default_value=0;
148 static uchar *safe_hash_search(
SAFE_HASH *hash,
const uchar *key, uint length)
151 DBUG_ENTER(
"safe_hash_search");
152 rw_rdlock(&hash->mutex);
153 result= my_hash_search(&hash->hash, key, length);
154 rw_unlock(&hash->mutex);
156 result= hash->default_value;
159 DBUG_PRINT(
"exit",(
"data: 0x%lx", (
long) result));
184 static my_bool safe_hash_set(
SAFE_HASH *hash,
const uchar *key, uint length,
189 DBUG_ENTER(
"safe_hash_set");
190 DBUG_PRINT(
"enter",(
"key: %.*s data: 0x%lx", length, key, (
long) data));
192 rw_wrlock(&hash->mutex);
195 if (data == hash->default_value)
205 if ((*entry->prev= entry->next))
206 entry->next->prev= entry->prev;
207 my_hash_delete(&hash->hash, (uchar*)
entry);
223 entry->key= (uchar*) (entry +1);
224 memcpy((
char*) entry->key, (
char*) key, length);
225 entry->length= length;
228 if ((entry->next= hash->root))
229 entry->next->prev= &entry->next;
230 entry->prev= &hash->root;
232 if (my_hash_insert(&hash->hash, (uchar*) entry))
242 rw_unlock(&hash->mutex);
262 static void safe_hash_change(
SAFE_HASH *hash, uchar *old_data, uchar *new_data)
265 DBUG_ENTER(
"safe_hash_set");
267 rw_wrlock(&hash->mutex);
269 for (entry= hash->root ; entry ; entry= next)
272 if (entry->data == old_data)
274 if (new_data == hash->default_value)
276 if ((*entry->prev= entry->next))
277 entry->next->prev= entry->prev;
278 my_hash_delete(&hash->hash, (uchar*) entry);
281 entry->data= new_data;
285 rw_unlock(&hash->mutex);
298 my_bool multi_keycache_init(
void)
300 return safe_hash_init(&key_cache_hash, 16, (uchar*) dflt_key_cache);
304 void multi_keycache_free(
void)
306 safe_hash_free(&key_cache_hash);
326 KEY_CACHE *multi_key_cache_search(uchar *key, uint length)
328 if (!key_cache_hash.hash.records)
329 return dflt_key_cache;
330 return (
KEY_CACHE*) safe_hash_search(&key_cache_hash, key, length);
350 my_bool multi_key_cache_set(
const uchar *key, uint length,
353 return safe_hash_set(&key_cache_hash, key, length, (uchar*) key_cache);
357 void multi_key_cache_change(
KEY_CACHE *old_data,
360 safe_hash_change(&key_cache_hash, (uchar*) old_data, (uchar*) new_data);