~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/innobase/ha/ha0ha.c

  • Committer: Brian Aker
  • Date: 2008-09-04 19:31:00 UTC
  • Revision ID: brian@tangent.org-20080904193100-l849hgghfy4urj43
Changing default character set from this point on.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
#include "ha0ha.ic"
12
12
#endif
13
13
 
14
 
#ifdef UNIV_DEBUG
15
 
# include "buf0buf.h"
16
 
#endif /* UNIV_DEBUG */
17
 
#ifdef UNIV_SYNC_DEBUG
18
 
# include "btr0sea.h"
19
 
#endif /* UNIV_SYNC_DEBUG */
20
 
#include "page0page.h"
 
14
#include "buf0buf.h"
21
15
 
22
16
/*****************************************************************
23
17
Creates a hash table with >= n array cells. The actual number of cells is
24
18
chosen to be a prime number slightly bigger than n. */
25
 
UNIV_INTERN
 
19
 
26
20
hash_table_t*
27
21
ha_create_func(
28
22
/*===========*/
29
23
                                /* out, own: created table */
 
24
        ibool   in_btr_search,  /* in: TRUE if the hash table is used in
 
25
                                the btr_search module */
30
26
        ulint   n,              /* in: number of array cells */
31
27
#ifdef UNIV_SYNC_DEBUG
32
28
        ulint   mutex_level,    /* in: level of the mutexes in the latching
40
36
 
41
37
        table = hash_create(n);
42
38
 
43
 
#ifdef UNIV_DEBUG
44
 
        table->adaptive = TRUE;
45
 
#endif /* UNIV_DEBUG */
 
39
        if (in_btr_search) {
 
40
                table->adaptive = TRUE;
 
41
        } else {
 
42
                table->adaptive = FALSE;
 
43
        }
 
44
 
46
45
        /* Creating MEM_HEAP_BTR_SEARCH type heaps can potentially fail,
47
46
        but in practise it never should in this case, hence the asserts. */
48
47
 
49
48
        if (n_mutexes == 0) {
50
 
                table->heap = mem_heap_create_in_btr_search(
51
 
                        ut_min(4096, MEM_MAX_ALLOC_IN_BUF));
52
 
                ut_a(table->heap);
 
49
                if (in_btr_search) {
 
50
                        table->heap = mem_heap_create_in_btr_search(4096);
 
51
                        ut_a(table->heap);
 
52
                } else {
 
53
                        table->heap = mem_heap_create_in_buffer(4096);
 
54
                }
53
55
 
54
56
                return(table);
55
57
        }
59
61
        table->heaps = mem_alloc(n_mutexes * sizeof(void*));
60
62
 
61
63
        for (i = 0; i < n_mutexes; i++) {
62
 
                table->heaps[i] = mem_heap_create_in_btr_search(4096);
63
 
                ut_a(table->heaps[i]);
 
64
                if (in_btr_search) {
 
65
                        table->heaps[i] = mem_heap_create_in_btr_search(4096);
 
66
                        ut_a(table->heaps[i]);
 
67
                } else {
 
68
                        table->heaps[i] = mem_heap_create_in_buffer(4096);
 
69
                }
64
70
        }
65
71
 
66
72
        return(table);
67
73
}
68
74
 
69
75
/*****************************************************************
70
 
Empties a hash table and frees the memory heaps. */
71
 
UNIV_INTERN
72
 
void
73
 
ha_clear(
74
 
/*=====*/
75
 
        hash_table_t*   table)  /* in, own: hash table */
76
 
{
77
 
        ulint   i;
78
 
        ulint   n;
79
 
 
80
 
#ifdef UNIV_SYNC_DEBUG
81
 
        ut_ad(rw_lock_own(&btr_search_latch, RW_LOCK_EXCLUSIVE));
82
 
#endif /* UNIV_SYNC_DEBUG */
83
 
 
84
 
        /* Free the memory heaps. */
85
 
        n = table->n_mutexes;
86
 
 
87
 
        for (i = 0; i < n; i++) {
88
 
                mem_heap_free(table->heaps[i]);
89
 
        }
90
 
 
91
 
        /* Clear the hash table. */
92
 
        n = hash_get_n_cells(table);
93
 
 
94
 
        for (i = 0; i < n; i++) {
95
 
                hash_get_nth_cell(table, i)->node = NULL;
96
 
        }
97
 
}
98
 
 
99
 
