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