1 /*****************************************************************************
2 
3 Copyright (c) 1995, 2020, Oracle and/or its affiliates. All Rights Reserved.
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 also distributed with certain software (including but not
10 limited to OpenSSL) that is licensed under separate terms, as designated in a
11 particular file or component or in included license documentation. The authors
12 of MySQL hereby grant you an additional permission to link the program and
13 your derivative works with the separately licensed software that they have
14 included with MySQL.
15 
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19 for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24 
25 *****************************************************************************/
26 
27 /** @file include/mtr0log.h
28  Mini-transaction logging routines
29 
30  Created 12/7/1995 Heikki Tuuri
31  *******************************************************/
32 
33 #ifndef mtr0log_h
34 #define mtr0log_h
35 
36 #include "dyn0buf.h"
37 #include "mtr0mtr.h"
38 #include "univ.i"
39 
40 // Forward declaration
41 struct dict_index_t;
42 
43 /** Writes 1, 2 or 4 bytes to a file page. Writes the corresponding log
44  record to the mini-transaction log if mtr is not NULL. */
45 void mlog_write_ulint(
46     byte *ptr,      /*!< in: pointer where to write */
47     ulint val,      /*!< in: value to write */
48     mlog_id_t type, /*!< in: MLOG_1BYTE, MLOG_2BYTES, MLOG_4BYTES */
49     mtr_t *mtr);    /*!< in: mini-transaction handle */
50 
51 /** Writes 8 bytes to a file page. Writes the corresponding log
52  record to the mini-transaction log, only if mtr is not NULL */
53 void mlog_write_ull(byte *ptr,       /*!< in: pointer where to write */
54                     ib_uint64_t val, /*!< in: value to write */
55                     mtr_t *mtr);     /*!< in: mini-transaction handle */
56 /** Writes a string to a file page buffered in the buffer pool. Writes the
57  corresponding log record to the mini-transaction log. */
58 void mlog_write_string(byte *ptr,       /*!< in: pointer where to write */
59                        const byte *str, /*!< in: string to write */
60                        ulint len,       /*!< in: string length */
61                        mtr_t *mtr);     /*!< in: mini-transaction handle */
62 /** Logs a write of a string to a file page buffered in the buffer pool.
63  Writes the corresponding log record to the mini-transaction log. */
64 void mlog_log_string(byte *ptr,   /*!< in: pointer written to */
65                      ulint len,   /*!< in: string length */
66                      mtr_t *mtr); /*!< in: mini-transaction handle */
67 /** Writes initial part of a log record consisting of one-byte item
68  type and four-byte space and page numbers. */
69 void mlog_write_initial_log_record(
70     const byte *ptr, /*!< in: pointer to (inside) a buffer
71                      frame holding the file page where
72                      modification is made */
73     mlog_id_t type,  /*!< in: log item type: MLOG_1BYTE, ... */
74     mtr_t *mtr);     /*!< in: mini-transaction handle */
75 
76 /** Catenates 1 - 4 bytes to the mtr log. The value is not compressed.
77 @param[in,out]	dyn_buf	buffer to write
78 @param[in]	val	value to write
79 @param[in]	type	type of value to write */
80 UNIV_INLINE
81 void mlog_catenate_ulint(mtr_buf_t *dyn_buf, ulint val, mlog_id_t type);
82 
83 /** Catenates 1 - 4 bytes to the mtr log.
84 @param[in]	mtr	mtr
85 @param[in]	val	value to write
86 @param[in]	type	MLOG_1BYTE, MLOG_2BYTES, MLOG_4BYTES */
87 UNIV_INLINE
88 void mlog_catenate_ulint(mtr_t *mtr, ulint val, mlog_id_t type);
89 
90 /** Catenates n bytes to the mtr log. */
91 void mlog_catenate_string(mtr_t *mtr,      /*!< in: mtr */
92                           const byte *str, /*!< in: string to write */
93                           ulint len);      /*!< in: string length */
94 
95 /** Catenates a compressed ulint to mlog.
96 @param[in]	mtr	mtr
97 @param[in]	val	value to write */
98 UNIV_INLINE
99 void mlog_catenate_ulint_compressed(mtr_t *mtr, ulint val);
100 
101 /** Catenates a compressed 64-bit integer to mlog.
102 @param[in]	mtr	mtr
103 @param[in]	val	value to write */
104 UNIV_INLINE
105 void mlog_catenate_ull_compressed(mtr_t *mtr, ib_uint64_t val);
106 
107 /** Opens a buffer to mlog. It must be closed with mlog_close.
108 @param[in,out]	mtr	mtr
109 @param[in]	size	buffer size in bytes; MUST be smaller than
110                         DYN_ARRAY_DATA_SIZE!
111 @param[out]	buffer  mlog buffer pointer if opened successfully
112 @retval true if opened successfully.
113 @retval false if not opened. One case is when redo is disabled for mtr. */
114 UNIV_INLINE MY_ATTRIBUTE((warn_unused_result)) bool mlog_open(mtr_t *mtr,
115                                                               ulint size,
116                                                               byte *&buffer);
117 
118 /** Opens a buffer to mlog. It must be closed with mlog_close.
119 This is used for writing log for metadata changes
120 @param[in,out]	mtr	mtr
121 @param[in]	size	buffer size in bytes; MUST be smaller than
122                         DYN_ARRAY_DATA_SIZE!
123 @param[out]	buffer  mlog buffer pointer if opened successfully
124 @retval true if opened successfully.
125 @retval false if not opened. One case is when redo is disabled for mtr. */
126 UNIV_INLINE MY_ATTRIBUTE((warn_unused_result)) bool mlog_open_metadata(
127     mtr_t *mtr, ulint size, byte *&buffer);
128 
129 /** Closes a buffer opened to mlog.
130 @param[in]	mtr	mtr
131 @param[in]	ptr	buffer space from ptr up was not used */
132 UNIV_INLINE
133 void mlog_close(mtr_t *mtr, byte *ptr);
134 
135 /** Writes a log record about a dictionary operation, which would cost
136 at most 23 bytes.
137 @param[in]	type		redo log record type
138 @param[in]	id		table id
139 @param[in]	version		table dynamic metadata version
140 @param[in,out]	log_ptr		current end of mini-transaction log
141 @param[in,out]	mtr		mini-transaction
142 @return end of mini-transaction log */
143 UNIV_INLINE
144 byte *mlog_write_initial_dict_log_record(mlog_id_t type, table_id_t id,
145                                          uint64_t version, byte *log_ptr,
146                                          mtr_t *mtr);
147 
148 /** Writes a log record about an operation.
149 @param[in]	type		redo log record type
150 @param[in]	space_id	tablespace identifier
151 @param[in]	page_no		page number
152 @param[in,out]	log_ptr		current end of mini-transaction log
153 @param[in,out]	mtr		mini-transaction
154 @return	end of mini-transaction log */
155 UNIV_INLINE
156 byte *mlog_write_initial_log_record_low(mlog_id_t type, space_id_t space_id,
157                                         page_no_t page_no, byte *log_ptr,
158                                         mtr_t *mtr);
159 
160 #ifndef UNIV_HOTBACKUP
161 /** Writes the initial part of a log record (3..11 bytes).
162 If the implementation of this function is changed, all size parameters to
163 mlog_open() should be adjusted accordingly!
164 @param[in]	ptr	pointer to (inside) a buffer frame holding the file
165                         page where modification is made
166 @param[in]	type	log item type: MLOG_1BYTE, ...
167 @param[in]	log_ptr	pointer to mtr log which has been opened
168 @param[in]	mtr	mtr
169 @return new value of log_ptr */
170 UNIV_INLINE
171 byte *mlog_write_initial_log_record_fast(const byte *ptr, mlog_id_t type,
172                                          byte *log_ptr, mtr_t *mtr);
173 #else /* !UNIV_HOTBACKUP */
174 #define mlog_write_initial_log_record(ptr, type, mtr) ((void)0)
175 #define mlog_write_initial_log_record_fast(ptr, type, log_ptr, mtr) ((byte *)0)
176 #endif /* !UNIV_HOTBACKUP */
177 
178 /** Parses an initial log record written by mlog_write_initial_dict_log_record.
179 @param[in]	ptr		buffer
180 @param[in]	end_ptr		buffer end
181 @param[out]	type		log record type, should be
182                                 MLOG_TABLE_DYNAMIC_META
183 @param[out]	id		table id
184 @param[out]	version		table dynamic metadata version
185 @return parsed record end, NULL if not a complete record */
186 byte *mlog_parse_initial_dict_log_record(const byte *ptr, const byte *end_ptr,
187                                          mlog_id_t *type, table_id_t *id,
188                                          uint64 *version);
189 
190 /** Parses an initial log record written by mlog_write_initial_log_record.
191  @return parsed record end, NULL if not a complete record */
192 byte *mlog_parse_initial_log_record(
193     const byte *ptr,     /*!< in: buffer */
194     const byte *end_ptr, /*!< in: buffer end */
195     mlog_id_t *type,     /*!< out: log record type: MLOG_1BYTE, ... */
196     space_id_t *space,   /*!< out: space id */
197     page_no_t *page_no); /*!< out: page number */
198 /** Parses a log record written by mlog_write_ulint or mlog_write_ull.
199  @return parsed record end, NULL if not a complete record */
200 byte *mlog_parse_nbytes(
201     mlog_id_t type,      /*!< in: log record type: MLOG_1BYTE, ... */
202     const byte *ptr,     /*!< in: buffer */
203     const byte *end_ptr, /*!< in: buffer end */
204     byte *page,          /*!< in: page where to apply the log record,
205                          or NULL */
206     void *page_zip);     /*!< in/out: compressed page, or NULL */
207 /** Parses a log record written by mlog_write_string.
208  @return parsed record end, NULL if not a complete record */
209 byte *mlog_parse_string(
210     byte *ptr,       /*!< in: buffer */
211     byte *end_ptr,   /*!< in: buffer end */
212     byte *page,      /*!< in: page where to apply the log record, or NULL */
213     void *page_zip); /*!< in/out: compressed page, or NULL */
214 
215 /** Opens a buffer for mlog, writes the initial log record and,
216 if needed, the field lengths of an index.  Reserves space
217 for further log entries.  The log entry must be closed with
218 mtr_close().
219 @param[in,out]	mtr	mini transaction
220 @param[in]	rec	index record or page
221 @param[in]	index	record descriptor
222 @param[in]	type	log item type
223 @param[in]	size	requested buffer size in bytes. if 0, calls
224                         mlog_close() and returns false.
225 @param[out]	log_ptr	log buffer pointer
226 @retval true if opened successfully.
227 @retval false if not opened. One case is when redo is disabled for mtr. */
228 bool mlog_open_and_write_index(mtr_t *mtr, const byte *rec,
229                                const dict_index_t *index, mlog_id_t type,
230                                ulint size, byte *&log_ptr);
231 
232 /** Parses a log record written by mlog_open_and_write_index.
233  @return parsed record end, NULL if not a complete record */
234 byte *mlog_parse_index(byte *ptr,           /*!< in: buffer */
235                        const byte *end_ptr, /*!< in: buffer end */
236                        ibool comp, /*!< in: TRUE=compact record format */
237                        dict_index_t **index); /*!< out, own: dummy index */
238 
239 /** Insert, update, and maybe other functions may use this value to define an
240 extra mlog buffer size for variable size data */
241 #define MLOG_BUF_MARGIN 256
242 
243 #include "mtr0log.ic"
244 
245 #endif /* mtr0log_h */
246