| 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 lob0ins_h |
| 28 |
#define lob0ins_h |
| 29 |
|
| 30 |
#include "lob0lob.h" |
| 31 |
|
| 32 |
namespace lob { |
| 33 |
|
| 34 |
/** This struct can hold BLOB routines/functions, and state variables, |
| 35 |
that are common for compressed and uncompressed BLOB. */ |
| 36 |
struct BaseInserter { |
| 37 |
/** Constructor. |
| 38 |
@param[in] ctx blob operation context. */ |
| 39 |
BaseInserter(InsertContext *ctx) |
| 40 |
: m_ctx(ctx), |
| 41 |
m_err(DB_SUCCESS), |
| 42 |
m_prev_page_no(ctx->get_page_no()), |
| 43 |
m_cur_blob_block(nullptr), |
| 44 |
m_cur_blob_page_no(FIL_NULL) {} |
| 45 |
|
| 46 |
/** Start the BLOB mtr. |
| 47 |
@return pointer to the BLOB mtr. */ |
| 48 |
mtr_t *start_blob_mtr() { |
| 49 |
mtr_start(&m_blob_mtr); |
| 50 |
m_blob_mtr.set_log_mode(m_ctx->get_log_mode()); |
| 51 |
m_blob_mtr.set_flush_observer(m_ctx->get_flush_observer()); |
| 52 |
return (&m_blob_mtr); |
| 53 |
} |
| 54 |
|
| 55 |
/** Allocate one BLOB page. |
| 56 |
@return the allocated block of the BLOB page. */ |
| 57 |
buf_block_t *alloc_blob_page(); |
| 58 |
|
| 59 |
/** Get the previous BLOB page frame. This will return a BLOB page. |
| 60 |
It should not be called for the first BLOB page, because it will not |
| 61 |
have a previous BLOB page. |
| 62 |
@return the previous BLOB page frame. */ |
| 63 |
page_t *get_previous_blob_page(); |
| 64 |
|
| 65 |
/** Get the previous BLOB page block. This will return a BLOB block. |
| 66 |
It should not be called for the first BLOB page, because it will not |
| 67 |
have a previous BLOB page. |
| 68 |
@return the previous BLOB block. */ |
| 69 |
buf_block_t *get_previous_blob_block(); |
| 70 |
|
| 71 |
/** Check if the index is SDI index |
| 72 |
@return true if index is SDI index else false */ |
| 73 |
bool is_index_sdi() { return (dict_index_is_sdi(m_ctx->index())); } |
| 74 |
|
| 75 |
/** Get the current BLOB page frame. |
| 76 |
@return the current BLOB page frame. */ |
| 77 |
page_t *cur_page() const { return (buf_block_get_frame(m_cur_blob_block)); } |
| 78 |
|
| 79 |
protected: |
| 80 |
/** The BLOB operation context */ |
| 81 |
InsertContext *m_ctx; |
| 82 |
|
| 83 |
/** Success or failure status of the operation so far. */ |
| 84 |
dberr_t m_err; |
| 85 |
|
| 86 |
/** The mini trx used to write into blob pages */ |
| 87 |
mtr_t m_blob_mtr; |
| 88 |
|
| 89 |
/** The previous BLOB page number. This is needed to maintain |
| 90 |
the linked list of BLOB pages. */ |
| 91 |
page_no_t m_prev_page_no; |
| 92 |
|
| 93 |
/** The current BLOB buf_block_t object. */ |
| 94 |
buf_block_t *m_cur_blob_block; |
| 95 |
|
| 96 |
/** The current BLOB page number. */ |
| 97 |
page_no_t m_cur_blob_page_no; |
| 98 |
}; |
| 99 |
|
| 100 |
/** Insert or write an uncompressed BLOB */ |
| 101 |
class Inserter : private BaseInserter { |
| 102 |
public: |
| 103 |
/** Constructor. |
| 104 |
@param[in] ctx blob operation context. */ |
| 105 |
Inserter(InsertContext *ctx) : BaseInserter(ctx) {} |
| 106 |
|
| 107 |
/** Destructor. */ |
| 108 |
~Inserter() = default; |
| 109 |
|
| 110 |
/** Write all the BLOBs of the clustered index record. |
| 111 |
@return DB_SUCCESS on success, error code on failure. */ |
| 112 |
dberr_t write(); |
| 113 |
|
| 114 |
/** Write one blob field data. |
| 115 |
@param[in] blob_j the blob field number |
| 116 |
@return DB_SUCCESS on success, error code on failure. */ |
| 117 |
dberr_t write_one_blob(size_t blob_j); |
| 118 |
|
| 119 |
/** Write one blob field data. |
| 120 |
@param[in] blob_j the blob field number |
| 121 |
@return DB_SUCCESS on success, error code on failure. */ |
| 122 |
dberr_t write_one_small_blob(size_t blob_j); |
| 123 |
|
| 124 |
/** Write one blob page. This function will be repeatedly called |
| 125 |
with an increasing nth_blob_page to completely write a BLOB. |
| 126 |
@param[in] field the big record field. |
| 127 |
@param[in] nth_blob_page count of the BLOB page (starting from 1). |
| 128 |
@return DB_SUCCESS or DB_FAIL. */ |
| 129 |
dberr_t write_single_blob_page(big_rec_field_t &field, ulint nth_blob_page); |
| 130 |
|
| 131 |
/** Check if the BLOB operation has reported any errors. |
| 132 |
@return true if BLOB operation is successful, false otherwise. */ |
| 133 |
bool is_ok() const { return (m_err == DB_SUCCESS); } |
| 134 |
|
| 135 |
/** Make the current page as next page of previous page. In other |
| 136 |
words, make the page m_cur_blob_page_no as the next page of page |
| 137 |
m_prev_page_no. */ |
| 138 |
void set_page_next(); |
| 139 |
|
| 140 |
/** Write the page type of the current BLOB page and also generate the |
| 141 |
redo log record. */ |
| 142 |
void log_page_type() { |
| 143 |
page_type_t page_type; |
| 144 |
page_t *blob_page = cur_page(); |
| 145 |
|
| 146 |
if (is_index_sdi()) { |
| 147 |
page_type = FIL_PAGE_SDI_BLOB; |
| 148 |
} else { |
| 149 |
page_type = FIL_PAGE_TYPE_BLOB; |
| 150 |
} |
| 151 |
|
| 152 |
mlog_write_ulint(blob_page + FIL_PAGE_TYPE, page_type, MLOG_2BYTES, |
| 153 |
&m_blob_mtr); |
| 154 |
} |
| 155 |
|
| 156 |
/** Calculate the payload size of the BLOB page. |
| 157 |
@return payload size in bytes. */ |
| 158 |
ulint payload() const { |
| 159 |
const page_size_t page_size = m_ctx->page_size(); |
| 160 |
const ulint payload_size = |
| 161 |
page_size.physical() - FIL_PAGE_DATA - LOB_HDR_SIZE - FIL_PAGE_DATA_END; |
| 162 |
return (payload_size); |
| 163 |
} |
| 164 |
|
| 165 |
/** Write contents into a single BLOB page. |
| 166 |
@param[in] field the big record field. */ |
| 167 |
void write_into_single_page(big_rec_field_t &field); |
| 168 |
|
| 169 |
/** Write first blob page. |
| 170 |
@param[in] field the big record field. |
| 171 |
@return DB_SUCCESS on success. */ |
| 172 |
dberr_t write_first_page(big_rec_field_t &field); |
| 173 |
|
| 174 |
private: |
| 175 |
/** The BLOB directory information. */ |
| 176 |
blob_dir_t m_dir; |
| 177 |
|
| 178 |
/** Data remaining to be written. */ |
| 179 |
ulint m_remaining; |
| 180 |
}; |
| 181 |
|
| 182 |
} // namespace lob |
| 183 |
|
| 184 |
#endif /* lob0ins_h */ |