1
/* Copyright (C) 2000-2003, 2005 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
18
** A class for static sized hash tables where old entries are deleted in
19
** first-in-last-out to usage.
25
#ifdef USE_PRAGMA_INTERFACE
26
#pragma interface /* gcc class interface */
29
class hash_filo_element
31
hash_filo_element *next_used,*prev_used;
33
hash_filo_element() {}
34
friend class hash_filo;
40
const uint size, key_offset, key_length;
41
const hash_get_key get_key;
42
hash_free_key free_element;
44
CHARSET_INFO *hash_charset;
46
hash_filo_element *first_link,*last_link;
51
hash_filo(uint size_arg, uint key_offset_arg , uint key_length_arg,
52
hash_get_key get_key_arg, hash_free_key free_element_arg,
53
CHARSET_INFO *hash_charset_arg)
54
:size(size_arg), key_offset(key_offset_arg), key_length(key_length_arg),
55
get_key(get_key_arg), free_element(free_element_arg),init(0),
56
hash_charset(hash_charset_arg)
58
bzero((char*) &cache,sizeof(cache));
65
if (cache.array.buffer) /* Avoid problems with thread library */
66
(void) hash_free(&cache);
67
pthread_mutex_destroy(&lock);
70
void clear(bool locked=0)
75
(void) pthread_mutex_init(&lock,MY_MUTEX_INIT_FAST);
78
(void) pthread_mutex_lock(&lock);
79
(void) hash_free(&cache);
80
(void) hash_init(&cache,hash_charset,size,key_offset,
81
key_length, get_key, free_element,0);
83
(void) pthread_mutex_unlock(&lock);
84
first_link=last_link=0;
87
hash_filo_element *search(uchar* key, size_t length)
89
hash_filo_element *entry=(hash_filo_element*)
90
hash_search(&cache,(uchar*) key,length);
92
{ // Found; link it first
93
if (entry != first_link)
94
{ // Relink used-chain
95
if (entry == last_link)
96
last_link=entry->prev_used;
99
entry->next_used->prev_used = entry->prev_used;
100
entry->prev_used->next_used = entry->next_used;
102
if ((entry->next_used= first_link))
103
first_link->prev_used=entry;
110
my_bool add(hash_filo_element *entry)
112
if (cache.records == size)
114
hash_filo_element *tmp=last_link;
115
last_link=last_link->prev_used;
116
hash_delete(&cache,(uchar*) tmp);
118
if (my_hash_insert(&cache,(uchar*) entry))
121
(*free_element)(entry); // This should never happen
124
if ((entry->next_used=first_link))
125
first_link->prev_used=entry;