/
00_hash_filo.patch
263 lines (250 loc) · 7.44 KB
/
00_hash_filo.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
diff -Nur mysql-5.0.25-nightly-20060823.orig/sql/hash_filo.cc mysql-5.0.25-nightly-20060823.host_cache00/sql/hash_filo.cc
--- mysql-5.0.25-nightly-20060823.orig/sql/hash_filo.cc 2006-08-22 23:09:16.000000000 -0700
+++ mysql-5.0.25-nightly-20060823.host_cache00/sql/hash_filo.cc 2006-09-25 20:30:32.000000000 -0700
@@ -16,8 +16,8 @@
/*
-** A class for static sized hash tables where old entries are deleted according
-** to usage.
+ A class for statically-sized hash tables, where entries maintained in
+ least recently used order using a doubly-linked list for easy traversal.
*/
#ifdef USE_PRAGMA_IMPLEMENTATION
@@ -26,3 +26,107 @@
#include "mysql_priv.h"
#include "hash_filo.h"
+
+hash_filo::hash_filo(uint size_arg, uint key_offset_arg, uint key_length_arg,
+ hash_get_key get_key_arg, hash_free_key free_element_arg,
+ CHARSET_INFO *hash_charset_arg)
+ :size(size_arg), key_offset(key_offset_arg), key_length(key_length_arg),
+ get_key(get_key_arg), free_element(free_element_arg), init(0),
+ hash_charset(hash_charset_arg)
+{
+ bzero((char*) &cache, sizeof(cache));
+}
+
+hash_filo::~hash_filo()
+{
+ if (init)
+ {
+ if (cache.array.buffer) /* Avoid problems with thread library */
+ (void) hash_free(&cache);
+ pthread_mutex_destroy(&lock);
+ }
+}
+
+void hash_filo::clear(bool locked)
+{
+ if (!init)
+ {
+ init=1;
+ (void) pthread_mutex_init(&lock, MY_MUTEX_INIT_FAST);
+ }
+ if (!locked)
+ (void) pthread_mutex_lock(&lock);
+ (void) hash_free(&cache);
+ (void) hash_init(&cache, hash_charset, size, key_offset,
+ key_length, get_key, free_element, 0);
+ if (!locked)
+ (void) pthread_mutex_unlock(&lock);
+ newest_element_p= oldest_element_p= NULL;
+}
+
+void hash_filo::resize(uint new_size)
+{
+ (void) pthread_mutex_lock(&lock);
+ size= new_size;
+ clear(1);
+ (void) pthread_mutex_unlock(&lock);
+}
+
+hash_filo_element *hash_filo::search(gptr key, uint length)
+{
+ hash_filo_element *entry= (hash_filo_element*) hash_search(&cache,
+ (byte*) key,
+ length);
+ if (entry)
+ { // Found; link it first
+ if (entry != newest_element_p)
+ { // Relink used-chain
+ if (entry == oldest_element_p)
+ oldest_element_p= entry->newer_element_p;
+ else {
+ entry->older_element_p->newer_element_p= entry->newer_element_p;
+ entry->newer_element_p->older_element_p= entry->older_element_p;
+ }
+ if ((entry->older_element_p= newest_element_p))
+ newest_element_p->newer_element_p= entry;
+ newest_element_p= entry;
+ }
+ }
+ return entry;
+}
+
+my_bool hash_filo::add(hash_filo_element *entry)
+{
+ if(unlikely(size == 0)) {
+ /* Our HASH size is 0, so throw away any entries attempted. */
+ if(free_element)
+ (*free_element)(entry);
+ return 0;
+ }
+
+ if (cache.records == size)
+ {
+ hash_filo_element *tmp= oldest_element_p;
+
+ if((oldest_element_p= oldest_element_p->newer_element_p))
+ oldest_element_p->older_element_p= NULL;
+ else
+ newest_element_p = NULL;
+
+ hash_delete(&cache, (byte*) tmp);
+ }
+
+ if (my_hash_insert(&cache, (byte*) entry))
+ {
+ if (free_element)
+ (*free_element)(entry); // This should never happen
+ return 1;
+ }
+
+ if ((entry->older_element_p= newest_element_p))
+ newest_element_p->newer_element_p= entry;
+ else
+ oldest_element_p= entry;
+ newest_element_p= entry;
+ return 0;
+}
diff -Nur mysql-5.0.25-nightly-20060823.orig/sql/hash_filo.h mysql-5.0.25-nightly-20060823.host_cache00/sql/hash_filo.h
--- mysql-5.0.25-nightly-20060823.orig/sql/hash_filo.h 2006-08-22 23:09:19.000000000 -0700
+++ mysql-5.0.25-nightly-20060823.host_cache00/sql/hash_filo.h 2006-09-25 20:30:32.000000000 -0700
@@ -16,8 +16,8 @@
/*
-** A class for static sized hash tables where old entries are deleted in
-** first-in-last-out to usage.
+ A class for statically-sized hash tables, where entries maintained in
+ least recently used order using a doubly-linked list for easy traversal.
*/
#ifndef HASH_FILO_H
@@ -29,106 +29,43 @@
class hash_filo_element
{
- hash_filo_element *next_used,*prev_used;
+ hash_filo_element *newer_element_p, *older_element_p;
public:
hash_filo_element() {}
+ hash_filo_element *newer_element() { return newer_element_p; }
+ hash_filo_element *older_element() { return older_element_p; }
friend class hash_filo;
};
class hash_filo
{
- const uint size, key_offset, key_length;
+ uint size;
+ const uint key_offset, key_length;
const hash_get_key get_key;
hash_free_key free_element;
bool init;
CHARSET_INFO *hash_charset;
- hash_filo_element *first_link,*last_link;
+ hash_filo_element *newest_element_p, *oldest_element_p;
+
public:
pthread_mutex_t lock;
HASH cache;
- hash_filo(uint size_arg, uint key_offset_arg , uint key_length_arg,
- hash_get_key get_key_arg, hash_free_key free_element_arg,
- CHARSET_INFO *hash_charset_arg)
- :size(size_arg), key_offset(key_offset_arg), key_length(key_length_arg),
- get_key(get_key_arg), free_element(free_element_arg),init(0),
- hash_charset(hash_charset_arg)
- {
- bzero((char*) &cache,sizeof(cache));
- }
-
- ~hash_filo()
- {
- if (init)
- {
- if (cache.array.buffer) /* Avoid problems with thread library */
- (void) hash_free(&cache);
- pthread_mutex_destroy(&lock);
- }
- }
- void clear(bool locked=0)
- {
- if (!init)
- {
- init=1;
- (void) pthread_mutex_init(&lock,MY_MUTEX_INIT_FAST);
- }
- if (!locked)
- (void) pthread_mutex_lock(&lock);
- (void) hash_free(&cache);
- (void) hash_init(&cache,hash_charset,size,key_offset,
- key_length, get_key, free_element,0);
- if (!locked)
- (void) pthread_mutex_unlock(&lock);
- first_link=last_link=0;
- }
-
- hash_filo_element *search(gptr key,uint length)
- {
- hash_filo_element *entry=(hash_filo_element*)
- hash_search(&cache,(byte*) key,length);
- if (entry)
- { // Found; link it first
- if (entry != first_link)
- { // Relink used-chain
- if (entry == last_link)
- last_link=entry->prev_used;
- else
- {
- entry->next_used->prev_used = entry->prev_used;
- entry->prev_used->next_used = entry->next_used;
- }
- if ((entry->next_used= first_link))
- first_link->prev_used=entry;
- first_link=entry;
- }
- }
- return entry;
- }
-
- my_bool add(hash_filo_element *entry)
- {
- if (cache.records == size)
- {
- hash_filo_element *tmp=last_link;
- last_link=last_link->prev_used;
- hash_delete(&cache,(byte*) tmp);
- }
- if (my_hash_insert(&cache,(byte*) entry))
- {
- if (free_element)
- (*free_element)(entry); // This should never happen
- return 1;
- }
- if ((entry->next_used=first_link))
- first_link->prev_used=entry;
- else
- last_link=entry;
- first_link=entry;
- return 0;
- }
+ hash_filo(uint size_arg, uint key_offset_arg, uint key_length_arg,
+ hash_get_key get_key_arg, hash_free_key free_element_arg,
+ CHARSET_INFO *hash_charset_arg);
+ ~hash_filo();
+
+ hash_filo_element *newest_element() { return newest_element_p; }
+ hash_filo_element *oldest_element() { return oldest_element_p; }
+
+ void clear(bool locked=0);
+ void resize(uint new_size);
+
+ hash_filo_element *search(gptr key, uint length);
+ my_bool add(hash_filo_element *entry);
};
#endif