| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2007, 2026, Oracle and/or its affiliates. |
| 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, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file ha/ha0storage.cc |
| 29 |
Hash storage. |
| 30 |
Provides a data structure that stores chunks of data in |
| 31 |
its own storage, avoiding duplicates. |
| 32 |
|
| 33 |
Created September 22, 2007 Vasil Dimov |
| 34 |
*******************************************************/ |
| 35 |
|
| 36 |
#include "ha0storage.h" |
| 37 |
#include "ha_prototypes.h" |
| 38 |
#include "hash0hash.h" |
| 39 |
#include "mem0mem.h" |
| 40 |
#include "ut0rnd.h" |
| 41 |
|
| 42 |
/** Retrieves a data from a storage. If it is present, a pointer to the |
| 43 |
stored copy of data is returned, otherwise NULL is returned. */ |
| 44 |
static const void *ha_storage_get( |
| 45 |
ha_storage_t *storage, /*!< in: hash storage */ |
| 46 |
const void *data, /*!< in: data to check for */ |
| 47 |
ulint data_len) /*!< in: data length */ |
| 48 |
{ |
| 49 |
ha_storage_node_t *node; |
| 50 |
|
| 51 |
/* avoid repetitive calls to ut::hash_binary() in the HASH_SEARCH |
| 52 |
macro */ |
| 53 |
const auto hash_value = |
| 54 |
ut::hash_binary(static_cast<const byte *>(data), data_len); |
| 55 |
|
| 56 |
#define IS_FOUND \ |
| 57 |
node->data_len == data_len &&memcmp(node->data, data, data_len) == 0 |
| 58 |
|
| 59 |
HASH_SEARCH(next, /* node->"next" */ |
| 60 |
storage->hash, /* the hash table */ |
| 61 |
hash_value, /* key */ |
| 62 |
ha_storage_node_t *, /* type of node->next */ |
| 63 |
node, /* auxiliary variable */ |
| 64 |
, /* assertion */ |
| 65 |
IS_FOUND); /* search criteria */ |
| 66 |
|
| 67 |
if (node == nullptr) { |
| 68 |
return (nullptr); |
| 69 |
} |
| 70 |
/* else */ |
| 71 |
|
| 72 |
return (node->data); |
| 73 |
} |
| 74 |
|
| 75 |
/** Copies data into the storage and returns a pointer to the copy. If the |
| 76 |
same data chunk is already present, then pointer to it is returned. |
| 77 |
Data chunks are considered to be equal if len1 == len2 and |
| 78 |
memcmp(data1, data2, len1) == 0. If "data" is not present (and thus |
| 79 |
data_len bytes need to be allocated) and the size of storage is going to |
| 80 |
become more than "memlim" then "data" is not added and NULL is returned. |
| 81 |
To disable this behavior "memlim" can be set to 0, which stands for |
| 82 |
"no limit". */ |
| 83 |
const void *ha_storage_put_memlim( |
| 84 |
ha_storage_t *storage, /*!< in/out: hash storage */ |
| 85 |
const void *data, /*!< in: data to store */ |
| 86 |
ulint data_len, /*!< in: data length */ |
| 87 |
ulint memlim) /*!< in: memory limit to obey */ |
| 88 |
{ |
| 89 |
void *raw; |
| 90 |
ha_storage_node_t *node; |
| 91 |
const void *data_copy; |
| 92 |
|
| 93 |
/* check if data chunk is already present */ |
| 94 |
data_copy = ha_storage_get(storage, data, data_len); |
| 95 |
if (data_copy != nullptr) { |
| 96 |
return (data_copy); |
| 97 |
} |
| 98 |
|
| 99 |
/* not present */ |
| 100 |
|
| 101 |
/* check if we are allowed to allocate data_len bytes */ |
| 102 |
if (memlim > 0 && ha_storage_get_size(storage) + data_len > memlim) { |
| 103 |
return (nullptr); |
| 104 |
} |
| 105 |
|
| 106 |
/* we put the auxiliary node struct and the data itself in one |
| 107 |
continuous block */ |
| 108 |
raw = mem_heap_alloc(storage->heap, sizeof(ha_storage_node_t) + data_len); |
| 109 |
|
| 110 |
node = (ha_storage_node_t *)raw; |
| 111 |
data_copy = (byte *)raw + sizeof(*node); |
| 112 |
|
| 113 |
memcpy((byte *)raw + sizeof(*node), data, data_len); |
| 114 |
|
| 115 |
node->data_len = data_len; |
| 116 |
node->data = data_copy; |
| 117 |
|
| 118 |
/* avoid repetitive calls to ut::hash_binary() in the HASH_INSERT |
| 119 |
macro */ |
| 120 |
const auto hash_value = |
| 121 |
ut::hash_binary(static_cast<const byte *>(data), data_len); |
| 122 |
|
| 123 |
HASH_INSERT(ha_storage_node_t, /* type used in the hash chain */ |
| 124 |
next, /* node->"next" */ |
| 125 |
storage->hash, /* the hash table */ |
| 126 |
hash_value, /* key */ |
| 127 |
node); /* add this data to the hash */ |
| 128 |
|
| 129 |
/* the output should not be changed because it will spoil the |
| 130 |
hash table */ |
| 131 |
return (data_copy); |
| 132 |
} |
| 133 |
|
| 134 |
#ifdef UNIV_COMPILE_TEST_FUNCS |
| 135 |
|
| 136 |
void test_ha_storage() { |
| 137 |
ha_storage_t *storage; |
| 138 |
char buf[1024]; |
| 139 |
int i; |
| 140 |
const void *stored[256]; |
| 141 |
const void *p; |
| 142 |
|
| 143 |
storage = ha_storage_create(0, 0); |
| 144 |
|
| 145 |
for (i = 0; i < 256; i++) { |
| 146 |
memset(buf, i, sizeof(buf)); |
| 147 |
stored[i] = ha_storage_put(storage, buf, sizeof(buf)); |
| 148 |
} |
| 149 |
|
| 150 |
// ha_storage_empty(&storage); |
| 151 |
|
| 152 |
for (i = 255; i >= 0; i--) { |
| 153 |
memset(buf, i, sizeof(buf)); |
| 154 |
p = ha_storage_put(storage, buf, sizeof(buf)); |
| 155 |
|
| 156 |
if (p != stored[i]) { |
| 157 |
ib::warn(ER_IB_MSG_523) << "ha_storage_put() returned " << p |
| 158 |
<< " instead of " << stored[i] << ", i=" << i; |
| 159 |
return; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
ib::info(ER_IB_MSG_524) << "all ok"; |
| 164 |
|
| 165 |
ha_storage_free(storage); |
| 166 |
} |
| 167 |
|
| 168 |
#endif /* UNIV_COMPILE_TEST_FUNCS */ |