1 /*****************************************************************************
2 
3 Copyright (c) 1996, 2016, Oracle and/or its affiliates. All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8 
9 This program is also distributed 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 included with MySQL.
15 
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License, version 2.0, 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 Street, Suite 500, Boston, MA 02110-1335 USA
24 
25 *****************************************************************************/
26 
27 /**************************************************//**
28 @file include/dict0boot.h
29 Data dictionary creation and booting
30 
31 Created 4/18/1996 Heikki Tuuri
32 *******************************************************/
33 
34 #ifndef dict0boot_h
35 #define dict0boot_h
36 
37 #include "univ.i"
38 
39 #include "mtr0mtr.h"
40 #include "mtr0log.h"
41 #include "ut0byte.h"
42 #include "buf0buf.h"
43 #include "fsp0fsp.h"
44 #include "dict0dict.h"
45 
46 typedef	byte	dict_hdr_t;
47 
48 /**********************************************************************//**
49 Gets a pointer to the dictionary header and x-latches its page.
50 @return	pointer to the dictionary header, page x-latched */
51 UNIV_INTERN
52 dict_hdr_t*
53 dict_hdr_get(
54 /*=========*/
55 	mtr_t*	mtr);	/*!< in: mtr */
56 /**********************************************************************//**
57 Returns a new table, index, or space id. */
58 UNIV_INTERN
59 void
60 dict_hdr_get_new_id(
61 /*================*/
62 	table_id_t*	table_id,	/*!< out: table id
63 					(not assigned if NULL) */
64 	index_id_t*	index_id,	/*!< out: index id
65 					(not assigned if NULL) */
66 	ulint*		space_id);	/*!< out: space id
67 					(not assigned if NULL) */
68 /**********************************************************************//**
69 Writes the current value of the row id counter to the dictionary header file
70 page. */
71 UNIV_INTERN
72 void
73 dict_hdr_flush_row_id(void);
74 /*=======================*/
75 /**********************************************************************//**
76 Returns a new row id.
77 @return	the new id */
78 UNIV_INLINE
79 row_id_t
80 dict_sys_get_new_row_id(void);
81 /*=========================*/
82 /**********************************************************************//**
83 Reads a row id from a record or other 6-byte stored form.
84 @return	row id */
85 UNIV_INLINE
86 row_id_t
87 dict_sys_read_row_id(
88 /*=================*/
89 	const byte*	field);	/*!< in: record field */
90 /**********************************************************************//**
91 Writes a row id to a record or other 6-byte stored form. */
92 UNIV_INLINE
93 void
94 dict_sys_write_row_id(
95 /*==================*/
96 	byte*		field,	/*!< in: record field */
97 	row_id_t	row_id);/*!< in: row id */
98 /*****************************************************************//**
99 Initializes the data dictionary memory structures when the database is
100 started. This function is also called when the data dictionary is created.
101 @return DB_SUCCESS or error code. */
102 UNIV_INTERN
103 dberr_t
104 dict_boot(void)
105 /*===========*/
106 	MY_ATTRIBUTE((warn_unused_result));
107 
108 /*****************************************************************//**
109 Creates and initializes the data dictionary at the server bootstrap.
110 @return DB_SUCCESS or error code. */
111 UNIV_INTERN
112 dberr_t
113 dict_create(void)
114 /*=============*/
115 	MY_ATTRIBUTE((warn_unused_result));
116 
117 /*********************************************************************//**
118 Check if a table id belongs to  system table.
119 @return true if the table id belongs to a system table. */
120 UNIV_INLINE
121 bool
122 dict_is_sys_table(
123 /*==============*/
124 	table_id_t	id)		/*!< in: table id to check */
125 	MY_ATTRIBUTE((warn_unused_result));
126 
127 /* Space id and page no where the dictionary header resides */
128 #define	DICT_HDR_SPACE		0	/* the SYSTEM tablespace */
129 #define	DICT_HDR_PAGE_NO	FSP_DICT_HDR_PAGE_NO
130 
131 /* The ids for the basic system tables and their indexes */
132 #define DICT_TABLES_ID		1
133 #define DICT_COLUMNS_ID		2
134 #define DICT_INDEXES_ID		3
135 #define DICT_FIELDS_ID		4
136 /* The following is a secondary index on SYS_TABLES */
137 #define DICT_TABLE_IDS_ID	5
138 
139 #define	DICT_HDR_FIRST_ID	10	/* the ids for tables etc. start
140 					from this number, except for basic
141 					system tables and their above defined
142 					indexes; ibuf tables and indexes are
143 					assigned as the id the number
144 					DICT_IBUF_ID_MIN plus the space id */
145 
146 /* The offset of the dictionary header on the page */
147 #define	DICT_HDR		FSEG_PAGE_DATA
148 
149 /*-------------------------------------------------------------*/
150 /* Dictionary header offsets */
151 #define DICT_HDR_ROW_ID		0	/* The latest assigned row id */
152 #define DICT_HDR_TABLE_ID	8	/* The latest assigned table id */
153 #define DICT_HDR_INDEX_ID	16	/* The latest assigned index id */
154 #define DICT_HDR_MAX_SPACE_ID	24	/* The latest assigned space id,or 0*/
155 #define DICT_HDR_MIX_ID_LOW	28	/* Obsolete,always DICT_HDR_FIRST_ID*/
156 #define DICT_HDR_TABLES		32	/* Root of SYS_TABLES clust index */
157 #define DICT_HDR_TABLE_IDS	36	/* Root of SYS_TABLE_IDS sec index */
158 #define DICT_HDR_COLUMNS	40	/* Root of SYS_COLUMNS clust index */
159 #define DICT_HDR_INDEXES	44	/* Root of SYS_INDEXES clust index */
160 #define DICT_HDR_FIELDS		48	/* Root of SYS_FIELDS clust index */
161 
162 #define DICT_HDR_FSEG_HEADER	56	/* Segment header for the tablespace
163 					segment into which the dictionary
164 					header is created */
165 /*-------------------------------------------------------------*/
166 
167 /* The columns in SYS_TABLES */
168 enum dict_col_sys_tables_enum {
169 	DICT_COL__SYS_TABLES__NAME		= 0,
170 	DICT_COL__SYS_TABLES__ID		= 1,
171 	DICT_COL__SYS_TABLES__N_COLS		= 2,
172 	DICT_COL__SYS_TABLES__TYPE		= 3,
173 	DICT_COL__SYS_TABLES__MIX_ID		= 4,
174 	DICT_COL__SYS_TABLES__MIX_LEN		= 5,
175 	DICT_COL__SYS_TABLES__CLUSTER_ID	= 6,
176 	DICT_COL__SYS_TABLES__SPACE		= 7,
177 	DICT_NUM_COLS__SYS_TABLES		= 8
178 };
179 /* The field numbers in the SYS_TABLES clustered index */
180 enum dict_fld_sys_tables_enum {
181 	DICT_FLD__SYS_TABLES__NAME		= 0,
182 	DICT_FLD__SYS_TABLES__DB_TRX_ID		= 1,
183 	DICT_FLD__SYS_TABLES__DB_ROLL_PTR	= 2,
184 	DICT_FLD__SYS_TABLES__ID		= 3,
185 	DICT_FLD__SYS_TABLES__N_COLS		= 4,
186 	DICT_FLD__SYS_TABLES__TYPE		= 5,
187 	DICT_FLD__SYS_TABLES__MIX_ID		= 6,
188 	DICT_FLD__SYS_TABLES__MIX_LEN		= 7,
189 	DICT_FLD__SYS_TABLES__CLUSTER_ID	= 8,
190 	DICT_FLD__SYS_TABLES__SPACE		= 9,
191 	DICT_NUM_FIELDS__SYS_TABLES		= 10
192 };
193 /* The field numbers in the SYS_TABLE_IDS index */
194 enum dict_fld_sys_table_ids_enum {
195 	DICT_FLD__SYS_TABLE_IDS__ID		= 0,
196 	DICT_FLD__SYS_TABLE_IDS__NAME		= 1,
197 	DICT_NUM_FIELDS__SYS_TABLE_IDS		= 2
198 };
199 /* The columns in SYS_COLUMNS */
200 enum dict_col_sys_columns_enum {
201 	DICT_COL__SYS_COLUMNS__TABLE_ID		= 0,
202 	DICT_COL__SYS_COLUMNS__POS		= 1,
203 	DICT_COL__SYS_COLUMNS__NAME		= 2,
204 	DICT_COL__SYS_COLUMNS__MTYPE		= 3,
205 	DICT_COL__SYS_COLUMNS__PRTYPE		= 4,
206 	DICT_COL__SYS_COLUMNS__LEN		= 5,
207 	DICT_COL__SYS_COLUMNS__PREC		= 6,
208 	DICT_NUM_COLS__SYS_COLUMNS		= 7
209 };
210 /* The field numbers in the SYS_COLUMNS clustered index */
211 enum dict_fld_sys_columns_enum {
212 	DICT_FLD__SYS_COLUMNS__TABLE_ID		= 0,
213 	DICT_FLD__SYS_COLUMNS__POS		= 1,
214 	DICT_FLD__SYS_COLUMNS__DB_TRX_ID	= 2,
215 	DICT_FLD__SYS_COLUMNS__DB_ROLL_PTR	= 3,
216 	DICT_FLD__SYS_COLUMNS__NAME		= 4,
217 	DICT_FLD__SYS_COLUMNS__MTYPE		= 5,
218 	DICT_FLD__SYS_COLUMNS__PRTYPE		= 6,
219 	DICT_FLD__SYS_COLUMNS__LEN		= 7,
220 	DICT_FLD__SYS_COLUMNS__PREC		= 8,
221 	DICT_NUM_FIELDS__SYS_COLUMNS		= 9
222 };
223 /* The columns in SYS_INDEXES */
224 enum dict_col_sys_indexes_enum {
225 	DICT_COL__SYS_INDEXES__TABLE_ID		= 0,
226 	DICT_COL__SYS_INDEXES__ID		= 1,
227 	DICT_COL__SYS_INDEXES__NAME		= 2,
228 	DICT_COL__SYS_INDEXES__N_FIELDS		= 3,
229 	DICT_COL__SYS_INDEXES__TYPE		= 4,
230 	DICT_COL__SYS_INDEXES__SPACE		= 5,
231 	DICT_COL__SYS_INDEXES__PAGE_NO		= 6,
232 	DICT_NUM_COLS__SYS_INDEXES		= 7
233 };
234 /* The field numbers in the SYS_INDEXES clustered index */
235 enum dict_fld_sys_indexes_enum {
236 	DICT_FLD__SYS_INDEXES__TABLE_ID		= 0,
237 	DICT_FLD__SYS_INDEXES__ID		= 1,
238 	DICT_FLD__SYS_INDEXES__DB_TRX_ID	= 2,
239 	DICT_FLD__SYS_INDEXES__DB_ROLL_PTR	= 3,
240 	DICT_FLD__SYS_INDEXES__NAME		= 4,
241 	DICT_FLD__SYS_INDEXES__N_FIELDS		= 5,
242 	DICT_FLD__SYS_INDEXES__TYPE		= 6,
243 	DICT_FLD__SYS_INDEXES__SPACE		= 7,
244 	DICT_FLD__SYS_INDEXES__PAGE_NO		= 8,
245 	DICT_NUM_FIELDS__SYS_INDEXES		= 9
246 };
247 /* The columns in SYS_FIELDS */
248 enum dict_col_sys_fields_enum {
249 	DICT_COL__SYS_FIELDS__INDEX_ID		= 0,
250 	DICT_COL__SYS_FIELDS__POS		= 1,
251 	DICT_COL__SYS_FIELDS__COL_NAME		= 2,
252 	DICT_NUM_COLS__SYS_FIELDS		= 3
253 };
254 /* The field numbers in the SYS_FIELDS clustered index */
255 enum dict_fld_sys_fields_enum {
256 	DICT_FLD__SYS_FIELDS__INDEX_ID		= 0,
257 	DICT_FLD__SYS_FIELDS__POS		= 1,
258 	DICT_FLD__SYS_FIELDS__DB_TRX_ID		= 2,
259 	DICT_FLD__SYS_FIELDS__DB_ROLL_PTR	= 3,
260 	DICT_FLD__SYS_FIELDS__COL_NAME		= 4,
261 	DICT_NUM_FIELDS__SYS_FIELDS		= 5
262 };
263 /* The columns in SYS_FOREIGN */
264 enum dict_col_sys_foreign_enum {
265 	DICT_COL__SYS_FOREIGN__ID		= 0,
266 	DICT_COL__SYS_FOREIGN__FOR_NAME		= 1,
267 	DICT_COL__SYS_FOREIGN__REF_NAME		= 2,
268 	DICT_COL__SYS_FOREIGN__N_COLS		= 3,
269 	DICT_NUM_COLS__SYS_FOREIGN		= 4
270 };
271 /* The field numbers in the SYS_FOREIGN clustered index */
272 enum dict_fld_sys_foreign_enum {
273 	DICT_FLD__SYS_FOREIGN__ID		= 0,
274 	DICT_FLD__SYS_FOREIGN__DB_TRX_ID	= 1,
275 	DICT_FLD__SYS_FOREIGN__DB_ROLL_PTR	= 2,
276 	DICT_FLD__SYS_FOREIGN__FOR_NAME		= 3,
277 	DICT_FLD__SYS_FOREIGN__REF_NAME		= 4,
278 	DICT_FLD__SYS_FOREIGN__N_COLS		= 5,
279 	DICT_NUM_FIELDS__SYS_FOREIGN		= 6
280 };
281 /* The field numbers in the SYS_FOREIGN_FOR_NAME secondary index */
282 enum dict_fld_sys_foreign_for_name_enum {
283 	DICT_FLD__SYS_FOREIGN_FOR_NAME__NAME	= 0,
284 	DICT_FLD__SYS_FOREIGN_FOR_NAME__ID	= 1,
285 	DICT_NUM_FIELDS__SYS_FOREIGN_FOR_NAME	= 2
286 };
287 /* The columns in SYS_FOREIGN_COLS */
288 enum dict_col_sys_foreign_cols_enum {
289 	DICT_COL__SYS_FOREIGN_COLS__ID			= 0,
290 	DICT_COL__SYS_FOREIGN_COLS__POS			= 1,
291 	DICT_COL__SYS_FOREIGN_COLS__FOR_COL_NAME	= 2,
292 	DICT_COL__SYS_FOREIGN_COLS__REF_COL_NAME	= 3,
293 	DICT_NUM_COLS__SYS_FOREIGN_COLS			= 4
294 };
295 /* The field numbers in the SYS_FOREIGN_COLS clustered index */
296 enum dict_fld_sys_foreign_cols_enum {
297 	DICT_FLD__SYS_FOREIGN_COLS__ID			= 0,
298 	DICT_FLD__SYS_FOREIGN_COLS__POS			= 1,
299 	DICT_FLD__SYS_FOREIGN_COLS__DB_TRX_ID		= 2,
300 	DICT_FLD__SYS_FOREIGN_COLS__DB_ROLL_PTR		= 3,
301 	DICT_FLD__SYS_FOREIGN_COLS__FOR_COL_NAME	= 4,
302 	DICT_FLD__SYS_FOREIGN_COLS__REF_COL_NAME	= 5,
303 	DICT_NUM_FIELDS__SYS_FOREIGN_COLS		= 6
304 };
305 /* The columns in SYS_TABLESPACES */
306 enum dict_col_sys_tablespaces_enum {
307 	DICT_COL__SYS_TABLESPACES__SPACE		= 0,
308 	DICT_COL__SYS_TABLESPACES__NAME			= 1,
309 	DICT_COL__SYS_TABLESPACES__FLAGS		= 2,
310 	DICT_NUM_COLS__SYS_TABLESPACES			= 3
311 };
312 /* The field numbers in the SYS_TABLESPACES clustered index */
313 enum dict_fld_sys_tablespaces_enum {
314 	DICT_FLD__SYS_TABLESPACES__SPACE		= 0,
315 	DICT_FLD__SYS_TABLESPACES__DB_TRX_ID		= 1,
316 	DICT_FLD__SYS_TABLESPACES__DB_ROLL_PTR		= 2,
317 	DICT_FLD__SYS_TABLESPACES__NAME			= 3,
318 	DICT_FLD__SYS_TABLESPACES__FLAGS		= 4,
319 	DICT_NUM_FIELDS__SYS_TABLESPACES		= 5
320 };
321 /* The columns in SYS_DATAFILES */
322 enum dict_col_sys_datafiles_enum {
323 	DICT_COL__SYS_DATAFILES__SPACE			= 0,
324 	DICT_COL__SYS_DATAFILES__PATH			= 1,
325 	DICT_NUM_COLS__SYS_DATAFILES			= 2
326 };
327 /* The field numbers in the SYS_DATAFILES clustered index */
328 enum dict_fld_sys_datafiles_enum {
329 	DICT_FLD__SYS_DATAFILES__SPACE			= 0,
330 	DICT_FLD__SYS_DATAFILES__DB_TRX_ID		= 1,
331 	DICT_FLD__SYS_DATAFILES__DB_ROLL_PTR		= 2,
332 	DICT_FLD__SYS_DATAFILES__PATH			= 3,
333 	DICT_NUM_FIELDS__SYS_DATAFILES			= 4
334 };
335 
336 /* A number of the columns above occur in multiple tables.  These are the
337 length of thos fields. */
338 #define	DICT_FLD_LEN_SPACE	4
339 #define	DICT_FLD_LEN_FLAGS	4
340 
341 /* When a row id which is zero modulo this number (which must be a power of
342 two) is assigned, the field DICT_HDR_ROW_ID on the dictionary header page is
343 updated */
344 #define DICT_HDR_ROW_ID_WRITE_MARGIN	256
345 
346 #ifndef UNIV_NONINL
347 #include "dict0boot.ic"
348 #endif
349 
350 #endif
351