/*****************************************************************
100
76
Inserts an entry into a hash table. If an entry with the same fold number
101
77
is found, its node is updated to point to the new data, and no new node
102
78
is inserted. */
103
 
UNIV_INTERN
 
79
 
104
80
ibool
105
 
ha_insert_for_fold_func(
106
 
/*====================*/
 
81
ha_insert_for_fold(
 
82
/*===============*/
107
83
                                /* out: TRUE if succeed, FALSE if no more
108
84
                                memory could be allocated */
109
85
        hash_table_t*   table,  /* in: hash table */
111
87
                                the same fold value already exists, it is
112
88
                                updated to point to the same data, and no new
113
89
                                node is created! */
114
 
#ifdef UNIV_DEBUG
115
 
        buf_block_t*    block,  /* in: buffer block containing the data */
116
 
#endif /* UNIV_DEBUG */
117
90
        void*           data)   /* in: data, must not be NULL */
118
91
{
119
92
        hash_cell_t*    cell;
120
93
        ha_node_t*      node;
121
94
        ha_node_t*      prev_node;
 
95
        buf_block_t*    prev_block;
122
96
        ulint           hash;
123
97
 
124
98
        ut_ad(table && data);
125
 
        ut_ad(block->frame == page_align(data));
126
99
        ut_ad(!table->mutexes || mutex_own(hash_get_mutex(table, fold)));
127
100
 
128
101
        hash = hash_calc_hash(fold, table);
133
106
 
134
107
        while (prev_node != NULL) {
135
108
                if (prev_node->fold == fold) {
136
 
#ifdef UNIV_DEBUG
137
109
                        if (table->adaptive) {
138
 
                                buf_block_t* prev_block = prev_node->block;
139
 
                                ut_a(prev_block->frame
140
 
                                     == page_align(prev_node->data));
 
110
                                prev_block = buf_block_align(prev_node->data);
141
111
                                ut_a(prev_block->n_pointers > 0);
142
112
                                prev_block->n_pointers--;
143
 
                                block->n_pointers++;
 
113
                                buf_block_align(data)->n_pointers++;
144
114
                        }
145
115
 
146
 
                        prev_node->block = block;
147
 
#endif /* UNIV_DEBUG */
148
116
                        prev_node->data = data;
149
117
 
150
118
                        return(TRUE);
166
134
                return(FALSE);
167
135
        }
168
136
 
169
 
        ha_node_set_data(node, block, data);
 
137
        ha_node_set_data(node, data);
170
138
 
171
 
#ifdef UNIV_DEBUG
172
139
        if (table->adaptive) {
173
 
                block->n_pointers++;
 
140
                buf_block_align(data)->n_pointers++;
174
141
        }
175
 
#endif /* UNIV_DEBUG */
 
142
 
176
143
        node->fold = fold;
177
144
 
178
145
        node->next = NULL;
198
165
 
199
166
/***************************************************************
200
167
Deletes a hash node. */
201
 
UNIV_INTERN
 
168
 
202
169
void
203
170
ha_delete_hash_node(
204
171
/*================*/
205
172
        hash_table_t*   table,          /* in: hash table */
206
173
        ha_node_t*      del_node)       /* in: node to be deleted */
207
174
{
208
 
#ifdef UNIV_DEBUG
209
175
        if (table->adaptive) {
210
 
                ut_a(del_node->block->frame = page_align(del_node->data));
211
 
                ut_a(del_node->block->n_pointers > 0);
212
 
                del_node->block->n_pointers--;
 
176
                ut_a(buf_block_align(del_node->data)->n_pointers > 0);
 
177
                buf_block_align(del_node->data)->n_pointers--;
213
178
        }
214
 
#endif /* UNIV_DEBUG */
 
179
 
215
180
        HASH_DELETE_AND_COMPACT(ha_node_t, next, table, del_node);
216
181
}
217
182
 
218
183
/*****************************************************************
219
184
Deletes an entry from a hash table. */
220
 
UNIV_INTERN
 
185
 
221
186
void
222
187
ha_delete(
223
188
/*======*/
240
205
/*************************************************************
241
206
Looks for an element when we know the pointer to the data, and updates
242
207
the pointer to data, if found. */
243
 
UNIV_INTERN
 
208
 
244
209
void
245
 
ha_search_and_update_if_found_func(
246
 
/*===============================*/
 
210
ha_search_and_update_if_found(
 
211
/*==========================*/
247
212
        hash_table_t*   table,  /* in: hash table */
248
213
        ulint           fold,   /* in: folded value of the searched data */
249
214
        void*           data,   /* in: pointer to the data */
250
 
#ifdef UNIV_DEBUG
251
 
        buf_block_t*    new_block,/* in: block containing new_data */
252
 
#endif
253
215
        void*           new_data)/* in: new pointer to the data */
254
216
{
255
217
        ha_node_t*      node;
256
218
 
257
219
        ut_ad(!table->mutexes || mutex_own(hash_get_mutex(table, fold)));
258
 
        ut_ad(new_block->frame == page_align(new_data));
259
220
 
260
221
        node = ha_search_with_data(table, fold, data);
261
222
 
262
223
        if (node) {
263
 
#ifdef UNIV_DEBUG
264
224
                if (table->adaptive) {
265
 
                        ut_a(node->block->n_pointers > 0);
266
 
                        node->block->n_pointers--;
267
 
                        new_block->n_pointers++;
 
225
                        ut_a(buf_block_align(node->data)->n_pointers > 0);
 
226
                        buf_block_align(node->data)->n_pointers--;
 
227
                        buf_block_align(new_data)->n_pointers++;
268
228
                }
269
229
 
270
 
                node->block = new_block;
271
 
#endif /* UNIV_DEBUG */
272
230
                node->data = new_data;
273
231
        }
274
232
}
276
234
/*********************************************************************
277
235
Removes from the chain determined by fold all nodes whose data pointer
278
236
points to the page given. */
279
 
UNIV_INTERN
 
237
 
280
238
void
281
239
ha_remove_all_nodes_to_page(
282
240
/*========================*/
283
241
        hash_table_t*   table,  /* in: hash table */
284
242
        ulint           fold,   /* in: fold value */
285
 
        const page_t*   page)   /* in: buffer page */
 
243
        page_t*         page)   /* in: buffer page */
286
244
{
287
245
        ha_node_t*      node;
288
246
 
291
249
        node = ha_chain_get_first(table, fold);
292
250
 
293
251
        while (node) {
294
 
                if (page_align(ha_node_get_data(node)) == page) {
 
252
                if (buf_frame_align(ha_node_get_data(node)) == page) {
295
253
 
296
254
                        /* Remove the hash node */
297
255
 
312
270
        node = ha_chain_get_first(table, fold);
313
271
 
314
272
        while (node) {
315
 
                ut_a(page_align(ha_node_get_data(node)) != page);
 
273
                ut_a(buf_frame_align(ha_node_get_data(node)) != page);
316
274
 
317
275
                node = ha_chain_get_next(node);
318
276
        }
321
279
 
322
280
/*****************************************************************
323
281
Validates a given range of the cells in hash table. */
324
 
UNIV_INTERN
 
282
 
325
283
ibool
326
284
ha_validate(
327
285
/*========*/
366
324
 
367
325
/*****************************************************************
368
326
Prints info of a hash table. */
369
 
UNIV_INTERN
 
327
 
370
328
void
371
329
ha_print_info(
372
330
/*==========*/