~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/innobase/include/ha0storage.ic

  • Committer: patrick crews
  • Date: 2010-09-29 15:15:19 UTC
  • mfrom: (1099.4.188 drizzle)
  • Revision ID: gleebix@gmail.com-20100929151519-6mrmzd1ciw2p9nws
Tags: 2010.09.1802
Update translations

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*****************************************************************************
2
 
 
3
 
Copyright (C) 2007, 2009, Innobase Oy. All Rights Reserved.
4
 
 
5
 
This program is free software; you can redistribute it and/or modify it under
6
 
the terms of the GNU General Public License as published by the Free Software
7
 
Foundation; version 2 of the License.
8
 
 
9
 
This program is distributed in the hope that it will be useful, but WITHOUT
10
 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
 
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
 
 
13
 
You should have received a copy of the GNU General Public License along with
14
 
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15
 
St, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
*****************************************************************************/
18
 
 
19
 
/**************************************************//**
20
 
@file include/ha0storage.ic
21
 
Hash storage.
22
 
Provides a data structure that stores chunks of data in
23
 
its own storage, avoiding duplicates.
24
 
 
25
 
Created September 24, 2007 Vasil Dimov
26
 
*******************************************************/
27
 
 
28
 
#include "univ.i"
29
 
#include "ha0storage.h"
30
 
#include "hash0hash.h"
31
 
#include "mem0mem.h"
32
 
 
33
 
/** Hash storage for strings */
34
 
struct ha_storage_struct {
35
 
        mem_heap_t*     heap;   /*!< memory heap from which memory is
36
 
                                allocated */
37
 
        hash_table_t*   hash;   /*!< hash table used to avoid
38
 
                                duplicates */
39
 
};
40
 
 
41
 
/** Objects of this type are stored in ha_storage_t */
42
 
typedef struct ha_storage_node_struct ha_storage_node_t;
43
 
/** Objects of this type are stored in ha_storage_struct */
44
 
struct ha_storage_node_struct {
45
 
        ulint                   data_len;/*!< length of the data */
46
 
        const void*             data;   /*!< pointer to data */
47
 
        ha_storage_node_t*      next;   /*!< next node in hash chain */
48
 
};
49
 
 
50
 
/*******************************************************************//**
51
 
Creates a hash storage. If any of the parameters is 0, then a default
52
 
value is used.
53
 
@return own: hash storage */
54
 
UNIV_INLINE
55
 
ha_storage_t*
56
 
ha_storage_create(
57
 
/*==============*/
58
 
        ulint   initial_heap_bytes,     /*!< in: initial heap's size */
59
 
        ulint   initial_hash_cells)     /*!< in: initial number of cells
60
 
                                        in the hash table */
61
 
{
62
 
        ha_storage_t*   storage;
63
 
        mem_heap_t*     heap;
64
 
 
65
 
        if (initial_heap_bytes == 0) {
66
 
 
67
 
                initial_heap_bytes = HA_STORAGE_DEFAULT_HEAP_BYTES;
68
 
        }
69
 
 
70
 
        if (initial_hash_cells == 0) {
71
 
 
72
 
                initial_hash_cells = HA_STORAGE_DEFAULT_HASH_CELLS;
73
 
        }
74
 
 
75
 
        /* we put "storage" within "storage->heap" */
76
 
 
77
 
        heap = mem_heap_create(sizeof(ha_storage_t)
78
 
                               + initial_heap_bytes);
79
 
 
80
 
        storage = (ha_storage_t*) mem_heap_alloc(heap,
81
 
                                                 sizeof(ha_storage_t));
82
 
 
83
 
        storage->heap = heap;
84
 
        storage->hash = hash_create(initial_hash_cells);
85
 
 
86
 
        return(storage);
87
 
}
88
 
 
89
 
/*******************************************************************//**
90
 
Empties a hash storage, freeing memory occupied by data chunks.
91
 
This invalidates any pointers previously returned by ha_storage_put().
92
 
The hash storage is not invalidated itself and can be used again. */
93
 
UNIV_INLINE
94
 
void
95
 
ha_storage_empty(
96
 
/*=============*/
97
 
        ha_storage_t**  storage)        /*!< in/out: hash storage */
98
 
{
99
 
        ha_storage_t    temp_storage;
100
 
 
101
 
        temp_storage.heap = (*storage)->heap;
102
 
        temp_storage.hash = (*storage)->hash;
103
 
 
104
 
        hash_table_clear(temp_storage.hash);
105
 
        mem_heap_empty(temp_storage.heap);
106
 
 
107
 
        *storage = (ha_storage_t*) mem_heap_alloc(temp_storage.heap,
108
 
                                                  sizeof(ha_storage_t));
109
 
 
110
 
        (*storage)->heap = temp_storage.heap;
111
 
        (*storage)->hash = temp_storage.hash;
112
 
}
113
 
 
114
 
/*******************************************************************//**
115
 
Frees a hash storage and everything it contains, it cannot be used after
116
 
this call.
117
 
This invalidates any pointers previously returned by ha_storage_put(). */
118
 
UNIV_INLINE
119
 
void
120
 
ha_storage_free(
121
 
/*============*/
122
 
        ha_storage_t*   storage)        /*!< in, own: hash storage */
123
 
{
124
 
        /* order is important because the pointer storage->hash is
125
 
        within the heap */
126
 
        hash_table_free(storage->hash);
127
 
        mem_heap_free(storage->heap);
128
 
}
129
 
 
130
 
/*******************************************************************//**
131
 
Gets the size of the memory used by a storage.
132
 
@return bytes used */
133
 
UNIV_INLINE
134
 
ulint
135
 
ha_storage_get_size(
136
 
/*================*/
137
 
        const ha_storage_t*     storage)        /*!< in: hash storage */
138
 
{
139
 
        ulint   ret;
140
 
 
141
 
        ret = mem_heap_get_size(storage->heap);
142
 
 
143
 
        /* this assumes hash->heap and hash->heaps are NULL */
144
 
        ret += sizeof(hash_table_t);
145
 
        ret += sizeof(hash_cell_t) * hash_get_n_cells(storage->hash);
146
 
 
147
 
        return(ret);
148
 
}