| 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 |
| 6 |
it under the terms of the GNU General Public License, version 2.0, |
| 7 |
as published by the 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, |
| 18 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
GNU General Public License, version 2.0, for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License |
| 23 |
along with this program; if not, write to the Free Software |
| 24 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/lob0undo.h |
| 29 |
Undo logging small changes to BLOBs. */ |
| 30 |
|
| 31 |
#ifndef lob0undo_h |
| 32 |
#define lob0undo_h |
| 33 |
|
| 34 |
#include <list> |
| 35 |
#include "dict0mem.h" |
| 36 |
#include "mem0mem.h" |
| 37 |
#include "univ.i" |
| 38 |
|
| 39 |
namespace lob { |
| 40 |
|
| 41 |
/** Undo information about LOB data alone without including LOB index. */ |
| 42 |
struct undo_data_t { |
| 43 |
/** Apply the undo information to the given LOB. |
| 44 |
@param[in] index clustered index containing the LOB. |
| 45 |
@param[in] lob_mem LOB on which the given undo will be |
| 46 |
applied. |
| 47 |
@param[in] len length of LOB. |
| 48 |
@param[in] lob_version lob version number |
| 49 |
@param[in] first_page_no the first page number of lob */ |
| 50 |
void apply(dict_index_t *index, byte *lob_mem, size_t len, size_t lob_version, |
| 51 |
page_no_t first_page_no); |
| 52 |
|
| 53 |
/** The LOB first page number. */ |
| 54 |
page_no_t m_page_no = FIL_NULL; |
| 55 |
|
| 56 |
/** The LOB version number on which this undo should be applied. */ |
| 57 |
ulint m_version = 0; |
| 58 |
|
| 59 |
/** The offset within LOB where partial update happened. */ |
| 60 |
ulint m_offset = 0; |
| 61 |
|
| 62 |
/** The length of the modification. */ |
| 63 |
ulint m_length = 0; |
| 64 |
|
| 65 |
/** Changes to the LOB data. */ |
| 66 |
byte *m_old_data = nullptr; |
| 67 |
|
| 68 |
/** Copy the old data from the undo page into this object. |
| 69 |
@param[in] undo_ptr the pointer into the undo log record. |
| 70 |
@param[in] len length of the old data. |
| 71 |
@return pointer past the old data. */ |
| 72 |
const byte *copy_old_data(const byte *undo_ptr, ulint len); |
| 73 |
|
| 74 |
/** Free allocated memory for old data. */ |
| 75 |
void destroy(); |
| 76 |
|
| 77 |
std::ostream &print(std::ostream &out) const; |
| 78 |
}; |
| 79 |
|
| 80 |
inline std::ostream &operator<<(std::ostream &out, const undo_data_t &obj) { |
| 81 |
return (obj.print(out)); |
| 82 |
} |
| 83 |
|
| 84 |
/** Container to hold a sequence of undo log records containing modification |
| 85 |
of BLOBs. */ |
| 86 |
struct undo_seq_t { |
| 87 |
/** Constructor. |
| 88 |
@param[in] field_no the field number of LOB.*/ |
| 89 |
undo_seq_t(ulint field_no) : m_field_no(field_no), m_undo_list(nullptr) {} |
| 90 |
|
| 91 |
/** Apply the undo log records on the given LOB in memory. |
| 92 |
@param[in] index the clustered index to which LOB belongs. |
| 93 |
@param[in] lob the BLOB in memory. |
| 94 |
@param[in] len the length of BLOB in memory. |
| 95 |
@param[in] lob_version the LOB version number. |
| 96 |
@param[in] first_page_no the first page number of BLOB.*/ |
| 97 |
void apply(dict_index_t *index, byte *lob, size_t len, size_t lob_version, |
| 98 |
page_no_t first_page_no) { |
| 99 |
if (m_undo_list != nullptr) { |
| 100 |
for (auto iter = m_undo_list->begin(); iter != m_undo_list->end(); |
| 101 |
++iter) { |
| 102 |
iter->apply(index, lob, len, lob_version, first_page_no); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/** Get the field number of BLOB. |
| 108 |
@return the field number of BLOB. */ |
| 109 |
ulint get_field_no() const { return (m_field_no); } |
| 110 |
|
| 111 |
/** Append the given undo log record to the end of container. |
| 112 |
@param[in] u1 the undo log record information. */ |
| 113 |
void push_back(undo_data_t &u1) { |
| 114 |
if (m_undo_list == nullptr) { |
| 115 |
m_undo_list = |
| 116 |
ut::new_withkey<std::list<undo_data_t>>(UT_NEW_THIS_FILE_PSI_KEY); |
| 117 |
} |
| 118 |
m_undo_list->push_back(u1); |
| 119 |
} |
| 120 |
|
| 121 |
/** Destroy the contents of this undo sequence list. */ |
| 122 |
void destroy() { |
| 123 |
if (m_undo_list != nullptr) { |
| 124 |
std::for_each(m_undo_list->begin(), m_undo_list->end(), |
| 125 |
[](undo_data_t &obj) { obj.destroy(); }); |
| 126 |
m_undo_list->clear(); |
| 127 |
ut::delete_(m_undo_list); |
| 128 |
m_undo_list = nullptr; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/** Check if any undo log exists to apply. */ |
| 133 |
bool exists() const { |
| 134 |
return (m_undo_list == nullptr ? false : !m_undo_list->empty()); |
| 135 |
} |
| 136 |
|
| 137 |
ulint m_field_no; |
| 138 |
|
| 139 |
private: |
| 140 |
std::list<undo_data_t> *m_undo_list = nullptr; |
| 141 |
}; |
| 142 |
|
| 143 |
/** The list of modifications to be applied on LOBs to get older versions. |
| 144 |
Given a field number, it should be able to obtain the list of undo |
| 145 |
information. */ |
| 146 |
struct undo_vers_t { |
| 147 |
public: |
| 148 |
/** Get the undo log sequence object for the given field number, which |
| 149 |
represents one blob. |
| 150 |
@param[in] field_no the field number of the blob. |
| 151 |
@return the undo sequence object or nullptr. */ |
| 152 |
undo_seq_t *get_undo_sequence_if_exists(ulint field_no) { |
| 153 |
if (m_versions == nullptr) { |
| 154 |
return (nullptr); |
| 155 |
} else { |
| 156 |
for (auto iter = m_versions->begin(); iter != m_versions->end(); ++iter) { |
| 157 |
if ((*iter)->get_field_no() == field_no) { |
| 158 |
return (*iter); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
return (nullptr); |
| 163 |
} |
| 164 |
|
| 165 |
/** Get the undo log sequence object for the given field number, which |
| 166 |
represents one blob. The undo sequence object is allocated if it does |
| 167 |
not exist. |
| 168 |
@param[in] field_no the field number of the blob. |
| 169 |
@return the undo sequence object. */ |
| 170 |
undo_seq_t *get_undo_sequence(ulint field_no) { |
| 171 |
if (m_versions == nullptr) { |
| 172 |
m_versions = |
| 173 |
ut::new_withkey<std::list<undo_seq_t *>>(UT_NEW_THIS_FILE_PSI_KEY); |
| 174 |
} else { |
| 175 |
for (auto iter = m_versions->begin(); iter != m_versions->end(); ++iter) { |
| 176 |
if ((*iter)->get_field_no() == field_no) { |
| 177 |
return (*iter); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
undo_seq_t *seq = |
| 183 |
ut::new_withkey<undo_seq_t>(UT_NEW_THIS_FILE_PSI_KEY, field_no); |
| 184 |
m_versions->push_back(seq); |
| 185 |
|
| 186 |
return (seq); |
| 187 |
} |
| 188 |
|
| 189 |
/** Empty the collected LOB undo information from cache. */ |
| 190 |
void reset() { |
| 191 |
if (m_versions != nullptr) { |
| 192 |
for (auto iter = m_versions->begin(); iter != m_versions->end(); ++iter) { |
| 193 |
(*iter)->destroy(); |
| 194 |
ut::delete_(*iter); |
| 195 |
} |
| 196 |
m_versions->clear(); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
/** Apply the undo log record on the given LOB in memory. |
| 201 |
@param[in] clust_index the clust index to which LOB belongs. |
| 202 |
@param[in] field_no the field number of the LOB. |
| 203 |
@param[in] lob the LOB data. |
| 204 |
@param[in] len the length of LOB. |
| 205 |
@param[in] lob_version LOB version number. |
| 206 |
@param[in] first_page the first page number of LOB.*/ |
| 207 |
void apply(dict_index_t *clust_index, ulint field_no, byte *lob, size_t len, |
| 208 |
size_t lob_version, page_no_t first_page) { |
| 209 |
undo_seq_t *seq = get_undo_sequence_if_exists(field_no); |
| 210 |
|
| 211 |
if (seq != nullptr) { |
| 212 |
seq->apply(clust_index, lob, len, lob_version, first_page); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
/** Destroy the accumulated undo_seq_t objects. */ |
| 217 |
void destroy() { |
| 218 |
if (m_versions != nullptr) { |
| 219 |
reset(); |
| 220 |
ut::delete_(m_versions); |
| 221 |
m_versions = nullptr; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
/** Check if the undo contained older versions. |
| 226 |
@return true if there are no older versions, false otherwise. */ |
| 227 |
bool is_empty() const { return (m_versions->empty()); } |
| 228 |
|
| 229 |
/** Destructor to free the resources. */ |
| 230 |
~undo_vers_t() { destroy(); } |
| 231 |
|
| 232 |
private: |
| 233 |
/** Maintain a list of undo_seq_t objects. */ |
| 234 |
std::list<undo_seq_t *> *m_versions = nullptr; |
| 235 |
}; |
| 236 |
|
| 237 |
} /* namespace lob */ |
| 238 |
|
| 239 |
#endif /* lob0undo_h */ |