MySQL 8.3.0
Source Code Documentation
mach0data.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1995, 2023, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is also distributed with certain software (including but not
10limited to OpenSSL) that is licensed under separate terms, as designated in a
11particular file or component or in included license documentation. The authors
12of MySQL hereby grant you an additional permission to link the program and
13your derivative works with the separately licensed software that they have
14included with MySQL.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
25*****************************************************************************/
26
27/** @file include/mach0data.h
28 Utilities for converting data from the database file
29 to the machine format.
30
31 Created 11/28/1995 Heikki Tuuri
32 ***********************************************************************/
33
34#ifndef mach0data_h
35#define mach0data_h
36
37#include "mtr0types.h"
38#include "univ.i"
39
40#ifdef UNIV_HOTBACKUP
41#include "ut0byte.h"
42#endif /* UNIV_HOTBACKUP */
43
44/* The data and all fields are always stored in a database file
45in the same format: ascii, big-endian, ... .
46All data in the files MUST be accessed using the functions in this
47module. */
48
49/** The following function is used to store data in one byte.
50@param[in] b pointer to byte where to store
51@param[in] n One byte integer to be stored, >= 0, < 256 */
52static inline void mach_write_to_1(byte *b, ulint n);
53
54/** The following function is used to fetch data from one byte.
55@param[in] b pointer to a byte to read
56@return ulint integer, >= 0, < 256 */
57[[nodiscard]] static inline uint8_t mach_read_from_1(const byte *b);
58
59/** The following function is used to store data in two consecutive bytes. We
60store the most significant byte to the lower address.
61@param[in] b pointer to 2 bytes where to store
62@param[in] n 2-byte integer to be stored, >= 0, < 64k */
63static inline void mach_write_to_2(byte *b, ulint n);
64
65/** The following function is used to fetch data from 2 consecutive
66bytes. The most significant byte is at the lowest address.
67@param[in] b pointer to 2 bytes where to store
68@return 2-byte integer, >= 0, < 64k */
69[[nodiscard]] static inline uint16_t mach_read_from_2(const byte *b);
70
71/** The following function is used to convert a 16-bit data item to the
72canonical format, for fast bytewise equality test against memory.
73@param[in] n integer in machine-dependent format
74@return 16-bit integer in canonical format */
75static inline uint16_t mach_encode_2(ulint n);
76
77/** The following function is used to convert a 16-bit data item from the
78canonical format, for fast bytewise equality test against memory.
79@param[in] n 16-bit integer in canonical format
80@return integer in machine-dependent format */
81static inline ulint mach_decode_2(uint16 n);
82
83/** The following function is used to store data in 3 consecutive
84bytes. We store the most significant byte to the lowest address.
85@param[in] b pointer to 3 bytes where to store
86@param[in] n 3 byte integer to be stored */
87static inline void mach_write_to_3(byte *b, ulint n);
88
89/** The following function is used to fetch data from 3 consecutive
90bytes. The most significant byte is at the lowest address.
91@param[in] b pointer to 3 bytes to read
92@return 32 bit integer */
93[[nodiscard]] static inline uint32_t mach_read_from_3(const byte *b);
94
95/** The following function is used to store data in 4 consecutive
96bytes. We store the most significant byte to the lowest address.
97@param[in] b pointer to 4 bytes where to store
98@param[in] n 4 byte integer to be stored */
99static inline void mach_write_to_4(byte *b, ulint n);
100
101/** The following function is used to fetch data from 4 consecutive
102bytes. The most significant byte is at the lowest address.
103@param[in] b pointer to 4 bytes to read
104@return 32 bit integer */
105[[nodiscard]] static inline uint32_t mach_read_from_4(const byte *b);
106
107/** Write a ulint in a compressed form (1..5 bytes).
108@param[in] b pointer to memory where to store
109@param[in] n ulint integer to be stored
110@return stored size in bytes */
111static inline ulint mach_write_compressed(byte *b, ulint n);
112
113/** Return the size of an ulint when written in the compressed form.
114@param[in] n ulint integer to be stored
115@return compressed size in bytes */
116static inline uint32_t mach_get_compressed_size(ulint n);
117
118/** Read a 32-bit integer in a compressed form.
119@param[in,out] b pointer to memory where to read;
120advanced by the number of bytes consumed
121@return unsigned value */
122static inline uint32_t mach_read_next_compressed(const byte **b);
123
124/** The following function is used to store data in 6 consecutive
125bytes. We store the most significant byte to the lowest address.
126@param[in] b pointer to 6 bytes where to store
127@param[in] id 48-bit integer to write */
128static inline void mach_write_to_6(byte *b, uint64_t id);
129
130/** The following function is used to fetch data from 6 consecutive
131bytes. The most significant byte is at the lowest address.
132@param[in] b pointer to 6 bytes to read
133@return 48-bit integer */
134[[nodiscard]] static inline uint64_t mach_read_from_6(const byte *b);
135
136/** The following function is used to store data in 7 consecutive
137bytes. We store the most significant byte to the lowest address.
138@param[in] b pointer to 7 bytes where to store
139@param[in] n 56-bit integer */
140static inline void mach_write_to_7(byte *b, uint64_t n);
141
142/** The following function is used to fetch data from 7 consecutive
143bytes. The most significant byte is at the lowest address.
144@param[in] b pointer to 7 bytes to read
145@return 56-bit integer */
146[[nodiscard]] static inline uint64_t mach_read_from_7(const byte *b);
147
148/** The following function is used to store data in 8 consecutive
149bytes. We store the most significant byte to the lowest address.
150@param[in] b pointer to 8 bytes where to store
151@param[in] n 64-bit integer to be stored */
152static inline void mach_write_to_8(void *b, uint64_t n);
153
154/** The following function is used to fetch data from 8 consecutive
155bytes. The most significant byte is at the lowest address.
156@param[in] b pointer to 8 bytes to read
157@return 64-bit integer */
158[[nodiscard]] static inline uint64_t mach_read_from_8(const byte *b);
159
160/** Writes a 64-bit integer in a compressed form (5..9 bytes).
161@param[in] b pointer to memory where to store
162@param[in] n 64-bit integer to be stored
163@return size in bytes */
164static inline ulint mach_u64_write_compressed(byte *b, uint64_t n);
165
166/** Read a 64-bit integer in a compressed form.
167@param[in,out] b pointer to memory where to read;
168advanced by the number of bytes consumed
169@return unsigned value */
170static inline uint64_t mach_u64_read_next_compressed(const byte **b);
171
172/** Writes a 64-bit integer in a compressed form (1..11 bytes).
173@param[in] b pointer to memory where to store
174@param[in] n 64-bit integer to be stored
175@return size in bytes */
176static inline ulint mach_u64_write_much_compressed(byte *b, uint64_t n);
177
178/** Reads a 64-bit integer in a compressed form.
179@param[in] b pointer to memory from where to read
180@return the value read */
181[[nodiscard]] static inline uint64_t mach_u64_read_much_compressed(
182 const byte *b);
183
184/** Read a 64-bit integer in a much compressed form.
185@param[in,out] ptr pointer to memory from where to read,
186advanced by the number of bytes consumed, or set NULL if out of space
187@param[in] end_ptr end of the buffer
188@return unsigned 64-bit integer */
189uint64_t mach_parse_u64_much_compressed(const byte **ptr, const byte *end_ptr);
190
191/** Read a 32-bit integer in a compressed form.
192@param[in,out] ptr pointer to memory from where to read;
193advanced by the number of bytes consumed, or set NULL if out of space
194@param[in] end_ptr end of the buffer
195@return unsigned value */
196uint32_t mach_parse_compressed(const byte **ptr, const byte *end_ptr);
197
198/** Read a 64-bit integer in a compressed form.
199@param[in,out] ptr pointer to memory from where to read;
200advanced by the number of bytes consumed, or set NULL if out of space
201@param[in] end_ptr end of the buffer
202@return unsigned value */
203static inline uint64_t mach_u64_parse_compressed(const byte **ptr,
204 const byte *end_ptr);
205
206/** Reads a double. It is stored in a little-endian format.
207 @return double read */
208[[nodiscard]] static inline double mach_double_read(
209 const byte *b); /*!< in: pointer to memory from where to read */
210
211/** Writes a double. It is stored in a little-endian format.
212@param[in] b pointer to memory where to write
213@param[in] d double */
214static inline void mach_double_write(byte *b, double d);
215
216/** Reads a float. It is stored in a little-endian format.
217 @return float read */
218[[nodiscard]] static inline float mach_float_read(
219 const byte *b); /*!< in: pointer to memory from where to read */
220
221/** Writes a float. It is stored in a little-endian format.
222@param[in] b pointer to memory where to write
223@param[in] d float */
224static inline void mach_float_write(byte *b, float d);
225
226#ifndef UNIV_HOTBACKUP
227/** Reads a ulint stored in the little-endian format.
228@param[in] buf From where to read.
229@param[in] buf_size From how many bytes to read.
230@return unsigned long int */
231[[nodiscard]] static inline ulint mach_read_from_n_little_endian(
232 const byte *buf, ulint buf_size);
233
234/** Writes a ulint in the little-endian format.
235@param[in] dest where to write
236@param[in] dest_size into how many bytes to write
237@param[in] n unsigned long int to write */
238static inline void mach_write_to_n_little_endian(byte *dest, ulint dest_size,
239 ulint n);
240
241/** Reads a ulint stored in the little-endian format.
242 @return unsigned long int */
243[[nodiscard]] static inline ulint mach_read_from_2_little_endian(
244 const byte *buf); /*!< in: from where to read */
245
246/** Writes a ulint in the little-endian format.
247@param[in] dest where to write
248@param[in] n unsigned long int to write */
249static inline void mach_write_to_2_little_endian(byte *dest, ulint n);
250
251/** Convert integral type from storage byte order (big endian) to host byte
252order.
253@param[in] src where to read from
254@param[in] len length of src
255@param[in] unsigned_type signed or unsigned flag
256@return integer value */
257static inline uint64_t mach_read_int_type(const byte *src, ulint len,
258 bool unsigned_type);
259
260/** Convert integral type from host byte order to (big-endian) storage
261byte order.
262@param[in] dest where to write
263@param[in] src where to read
264@param[in] len length of src
265@param[in] usign signed or unsigned flag */
266static inline void mach_write_int_type(byte *dest, const byte *src, ulint len,
267 bool usign);
268
269/** Convert a ulonglong integer from host byte order to (big-endian) storage
270byte order.
271@param[in] dest where to write
272@param[in] src where to read from
273@param[in] len length of dest
274@param[in] usign signed or unsigned flag */
275static inline void mach_write_ulonglong(byte *dest, ulonglong src, ulint len,
276 bool usign);
277
278#endif
279
280/** Read 1 to 4 bytes from a file page buffered in the buffer pool.
281@param[in] ptr pointer where to read
282@param[in] type MLOG_1BYTE, MLOG_2BYTES, or MLOG_4BYTES
283@return value read */
284[[nodiscard]] static inline uint32_t mach_read_ulint(const byte *ptr,
286
287#include "mach0data.ic"
288
289#endif
constexpr DWORD buf_size
Definition: create_def.cc:228
static void mach_write_int_type(byte *dest, const byte *src, ulint len, bool usign)
Convert integral type from host byte order to (big-endian) storage byte order.
static ulint mach_u64_write_much_compressed(byte *b, uint64_t n)
Writes a 64-bit integer in a compressed form (1..11 bytes).
static ulint mach_read_from_n_little_endian(const byte *buf, ulint buf_size)
Reads a ulint stored in the little-endian format.
static uint64_t mach_u64_read_much_compressed(const byte *b)
Reads a 64-bit integer in a compressed form.
static uint16_t mach_read_from_2(const byte *b)
The following function is used to fetch data from 2 consecutive bytes.
static uint64_t mach_u64_read_next_compressed(const byte **b)
Read a 64-bit integer in a compressed form.
uint64_t mach_parse_u64_much_compressed(const byte **ptr, const byte *end_ptr)
Read a 64-bit integer in a much compressed form.
Definition: mach0data.cc:43
static uint32_t mach_read_from_4(const byte *b)
The following function is used to fetch data from 4 consecutive bytes.
static void mach_write_to_n_little_endian(byte *dest, ulint dest_size, ulint n)
Writes a ulint in the little-endian format.
static void mach_write_to_7(byte *b, uint64_t n)
The following function is used to store data in 7 consecutive bytes.
static void mach_write_to_2_little_endian(byte *dest, ulint n)
Writes a ulint in the little-endian format.
static double mach_double_read(const byte *b)
Reads a double.
static void mach_write_to_2(byte *b, ulint n)
The following function is used to store data in two consecutive bytes.
static void mach_write_to_1(byte *b, ulint n)
The following function is used to store data in one byte.
static void mach_write_to_3(byte *b, ulint n)
The following function is used to store data in 3 consecutive bytes.
static uint32_t mach_read_ulint(const byte *ptr, mlog_id_t type)
Read 1 to 4 bytes from a file page buffered in the buffer pool.
static ulint mach_decode_2(uint16 n)
The following function is used to convert a 16-bit data item from the canonical format,...
static void mach_write_to_8(void *b, uint64_t n)
The following function is used to store data in 8 consecutive bytes.
static ulint mach_write_compressed(byte *b, ulint n)
Write a ulint in a compressed form (1..5 bytes).
static uint64_t mach_read_from_7(const byte *b)
The following function is used to fetch data from 7 consecutive bytes.
static uint8_t mach_read_from_1(const byte *b)
The following function is used to fetch data from one byte.
static ulint mach_u64_write_compressed(byte *b, uint64_t n)
Writes a 64-bit integer in a compressed form (5..9 bytes).
static void mach_write_ulonglong(byte *dest, ulonglong src, ulint len, bool usign)
Convert a ulonglong integer from host byte order to (big-endian) storage byte order.
static void mach_write_to_4(byte *b, ulint n)
The following function is used to store data in 4 consecutive bytes.
static float mach_float_read(const byte *b)
Reads a float.
static uint16_t mach_encode_2(ulint n)
The following function is used to convert a 16-bit data item to the canonical format,...
static void mach_write_to_6(byte *b, uint64_t id)
The following function is used to store data in 6 consecutive bytes.
uint32_t mach_parse_compressed(const byte **ptr, const byte *end_ptr)
Read a 32-bit integer in a compressed form.
Definition: mach0data.cc:79
static uint32_t mach_get_compressed_size(ulint n)
Return the size of an ulint when written in the compressed form.
static void mach_float_write(byte *b, float d)
Writes a float.
static uint64_t mach_read_from_6(const byte *b)
The following function is used to fetch data from 6 consecutive bytes.
static ulint mach_read_from_2_little_endian(const byte *buf)
Reads a ulint stored in the little-endian format.
static uint32_t mach_read_from_3(const byte *b)
The following function is used to fetch data from 3 consecutive bytes.
static uint64_t mach_read_int_type(const byte *src, ulint len, bool unsigned_type)
Convert integral type from storage byte order (big endian) to host byte order.
static uint64_t mach_read_from_8(const byte *b)
The following function is used to fetch data from 8 consecutive bytes.
static uint32_t mach_read_next_compressed(const byte **b)
Read a 32-bit integer in a compressed form.
static uint64_t mach_u64_parse_compressed(const byte **ptr, const byte *end_ptr)
Read a 64-bit integer in a compressed form.
static void mach_double_write(byte *b, double d)
Writes a double.
Utilities for converting data from the database file to the machine format.
Mini-transaction buffer global types.
mlog_id_t
Definition: mtr0types.h:62
unsigned long long int ulonglong
Definition: my_inttypes.h:55
uint16_t uint16
Definition: my_inttypes.h:64
Definition: buf0block_hint.cc:29
required string type
Definition: replication_group_member_actions.proto:33
Version control for database, common definitions, and include files.
unsigned long int ulint
Definition: univ.i:405
Utilities for byte operations.
int n
Definition: xcom_base.cc:508