1
/*****************************************************************************
3
Copyright (C) 2007, 2009, Innobase Oy. All Rights Reserved.
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.
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.
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
17
*****************************************************************************/
19
/**************************************************//**
20
@file include/ha0storage.ic
22
Provides a data structure that stores chunks of data in
23
its own storage, avoiding duplicates.
25
Created September 24, 2007 Vasil Dimov
26
*******************************************************/
29
#include "ha0storage.h"
30
#include "hash0hash.h"
33
/** Hash storage for strings */
34
struct ha_storage_struct {
35
mem_heap_t* heap; /*!< memory heap from which memory is
37
hash_table_t* hash; /*!< hash table used to avoid
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 */
50
/*******************************************************************//**
51
Creates a hash storage. If any of the parameters is 0, then a default
53
@return own: hash storage */
58
ulint initial_heap_bytes, /*!< in: initial heap's size */
59
ulint initial_hash_cells) /*!< in: initial number of cells
62
ha_storage_t* storage;
65
if (initial_heap_bytes == 0) {
67
initial_heap_bytes = HA_STORAGE_DEFAULT_HEAP_BYTES;
70
if (initial_hash_cells == 0) {
72
initial_hash_cells = HA_STORAGE_DEFAULT_HASH_CELLS;
75
/* we put "storage" within "storage->heap" */
77
heap = mem_heap_create(sizeof(ha_storage_t)
78
+ initial_heap_bytes);
80
storage = (ha_storage_t*) mem_heap_alloc(heap,
81
sizeof(ha_storage_t));
84
storage->hash = hash_create(initial_hash_cells);
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. */
97
ha_storage_t** storage) /*!< in/out: hash storage */
99
ha_storage_t temp_storage;
101
temp_storage.heap = (*storage)->heap;
102
temp_storage.hash = (*storage)->hash;
104
hash_table_clear(temp_storage.hash);
105
mem_heap_empty(temp_storage.heap);
107
*storage = (ha_storage_t*) mem_heap_alloc(temp_storage.heap,
108
sizeof(ha_storage_t));
110
(*storage)->heap = temp_storage.heap;
111
(*storage)->hash = temp_storage.hash;
114
/*******************************************************************//**
115
Frees a hash storage and everything it contains, it cannot be used after
117
This invalidates any pointers previously returned by ha_storage_put(). */
122
ha_storage_t* storage) /*!< in, own: hash storage */
124
/* order is important because the pointer storage->hash is
126
hash_table_free(storage->hash);
127
mem_heap_free(storage->heap);
130
/*******************************************************************//**
131
Gets the size of the memory used by a storage.
132
@return bytes used */
137
const ha_storage_t* storage) /*!< in: hash storage */
141
ret = mem_heap_get_size(storage->heap);
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);