| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2016, 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 |
#ifndef lob0zip_h |
| 28 |
#define lob0zip_h |
| 29 |
|
| 30 |
#include "lob0ins.h" |
| 31 |
|
| 32 |
namespace lob { |
| 33 |
|
| 34 |
/** Insert or write the compressed BLOB as a single zlib stream. */ |
| 35 |
class zInserter : private BaseInserter { |
| 36 |
public: |
| 37 |
/** Constructor. |
| 38 |
@param[in] ctx blob operation context. */ |
| 39 |
zInserter(InsertContext *ctx) : BaseInserter(ctx), m_heap(nullptr) {} |
| 40 |
|
| 41 |
/** Destructor. */ |
| 42 |
~zInserter(); |
| 43 |
|
| 44 |
/** Prepare to write a compressed BLOB. Setup the zlib |
| 45 |
compression stream. |
| 46 |
@return DB_SUCCESS on success, error code on failure. */ |
| 47 |
dberr_t prepare(); |
| 48 |
|
| 49 |
/** Write all the BLOBs of the clustered index record. |
| 50 |
@return DB_SUCCESS on success, error code on failure. */ |
| 51 |
dberr_t write(); |
| 52 |
|
| 53 |
/** Write one blob field data. |
| 54 |
@param[in] blob_j the blob field number |
| 55 |
@return DB_SUCCESS on success, error code on failure. */ |
| 56 |
dberr_t write_one_blob(size_t blob_j); |
| 57 |
|
| 58 |
/** Cleanup after completing the write of compressed BLOB. |
| 59 |
@param[in] validate if true, validate all the |
| 60 |
lob references. if false, |
| 61 |
skip this validation. |
| 62 |
@return DB_SUCCESS on success, error code on failure. */ |
| 63 |
dberr_t finish(bool validate = true) { |
| 64 |
int ret = deflateEnd(&m_stream); |
| 65 |
ut_ad(ret == Z_OK); |
| 66 |
ut_ad(!validate || validate_blobrefs()); |
| 67 |
|
| 68 |
if (ret != Z_OK) { |
| 69 |
m_err = DB_FAIL; |
| 70 |
} |
| 71 |
return (m_err); |
| 72 |
} |
| 73 |
|
| 74 |
/** Write the page type of the BLOB page and also generate the |
| 75 |
redo log record. |
| 76 |
@param[in] blob_page the BLOB page |
| 77 |
@param[in] nth_blob_page the count of BLOB page from |
| 78 |
the beginning of the BLOB. */ |
| 79 |
void log_page_type(page_t *blob_page, ulint nth_blob_page) { |
| 80 |
page_type_t page_type; |
| 81 |
|
| 82 |
if (is_index_sdi()) { |
| 83 |
page_type = FIL_PAGE_SDI_ZBLOB; |
| 84 |
} else if (nth_blob_page == 0) { |
| 85 |
page_type = FIL_PAGE_TYPE_ZBLOB; |
| 86 |
} else { |
| 87 |
page_type = FIL_PAGE_TYPE_ZBLOB2; |
| 88 |
} |
| 89 |
|
| 90 |
mlog_write_ulint(blob_page + FIL_PAGE_TYPE, page_type, MLOG_2BYTES, |
| 91 |
&m_blob_mtr); |
| 92 |
} |
| 93 |
|
| 94 |
/** Calculate the total number of pages needed to store |
| 95 |
the given blobs */ |
| 96 |
ulint calc_total_pages() { |
| 97 |
const page_size_t page_size = m_ctx->page_size(); |
| 98 |
|
| 99 |
/* Space available in compressed page to carry blob data */ |
| 100 |
const ulint payload_size_zip = page_size.physical() - FIL_PAGE_DATA; |
| 101 |
|
| 102 |
const big_rec_t *vec = m_ctx->get_big_rec_vec(); |
| 103 |
|
| 104 |
ulint total_blob_pages = 0; |
| 105 |
for (ulint i = 0; i < vec->n_fields; i++) { |
| 106 |
total_blob_pages += |
| 107 |
static_cast<ulint>( |
| 108 |
deflateBound(&m_stream, static_cast<uLong>(vec->fields[i].len)) + |
| 109 |
payload_size_zip - 1) / |
| 110 |
payload_size_zip; |
| 111 |
} |
| 112 |
|
| 113 |
return (total_blob_pages); |
| 114 |
} |
| 115 |
|
| 116 |
/** Write contents into a single BLOB page. |
| 117 |
@return code as returned by zlib. */ |
| 118 |
int write_into_single_page(); |
| 119 |
|
| 120 |
/** Commit the BLOB mtr. */ |
| 121 |
void commit_blob_mtr() { mtr_commit(&m_blob_mtr); } |
| 122 |
|
| 123 |
/** Write one blob page. This function will be repeatedly called |
| 124 |
with an increasing nth_blob_page to completely write a BLOB. |
| 125 |
@param[in] field the big record field. |
| 126 |
@param[in] nth_blob_page count of the BLOB page (starting from 1). |
| 127 |
@return code as returned by the zlib. */ |
| 128 |
int write_single_blob_page(big_rec_field_t &field, ulint nth_blob_page); |
| 129 |
|
| 130 |
/** Write first blob page. |
| 131 |
@param[in] field the big record field. |
| 132 |
@return code as returned by the zlib. */ |
| 133 |
int write_first_page(big_rec_field_t &field); |
| 134 |
|
| 135 |
/** Verify that all pointers to externally stored columns in the record |
| 136 |
is be valid. If validation fails, this function doesn't return. |
| 137 |
@return true if valid. */ |
| 138 |
bool validate_blobrefs() const { |
| 139 |
const ulint *offsets = m_ctx->get_offsets(); |
| 140 |
|
| 141 |
for (ulint i = 0; i < rec_offs_n_fields(offsets); i++) { |
| 142 |
if (!rec_offs_nth_extern(m_ctx->index(), offsets, i)) { |
| 143 |
continue; |
| 144 |
} |
| 145 |
|
| 146 |
byte *field_ref = |
| 147 |
btr_rec_get_field_ref(m_ctx->index(), m_ctx->rec(), offsets, i); |
| 148 |
|
| 149 |
ref_t blobref(field_ref); |
| 150 |
|
| 151 |
/* The pointer must not be zero if the operation |
| 152 |
succeeded. */ |
| 153 |
ut_a(!blobref.is_null() || m_err != DB_SUCCESS); |
| 154 |
|
| 155 |
/* The column must not be disowned by this record. */ |
| 156 |
ut_a(blobref.is_owner()); |
| 157 |
} |
| 158 |
return (true); |
| 159 |
} |
| 160 |
|
| 161 |
/** For the given blob field, update its length in the blob reference |
| 162 |
which is available in the clustered index record. |
| 163 |
@param[in] field the concerned blob field. */ |
| 164 |
void update_length_in_blobref(big_rec_field_t &field); |
| 165 |
|
| 166 |
/** Make the current page as next page of previous page. In other |
| 167 |
words, make the page m_cur_blob_page_no as the next page |
| 168 |
(FIL_PAGE_NEXT) of page m_prev_page_no. |
| 169 |
@return DB_SUCCESS on success, or error code on failure. */ |
| 170 |
dberr_t set_page_next(); |
| 171 |
|
| 172 |
/** Write one small blob field data. Refer to ref_t to determine |
| 173 |
the definition of small blob. |
| 174 |
@param[in] blob_j the blob field number |
| 175 |
@return DB_SUCCESS on success, error code on failure. */ |
| 176 |
dberr_t write_one_small_blob(size_t blob_j); |
| 177 |
|
| 178 |
private: |
| 179 |
/** Add the BLOB page information to the directory |
| 180 |
@param[in] page_info BLOB page information. */ |
| 181 |
void add_to_blob_dir(const blob_page_info_t &page_info) { |
| 182 |
m_dir.add(page_info); |
| 183 |
} |
| 184 |
|
| 185 |
mem_heap_t *m_heap; |
| 186 |
z_stream m_stream; |
| 187 |
|
| 188 |
/** The BLOB directory information. */ |
| 189 |
blob_dir_t m_dir; |
| 190 |
}; |
| 191 |
|
| 192 |
inline zInserter::~zInserter() { |
| 193 |
if (m_heap != nullptr) { |
| 194 |
mem_heap_free(m_heap); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
} // namespace lob |
| 199 |
|
| 200 |
#endif // lob0zip_h |