| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2017, 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 |
#ifndef DICT_SDI_H |
| 29 |
#define DICT_SDI_H |
| 30 |
|
| 31 |
#include <zconf.h> |
| 32 |
#include <zlib.h> |
| 33 |
|
| 34 |
/** Size of sdi_key_t::type */ |
| 35 |
constexpr const uint32_t SDI_TYPE_LEN = 4; |
| 36 |
|
| 37 |
/** Size of sdi_key_t::id */ |
| 38 |
constexpr const uint32_t SDI_KEY_LEN = 8; |
| 39 |
|
| 40 |
/** Compress SDI using zlib */ |
| 41 |
class Sdi_Compressor { |
| 42 |
public: |
| 43 |
Sdi_Compressor(uint32_t src_len, const void *sdi) |
| 44 |
: m_src_len(src_len), m_comp_len(), m_sdi(sdi), m_comp_sdi() {} |
| 45 |
|
| 46 |
~Sdi_Compressor() { ut::free(m_comp_sdi); } |
| 47 |
|
| 48 |
/** Compress the SDI */ |
| 49 |
inline void compress() { |
| 50 |
uLongf zlen = compressBound(static_cast<uLong>(m_src_len)); |
| 51 |
auto src = reinterpret_cast<const Bytef *>(m_sdi); |
| 52 |
|
| 53 |
m_comp_sdi = |
| 54 |
static_cast<byte *>(ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, zlen)); |
| 55 |
ut_ad(m_comp_sdi != nullptr); |
| 56 |
|
| 57 |
switch ( |
| 58 |
compress2(m_comp_sdi, &zlen, src, static_cast<uLong>(m_src_len), 6)) { |
| 59 |
case Z_BUF_ERROR: |
| 60 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_FAILED_SDI_Z_BUF_ERROR); |
| 61 |
break; |
| 62 |
|
| 63 |
case Z_MEM_ERROR: |
| 64 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_FAILED_SDI_Z_MEM_ERROR); |
| 65 |
break; |
| 66 |
|
| 67 |
case Z_STREAM_ERROR: |
| 68 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_SDI_Z_STREAM_ERROR); |
| 69 |
break; |
| 70 |
|
| 71 |
case Z_OK: |
| 72 |
m_comp_len = zlen; |
| 73 |
break; |
| 74 |
|
| 75 |
default: |
| 76 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_SDI_Z_UNKNOWN_ERROR); |
| 77 |
break; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
/** @return compressed SDI record */ |
| 82 |
const byte *get_data() const { return (m_comp_sdi); } |
| 83 |
|
| 84 |
/** @return length of uncompressed SDI */ |
| 85 |
uint32_t get_src_len() const { return (m_src_len); } |
| 86 |
|
| 87 |
/** @return length of compressed SDI */ |
| 88 |
uint32_t get_comp_len() const { return (m_comp_len); } |
| 89 |
|
| 90 |
private: |
| 91 |
/** Length of uncompressed SDI */ |
| 92 |
uint32_t m_src_len; |
| 93 |
/** Length of compressed SDI */ |
| 94 |
uint32_t m_comp_len; |
| 95 |
/** Uncompressed SDI */ |
| 96 |
const void *m_sdi; |
| 97 |
/** Compressed SDI */ |
| 98 |
byte *m_comp_sdi; |
| 99 |
}; |
| 100 |
|
| 101 |
/** Create SDI in a tablespace. This API should be used when |
| 102 |
upgrading a tablespace with no SDI. |
| 103 |
@param[in,out] tablespace tablespace object |
| 104 |
@retval false success |
| 105 |
@retval true failure */ |
| 106 |
bool dict_sdi_create(dd::Tablespace *tablespace); |
| 107 |
|
| 108 |
/** Drop SDI in a tablespace. This API should be used only |
| 109 |
when SDI is corrupted. |
| 110 |
@param[in,out] tablespace tablespace object |
| 111 |
@retval false success |
| 112 |
@retval true failure */ |
| 113 |
bool dict_sdi_drop(dd::Tablespace *tablespace); |
| 114 |
|
| 115 |
/** Get the SDI keys in a tablespace into the vector provided. |
| 116 |
@param[in] tablespace tablespace object |
| 117 |
@param[in,out] vector vector to hold SDI keys |
| 118 |
@retval false success |
| 119 |
@retval true failure */ |
| 120 |
bool dict_sdi_get_keys(const dd::Tablespace &tablespace, sdi_vector_t &vector); |
| 121 |
|
| 122 |
/** Retrieve SDI from tablespace. |
| 123 |
@param[in] tablespace tablespace object |
| 124 |
@param[in] sdi_key SDI key |
| 125 |
@param[in,out] sdi SDI retrieved from tablespace |
| 126 |
@param[in,out] sdi_len in: size of memory allocated |
| 127 |
out: actual length of SDI |
| 128 |
@retval false success |
| 129 |
@retval true in case of failures like record not found, |
| 130 |
sdi_len is UINT64MAX_T, else sdi_len is |
| 131 |
actual length of SDI */ |
| 132 |
bool dict_sdi_get(const dd::Tablespace &tablespace, const sdi_key_t *sdi_key, |
| 133 |
void *sdi, uint64_t *sdi_len); |
| 134 |
|
| 135 |
/** Insert/Update SDI in tablespace. |
| 136 |
@param[in] hton handlerton object |
| 137 |
@param[in] tablespace tablespace object |
| 138 |
@param[in] table table object |
| 139 |
@param[in] sdi_key SDI key to uniquely identify the tablespace |
| 140 |
object |
| 141 |
@param[in] sdi SDI to be stored in tablespace |
| 142 |
@param[in] sdi_len SDI length |
| 143 |
@retval false success |
| 144 |
@retval true failure */ |
| 145 |
bool dict_sdi_set(handlerton *hton, const dd::Tablespace &tablespace, |
| 146 |
const dd::Table *table, const sdi_key_t *sdi_key, |
| 147 |
const void *sdi, uint64_t sdi_len); |
| 148 |
|
| 149 |
/** Delete SDI from tablespace. |
| 150 |
@param[in] tablespace tablespace object |
| 151 |
@param[in] table table object |
| 152 |
@param[in] sdi_key SDI key to uniquely identify the tablespace |
| 153 |
object |
| 154 |
@retval false success |
| 155 |
@retval true failure */ |
| 156 |
bool dict_sdi_delete(const dd::Tablespace &tablespace, const dd::Table *table, |
| 157 |
const sdi_key_t *sdi_key); |
| 158 |
#endif |