| 1 |
/*********************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1995, 2026, Oracle and/or its affiliates. |
| 4 |
Copyright (c) 2009, Percona Inc. |
| 5 |
|
| 6 |
Portions of this file contain modifications contributed and copyrighted |
| 7 |
by Percona Inc.. Those modifications are |
| 8 |
gratefully acknowledged and are described briefly in the InnoDB |
| 9 |
documentation. The contributions by Percona Inc. are incorporated with |
| 10 |
their permission, and subject to the conditions contained in the file |
| 11 |
COPYING.Percona. |
| 12 |
|
| 13 |
This program is free software; you can redistribute it and/or modify |
| 14 |
it under the terms of the GNU General Public License, version 2.0, |
| 15 |
as published by the Free Software Foundation. |
| 16 |
|
| 17 |
This program is designed to work with certain software (including |
| 18 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 19 |
as designated in a particular file or component or in included license |
| 20 |
documentation. The authors of MySQL hereby grant you an additional |
| 21 |
permission to link the program and your derivative works with the |
| 22 |
separately licensed software that they have either included with |
| 23 |
the program or referenced in the documentation. |
| 24 |
|
| 25 |
This program is distributed in the hope that it will be useful, |
| 26 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 27 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 28 |
GNU General Public License, version 2.0, for more details. |
| 29 |
|
| 30 |
You should have received a copy of the GNU General Public License |
| 31 |
along with this program; if not, write to the Free Software |
| 32 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 33 |
|
| 34 |
***********************************************************************/ |
| 35 |
|
| 36 |
/** NOTE: The functions in this file should only use functions from |
| 37 |
other files in library. The code in this file is used to make a library for |
| 38 |
external tools. */ |
| 39 |
|
| 40 |
/** @file os/file.h |
| 41 |
The interface to the operating system file io |
| 42 |
|
| 43 |
Created 10/21/1995 Heikki Tuuri |
| 44 |
*******************************************************/ |
| 45 |
#ifndef os_file_h |
| 46 |
#define os_file_h |
| 47 |
|
| 48 |
#include "univ.i" |
| 49 |
|
| 50 |
/** Compression algorithm. */ |
| 51 |
struct Compression { |
| 52 |
/** Algorithm types supported */ |
| 53 |
enum Type : uint8_t { |
| 54 |
/* Note: During recovery we don't have the compression type |
| 55 |
because the .frm file has not been read yet. Therefore |
| 56 |
we write the recovered pages out without compression. */ |
| 57 |
|
| 58 |
/** No compression */ |
| 59 |
NONE = 0, |
| 60 |
|
| 61 |
/** Use ZLib */ |
| 62 |
ZLIB = 1, |
| 63 |
|
| 64 |
/** Use LZ4 faster variant, usually lower compression. */ |
| 65 |
LZ4 = 2 |
| 66 |
}; |
| 67 |
|
| 68 |
/** Compressed page meta-data */ |
| 69 |
struct meta_t { |
| 70 |
/** Version number */ |
| 71 |
uint8_t m_version; |
| 72 |
|
| 73 |
/** Algorithm type */ |
| 74 |
Type m_algorithm; |
| 75 |
|
| 76 |
/** Original page type */ |
| 77 |
uint16_t m_original_type; |
| 78 |
|
| 79 |
/** Original page size, before compression */ |
| 80 |
uint16_t m_original_size; |
| 81 |
|
| 82 |
/** Size after compression */ |
| 83 |
uint16_t m_compressed_size; |
| 84 |
}; |
| 85 |
|
| 86 |
/** Default constructor */ |
| 87 |
Compression() : m_type(NONE) {} |
| 88 |
|
| 89 |
/** Specific constructor |
| 90 |
@param[in] type Algorithm type */ |
| 91 |
explicit Compression(Type type) : m_type(type) { |
| 92 |
#ifdef UNIV_DEBUG |
| 93 |
switch (m_type) { |
| 94 |
case NONE: |
| 95 |
case ZLIB: |
| 96 |
case LZ4: |
| 97 |
break; |
| 98 |
default: |
| 99 |
ut_error; |
| 100 |
} |
| 101 |
#endif /* UNIV_DEBUG */ |
| 102 |
} |
| 103 |
|
| 104 |
/** @return string representation. */ |
| 105 |
std::string to_string() const { |
| 106 |
std::ostringstream os; |
| 107 |
|
| 108 |
os << "type: "; |
| 109 |
switch (m_type) { |
| 110 |
case NONE: |
| 111 |
os << "NONE"; |
| 112 |
break; |
| 113 |
case ZLIB: |
| 114 |
os << "ZLIB"; |
| 115 |
break; |
| 116 |
case LZ4: |
| 117 |
os << "LZ4"; |
| 118 |
break; |
| 119 |
default: |
| 120 |
os << "<UNKNOWN>"; |
| 121 |
break; |
| 122 |
} |
| 123 |
|
| 124 |
return (os.str()); |
| 125 |
} |
| 126 |
|
| 127 |
/** Version of compressed page */ |
| 128 |
static constexpr uint8_t FIL_PAGE_VERSION_1 = 1; |
| 129 |
static constexpr uint8_t FIL_PAGE_VERSION_2 = 2; |
| 130 |
|
| 131 |
/** Check the page header type field. |
| 132 |
@param[in] page Page contents |
| 133 |
@return true if it is a compressed page */ |
| 134 |
[[nodiscard]] static bool is_compressed_page(const byte *page); |
| 135 |
|
| 136 |
/** Check the page header type field. |
| 137 |
@param[in] page Page contents |
| 138 |
@return true if it is a compressed and encrypted page */ |
| 139 |
[[nodiscard]] static bool is_compressed_encrypted_page(const byte *page); |
| 140 |
|
| 141 |
/** Check if the version on page is valid. |
| 142 |
@param[in] version version |
| 143 |
@return true if version is valid */ |
| 144 |
static bool is_valid_page_version(uint8_t version); |
| 145 |
|
| 146 |
/** Check whether the compression algorithm is supported. |
| 147 |
@param[in] algorithm Compression algorithm to check |
| 148 |
@param[out] compression The type that algorithm maps to |
| 149 |
@return DB_SUCCESS or error code */ |
| 150 |
[[nodiscard]] static dberr_t check(const char *algorithm, |
| 151 |
Compression *compression); |
| 152 |
|
| 153 |
/** Validate the algorithm string. |
| 154 |
@param[in] algorithm Compression algorithm to check |
| 155 |
@return DB_SUCCESS or error code */ |
| 156 |
[[nodiscard]] static dberr_t validate(const char *algorithm); |
| 157 |
|
| 158 |
/** Validate the algorithm string. |
| 159 |
@param[in] type compression type |
| 160 |
@return true if type is valid, else false */ |
| 161 |
[[nodiscard]] static bool validate(const Type type); |
| 162 |
|
| 163 |
/** Convert to a "string". |
| 164 |
@param[in] type The compression type |
| 165 |
@return the string representation */ |
| 166 |
[[nodiscard]] static const char *to_string(Type type); |
| 167 |
|
| 168 |
/** Convert the meta data to a std::string. |
| 169 |
@param[in] meta Page Meta data |
| 170 |
@return the string representation */ |
| 171 |
[[nodiscard]] static std::string to_string(const meta_t &meta); |
| 172 |
|
| 173 |
/** Deserizlise the page header compression meta-data |
| 174 |
@param[in] page Pointer to the page header |
| 175 |
@param[out] control Deserialised data */ |
| 176 |
static void deserialize_header(const byte *page, meta_t *control); |
| 177 |
|
| 178 |
/** Check if the string is "empty" or "none". |
| 179 |
@param[in] algorithm Compression algorithm to check |
| 180 |
@return true if no algorithm requested */ |
| 181 |
[[nodiscard]] static bool is_none(const char *algorithm); |
| 182 |
|
| 183 |
/** Decompress the page data contents. Page type must be FIL_PAGE_COMPRESSED, |
| 184 |
if not then the source contents are left unchanged and DB_SUCCESS is returned. |
| 185 |
@param[in] dblwr_read true if double write recovery in progress |
| 186 |
@param[in,out] src Data read from disk, decompressed data |
| 187 |
will be copied to this page |
| 188 |
@param[in,out] dst Scratch area to use for decompression or |
| 189 |
nullptr. |
| 190 |
@param[in] dst_len If dst is valid, size of the scratch area in |
| 191 |
bytes. |
| 192 |
@return DB_SUCCESS or error code */ |
| 193 |
[[nodiscard]] static dberr_t deserialize(bool dblwr_read, byte *src, |
| 194 |
byte *dst, ulint dst_len); |
| 195 |
|
| 196 |
/** Compression type */ |
| 197 |
Type m_type; |
| 198 |
}; |
| 199 |
#endif |