You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1057 lines
36 KiB

20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
  1. /******************************************************
  2. Data dictionary system
  3. (c) 1996 Innobase Oy
  4. Created 1/8/1996 Heikki Tuuri
  5. *******************************************************/
  6. #ifndef dict0dict_h
  7. #define dict0dict_h
  8. #include "univ.i"
  9. #include "dict0types.h"
  10. #include "dict0mem.h"
  11. #include "data0type.h"
  12. #include "data0data.h"
  13. #include "sync0sync.h"
  14. #include "sync0rw.h"
  15. #include "mem0mem.h"
  16. #include "rem0types.h"
  17. #include "btr0types.h"
  18. #include "ut0mem.h"
  19. #include "ut0lst.h"
  20. #include "hash0hash.h"
  21. #include "ut0rnd.h"
  22. #include "ut0byte.h"
  23. #include "trx0types.h"
  24. #ifndef UNIV_HOTBACKUP
  25. /**********************************************************************
  26. Makes all characters in a NUL-terminated UTF-8 string lower case. */
  27. void
  28. dict_casedn_str(
  29. /*============*/
  30. char* a); /* in/out: string to put in lower case */
  31. #endif /* !UNIV_HOTBACKUP */
  32. /************************************************************************
  33. Get the database name length in a table name. */
  34. ulint
  35. dict_get_db_name_len(
  36. /*=================*/
  37. /* out: database name length */
  38. const char* name); /* in: table name in the form
  39. dbname '/' tablename */
  40. /************************************************************************
  41. Return the end of table name where we have removed dbname and '/'. */
  42. const char*
  43. dict_remove_db_name(
  44. /*================*/
  45. /* out: table name */
  46. const char* name); /* in: table name in the form
  47. dbname '/' tablename */
  48. /************************************************************************
  49. Decrements the count of open MySQL handles to a table. */
  50. void
  51. dict_table_decrement_handle_count(
  52. /*==============================*/
  53. dict_table_t* table); /* in: table */
  54. /**************************************************************************
  55. Inits the data dictionary module. */
  56. void
  57. dict_init(void);
  58. /*===========*/
  59. /************************************************************************
  60. Gets the space id of every table of the data dictionary and makes a linear
  61. list and a hash table of them to the data dictionary cache. This function
  62. can be called at database startup if we did not need to do a crash recovery.
  63. In crash recovery we must scan the space id's from the .ibd files in MySQL
  64. database directories. */
  65. void
  66. dict_load_space_id_list(void);
  67. /*=========================*/
  68. /*************************************************************************
  69. Gets the column data type. */
  70. UNIV_INLINE
  71. void
  72. dict_col_copy_type(
  73. /*===============*/
  74. const dict_col_t* col, /* in: column */
  75. dtype_t* type); /* out: data type */
  76. /*************************************************************************
  77. Gets the column data type. */
  78. void
  79. dict_col_copy_type_noninline(
  80. /*=========================*/
  81. const dict_col_t* col, /* in: column */
  82. dtype_t* type); /* out: data type */
  83. /***************************************************************************
  84. Returns the minimum size of the column. */
  85. UNIV_INLINE
  86. ulint
  87. dict_col_get_min_size(
  88. /*==================*/
  89. /* out: minimum size */
  90. const dict_col_t* col); /* in: column */
  91. /***************************************************************************
  92. Returns the maximum size of the column. */
  93. UNIV_INLINE
  94. ulint
  95. dict_col_get_max_size(
  96. /*==================*/
  97. /* out: maximum size */
  98. const dict_col_t* col); /* in: column */
  99. /***************************************************************************
  100. Returns the size of a fixed size column, 0 if not a fixed size column. */
  101. UNIV_INLINE
  102. ulint
  103. dict_col_get_fixed_size(
  104. /*====================*/
  105. /* out: fixed size, or 0 */
  106. const dict_col_t* col); /* in: column */
  107. /***************************************************************************
  108. Returns the ROW_FORMAT=REDUNDANT stored SQL NULL size of a column.
  109. For fixed length types it is the fixed length of the type, otherwise 0. */
  110. UNIV_INLINE
  111. ulint
  112. dict_col_get_sql_null_size(
  113. /*=======================*/
  114. /* out: SQL null storage size
  115. in ROW_FORMAT=REDUNDANT */
  116. const dict_col_t* col); /* in: column */
  117. /*************************************************************************
  118. Gets the column number. */
  119. UNIV_INLINE
  120. ulint
  121. dict_col_get_no(
  122. /*============*/
  123. const dict_col_t* col);
  124. /*************************************************************************
  125. Gets the column position in the clustered index. */
  126. UNIV_INLINE
  127. ulint
  128. dict_col_get_clust_pos(
  129. /*===================*/
  130. const dict_col_t* col, /* in: table column */
  131. const dict_index_t* clust_index); /* in: clustered index */
  132. /*************************************************************************
  133. Gets the column position in the clustered index. */
  134. ulint
  135. dict_col_get_clust_pos_noninline(
  136. /*=============================*/
  137. const dict_col_t* col, /* in: table column */
  138. const dict_index_t* clust_index); /* in: clustered index */
  139. /********************************************************************
  140. If the given column name is reserved for InnoDB system columns, return
  141. TRUE. */
  142. ibool
  143. dict_col_name_is_reserved(
  144. /*======================*/
  145. /* out: TRUE if name is reserved */
  146. const char* name); /* in: column name */
  147. /************************************************************************
  148. Initializes the autoinc counter. It is not an error to initialize an already
  149. initialized counter. */
  150. void
  151. dict_table_autoinc_initialize(
  152. /*==========================*/
  153. dict_table_t* table, /* in: table */
  154. ib_longlong value); /* in: next value to assign to a row */
  155. /************************************************************************
  156. Gets the next autoinc value (== autoinc counter value), 0 if not yet
  157. initialized. If initialized, increments the counter by 1. */
  158. ib_longlong
  159. dict_table_autoinc_get(
  160. /*===================*/
  161. /* out: value for a new row, or 0 */
  162. dict_table_t* table); /* in: table */
  163. /************************************************************************
  164. Decrements the autoinc counter value by 1. */
  165. void
  166. dict_table_autoinc_decrement(
  167. /*=========================*/
  168. dict_table_t* table); /* in: table */
  169. /************************************************************************
  170. Reads the next autoinc value (== autoinc counter value), 0 if not yet
  171. initialized. */
  172. ib_longlong
  173. dict_table_autoinc_read(
  174. /*====================*/
  175. /* out: value for a new row, or 0 */
  176. dict_table_t* table); /* in: table */
  177. /************************************************************************
  178. Peeks the autoinc counter value, 0 if not yet initialized. Does not
  179. increment the counter. The read not protected by any mutex! */
  180. ib_longlong
  181. dict_table_autoinc_peek(
  182. /*====================*/
  183. /* out: value of the counter */
  184. dict_table_t* table); /* in: table */
  185. /************************************************************************
  186. Updates the autoinc counter if the value supplied is equal or bigger than the
  187. current value. If not inited, does nothing. */
  188. void
  189. dict_table_autoinc_update(
  190. /*======================*/
  191. dict_table_t* table, /* in: table */
  192. ib_longlong value); /* in: value which was assigned to a row */
  193. /**************************************************************************
  194. Adds system columns to a table object. */
  195. void
  196. dict_table_add_system_columns(
  197. /*==========================*/
  198. dict_table_t* table, /* in/out: table */
  199. mem_heap_t* heap); /* in: temporary heap */
  200. /**************************************************************************
  201. Adds a table object to the dictionary cache. */
  202. void
  203. dict_table_add_to_cache(
  204. /*====================*/
  205. dict_table_t* table, /* in: table */
  206. mem_heap_t* heap); /* in: temporary heap */
  207. /**************************************************************************
  208. Removes a table object from the dictionary cache. */
  209. void
  210. dict_table_remove_from_cache(
  211. /*=========================*/
  212. dict_table_t* table); /* in, own: table */
  213. /**************************************************************************
  214. Renames a table object. */
  215. ibool
  216. dict_table_rename_in_cache(
  217. /*=======================*/
  218. /* out: TRUE if success */
  219. dict_table_t* table, /* in: table */
  220. const char* new_name, /* in: new name */
  221. ibool rename_also_foreigns);/* in: in ALTER TABLE we want
  222. to preserve the original table name
  223. in constraints which reference it */
  224. /**************************************************************************
  225. Change the id of a table object in the dictionary cache. This is used in
  226. DISCARD TABLESPACE. */
  227. void
  228. dict_table_change_id_in_cache(
  229. /*==========================*/
  230. dict_table_t* table, /* in: table object already in cache */
  231. dulint new_id);/* in: new id to set */
  232. /**************************************************************************
  233. Adds a foreign key constraint object to the dictionary cache. May free
  234. the object if there already is an object with the same identifier in.
  235. At least one of foreign table or referenced table must already be in
  236. the dictionary cache! */
  237. ulint
  238. dict_foreign_add_to_cache(
  239. /*======================*/
  240. /* out: DB_SUCCESS or error code */
  241. dict_foreign_t* foreign, /* in, own: foreign key constraint */
  242. ibool check_charsets);/* in: TRUE=check charset
  243. compatibility */
  244. /*************************************************************************
  245. Checks if a table is referenced by foreign keys. */
  246. ibool
  247. dict_table_referenced_by_foreign_key(
  248. /*=================================*/
  249. /* out: TRUE if table is referenced by a
  250. foreign key */
  251. dict_table_t* table); /* in: InnoDB table */
  252. /**************************************************************************
  253. Determines whether a string starts with the specified keyword. */
  254. ibool
  255. dict_str_starts_with_keyword(
  256. /*=========================*/
  257. /* out: TRUE if str starts
  258. with keyword */
  259. void* mysql_thd, /* in: MySQL thread handle */
  260. const char* str, /* in: string to scan for keyword */
  261. const char* keyword); /* in: keyword to look for */
  262. /*************************************************************************
  263. Scans a table create SQL string and adds to the data dictionary
  264. the foreign key constraints declared in the string. This function
  265. should be called after the indexes for a table have been created.
  266. Each foreign key constraint must be accompanied with indexes in
  267. bot participating tables. The indexes are allowed to contain more
  268. fields than mentioned in the constraint. */
  269. ulint
  270. dict_create_foreign_constraints(
  271. /*============================*/
  272. /* out: error code or DB_SUCCESS */
  273. trx_t* trx, /* in: transaction */
  274. const char* sql_string, /* in: table create statement where
  275. foreign keys are declared like:
  276. FOREIGN KEY (a, b) REFERENCES
  277. table2(c, d), table2 can be written
  278. also with the database
  279. name before it: test.table2; the
  280. default database id the database of
  281. parameter name */
  282. const char* name, /* in: table full name in the
  283. normalized form
  284. database_name/table_name */
  285. ibool reject_fks); /* in: if TRUE, fail with error
  286. code DB_CANNOT_ADD_CONSTRAINT if
  287. any foreign keys are found. */
  288. /**************************************************************************
  289. Parses the CONSTRAINT id's to be dropped in an ALTER TABLE statement. */
  290. ulint
  291. dict_foreign_parse_drop_constraints(
  292. /*================================*/
  293. /* out: DB_SUCCESS or
  294. DB_CANNOT_DROP_CONSTRAINT if
  295. syntax error or the constraint
  296. id does not match */
  297. mem_heap_t* heap, /* in: heap from which we can
  298. allocate memory */
  299. trx_t* trx, /* in: transaction */
  300. dict_table_t* table, /* in: table */
  301. ulint* n, /* out: number of constraints
  302. to drop */
  303. const char*** constraints_to_drop); /* out: id's of the
  304. constraints to drop */
  305. /**************************************************************************
  306. Returns a table object and optionally increment its MySQL open handle count.
  307. NOTE! This is a high-level function to be used mainly from outside the
  308. 'dict' directory. Inside this directory dict_table_get_low is usually the
  309. appropriate function. */
  310. dict_table_t*
  311. dict_table_get(
  312. /*===========*/
  313. /* out: table, NULL if
  314. does not exist */
  315. const char* table_name, /* in: table name */
  316. ibool inc_mysql_count);
  317. /* in: whether to increment the open
  318. handle count on the table */
  319. /**************************************************************************
  320. Returns a table object based on table id. */
  321. dict_table_t*
  322. dict_table_get_on_id(
  323. /*=================*/
  324. /* out: table, NULL if does not exist */
  325. dulint table_id, /* in: table id */
  326. trx_t* trx); /* in: transaction handle */
  327. /**************************************************************************
  328. Returns a table object based on table id. */
  329. UNIV_INLINE
  330. dict_table_t*
  331. dict_table_get_on_id_low(
  332. /*=====================*/
  333. /* out: table, NULL if does not exist */
  334. dulint table_id); /* in: table id */
  335. /**************************************************************************
  336. Checks if a table is in the dictionary cache. */
  337. UNIV_INLINE
  338. dict_table_t*
  339. dict_table_check_if_in_cache_low(
  340. /*=============================*/
  341. /* out: table, NULL if not found */
  342. const char* table_name); /* in: table name */
  343. /**************************************************************************
  344. Gets a table; loads it to the dictionary cache if necessary. A low-level
  345. function. */
  346. UNIV_INLINE
  347. dict_table_t*
  348. dict_table_get_low(
  349. /*===============*/
  350. /* out: table, NULL if not found */
  351. const char* table_name); /* in: table name */
  352. /**************************************************************************
  353. A noninlined version of dict_table_get_low. */
  354. dict_table_t*
  355. dict_table_get_low_noninlined(
  356. /*==========================*/
  357. /* out: table, NULL if not found */
  358. const char* table_name); /* in: table name */
  359. /**************************************************************************
  360. Returns an index object. */
  361. UNIV_INLINE
  362. dict_index_t*
  363. dict_table_get_index(
  364. /*=================*/
  365. /* out: index, NULL if does not exist */
  366. dict_table_t* table, /* in: table */
  367. const char* name); /* in: index name */
  368. /**************************************************************************
  369. Returns an index object. */
  370. dict_index_t*
  371. dict_table_get_index_noninline(
  372. /*===========================*/
  373. /* out: index, NULL if does not exist */
  374. dict_table_t* table, /* in: table */
  375. const char* name); /* in: index name */
  376. /**************************************************************************
  377. Returns a column's name. */
  378. const char*
  379. dict_table_get_col_name(
  380. /*====================*/
  381. /* out: column name. NOTE: not
  382. guaranteed to stay valid if table is
  383. modified in any way (columns added,
  384. etc.). */
  385. const dict_table_t* table, /* in: table */
  386. ulint col_nr);/* in: column number */
  387. /**************************************************************************
  388. Prints a table definition. */
  389. void
  390. dict_table_print(
  391. /*=============*/
  392. dict_table_t* table); /* in: table */
  393. /**************************************************************************
  394. Prints a table data. */
  395. void
  396. dict_table_print_low(
  397. /*=================*/
  398. dict_table_t* table); /* in: table */
  399. /**************************************************************************
  400. Prints a table data when we know the table name. */
  401. void
  402. dict_table_print_by_name(
  403. /*=====================*/
  404. const char* name);
  405. /**************************************************************************
  406. Outputs info on foreign keys of a table. */
  407. void
  408. dict_print_info_on_foreign_keys(
  409. /*============================*/
  410. ibool create_table_format, /* in: if TRUE then print in
  411. a format suitable to be inserted into
  412. a CREATE TABLE, otherwise in the format
  413. of SHOW TABLE STATUS */
  414. FILE* file, /* in: file where to print */
  415. trx_t* trx, /* in: transaction */
  416. dict_table_t* table); /* in: table */
  417. /**************************************************************************
  418. Outputs info on a foreign key of a table in a format suitable for
  419. CREATE TABLE. */
  420. void
  421. dict_print_info_on_foreign_key_in_create_format(
  422. /*============================================*/
  423. FILE* file, /* in: file where to print */
  424. trx_t* trx, /* in: transaction */
  425. dict_foreign_t* foreign, /* in: foreign key constraint */
  426. ibool add_newline); /* in: whether to add a newline */
  427. /************************************************************************
  428. Displays the names of the index and the table. */
  429. void
  430. dict_index_name_print(
  431. /*==================*/
  432. FILE* file, /* in: output stream */
  433. trx_t* trx, /* in: transaction */
  434. const dict_index_t* index); /* in: index to print */
  435. /************************************************************************
  436. Gets the first index on the table (the clustered index). */
  437. UNIV_INLINE
  438. dict_index_t*
  439. dict_table_get_first_index(
  440. /*=======================*/
  441. /* out: index, NULL if none exists */
  442. dict_table_t* table); /* in: table */
  443. /************************************************************************
  444. Gets the first index on the table (the clustered index). */
  445. dict_index_t*
  446. dict_table_get_first_index_noninline(
  447. /*=================================*/
  448. /* out: index, NULL if none exists */
  449. dict_table_t* table); /* in: table */
  450. /************************************************************************
  451. Gets the next index on the table. */
  452. UNIV_INLINE
  453. dict_index_t*
  454. dict_table_get_next_index(
  455. /*======================*/
  456. /* out: index, NULL if none left */
  457. dict_index_t* index); /* in: index */
  458. /************************************************************************
  459. Gets the next index on the table. */
  460. dict_index_t*
  461. dict_table_get_next_index_noninline(
  462. /*================================*/
  463. /* out: index, NULL if none left */
  464. dict_index_t* index); /* in: index */
  465. /************************************************************************
  466. Check whether the index is the clustered index. */
  467. UNIV_INLINE
  468. ulint
  469. dict_index_is_clust(
  470. /*================*/
  471. /* out: nonzero for clustered index,
  472. zero for other indexes */
  473. const dict_index_t* index) /* in: index */
  474. __attribute__((pure));
  475. /************************************************************************
  476. Gets the number of user-defined columns in a table in the dictionary
  477. cache. */
  478. UNIV_INLINE
  479. ulint
  480. dict_table_get_n_user_cols(
  481. /*=======================*/
  482. /* out: number of user-defined (e.g., not
  483. ROW_ID) columns of a table */
  484. dict_table_t* table); /* in: table */
  485. /************************************************************************
  486. Gets the number of system columns in a table in the dictionary cache. */
  487. UNIV_INLINE
  488. ulint
  489. dict_table_get_n_sys_cols(
  490. /*======================*/
  491. /* out: number of system (e.g.,
  492. ROW_ID) columns of a table */
  493. dict_table_t* table); /* in: table */
  494. /************************************************************************
  495. Gets the number of all columns (also system) in a table in the dictionary
  496. cache. */
  497. UNIV_INLINE
  498. ulint
  499. dict_table_get_n_cols(
  500. /*==================*/
  501. /* out: number of columns of a table */
  502. dict_table_t* table); /* in: table */
  503. #ifdef UNIV_DEBUG
  504. /************************************************************************
  505. Gets the nth column of a table. */
  506. UNIV_INLINE
  507. dict_col_t*
  508. dict_table_get_nth_col(
  509. /*===================*/
  510. /* out: pointer to column object */
  511. const dict_table_t* table, /* in: table */
  512. ulint pos); /* in: position of column */
  513. /************************************************************************
  514. Gets the given system column of a table. */
  515. UNIV_INLINE
  516. dict_col_t*
  517. dict_table_get_sys_col(
  518. /*===================*/
  519. /* out: pointer to column object */
  520. const dict_table_t* table, /* in: table */
  521. ulint sys); /* in: DATA_ROW_ID, ... */
  522. #else /* UNIV_DEBUG */
  523. #define dict_table_get_nth_col(table, pos) \
  524. ((table)->cols + (pos))
  525. #define dict_table_get_sys_col(table, sys) \
  526. ((table)->cols + (table)->n_cols + (sys) - DATA_N_SYS_COLS)
  527. #endif /* UNIV_DEBUG */
  528. /************************************************************************
  529. Gets the nth column of a table. */
  530. const dict_col_t*
  531. dict_table_get_nth_col_noninline(
  532. /*=============================*/
  533. /* out: pointer to column object */
  534. const dict_table_t* table, /* in: table */
  535. ulint pos); /* in: position of column */
  536. /************************************************************************
  537. Gets the given system column number of a table. */
  538. UNIV_INLINE
  539. ulint
  540. dict_table_get_sys_col_no(
  541. /*======================*/
  542. /* out: column number */
  543. const dict_table_t* table, /* in: table */
  544. ulint sys); /* in: DATA_ROW_ID, ... */
  545. /************************************************************************
  546. Check whether the table uses the compact page format. */
  547. UNIV_INLINE
  548. ibool
  549. dict_table_is_comp(
  550. /*===============*/
  551. /* out: TRUE if table uses the
  552. compact page format */
  553. const dict_table_t* table); /* in: table */
  554. /************************************************************************
  555. Check whether the table uses the compact page format. */
  556. ibool
  557. dict_table_is_comp_noninline(
  558. /*=========================*/
  559. /* out: TRUE if table uses the
  560. compact page format */
  561. const dict_table_t* table); /* in: table */
  562. /************************************************************************
  563. Check whether the table uses the compressed compact page format. */
  564. UNIV_INLINE
  565. ulint
  566. dict_table_zip_size(
  567. /*================*/
  568. /* out: compressed page size,
  569. or 0 if not compressed */
  570. const dict_table_t* table); /* in: table */
  571. /************************************************************************
  572. Checks if a column is in the ordering columns of the clustered index of a
  573. table. Column prefixes are treated like whole columns. */
  574. ibool
  575. dict_table_col_in_clustered_key(
  576. /*============================*/
  577. /* out: TRUE if the column, or its prefix, is
  578. in the clustered key */
  579. dict_table_t* table, /* in: table */
  580. ulint n); /* in: column number */
  581. /***********************************************************************
  582. Copies types of columns contained in table to tuple. */
  583. void
  584. dict_table_copy_types(
  585. /*==================*/
  586. dtuple_t* tuple, /* in/out: data tuple */
  587. const dict_table_t* table); /* in: table */
  588. /**************************************************************************
  589. Looks for an index with the given id. NOTE that we do not reserve
  590. the dictionary mutex: this function is for emergency purposes like
  591. printing info of a corrupt database page! */
  592. dict_index_t*
  593. dict_index_find_on_id_low(
  594. /*======================*/
  595. /* out: index or NULL if not found from cache */
  596. dulint id); /* in: index id */
  597. /**************************************************************************
  598. Adds an index to the dictionary cache. */
  599. void
  600. dict_index_add_to_cache(
  601. /*====================*/
  602. dict_table_t* table, /* in: table on which the index is */
  603. dict_index_t* index, /* in, own: index; NOTE! The index memory
  604. object is freed in this function! */
  605. ulint page_no);/* in: root page number of the index */
  606. /**************************************************************************
  607. Removes an index from the dictionary cache. */
  608. void
  609. dict_index_remove_from_cache(
  610. /*=========================*/
  611. dict_table_t* table, /* in: table */
  612. dict_index_t* index); /* in, own: index */
  613. /************************************************************************
  614. Gets the number of fields in the internal representation of an index,
  615. including fields added by the dictionary system. */
  616. UNIV_INLINE
  617. ulint
  618. dict_index_get_n_fields(
  619. /*====================*/
  620. /* out: number of fields */
  621. const dict_index_t* index); /* in: an internal
  622. representation of index (in
  623. the dictionary cache) */
  624. /************************************************************************
  625. Gets the number of fields in the internal representation of an index
  626. that uniquely determine the position of an index entry in the index, if
  627. we do not take multiversioning into account: in the B-tree use the value
  628. returned by dict_index_get_n_unique_in_tree. */
  629. UNIV_INLINE
  630. ulint
  631. dict_index_get_n_unique(
  632. /*====================*/
  633. /* out: number of fields */
  634. dict_index_t* index); /* in: an internal representation of index
  635. (in the dictionary cache) */
  636. /************************************************************************
  637. Gets the number of fields in the internal representation of an index
  638. which uniquely determine the position of an index entry in the index, if
  639. we also take multiversioning into account. */
  640. UNIV_INLINE
  641. ulint
  642. dict_index_get_n_unique_in_tree(
  643. /*============================*/
  644. /* out: number of fields */
  645. dict_index_t* index); /* in: an internal representation of index
  646. (in the dictionary cache) */
  647. /************************************************************************
  648. Gets the number of user-defined ordering fields in the index. In the internal
  649. representation we add the row id to the ordering fields to make all indexes
  650. unique, but this function returns the number of fields the user defined
  651. in the index as ordering fields. */
  652. UNIV_INLINE
  653. ulint
  654. dict_index_get_n_ordering_defined_by_user(
  655. /*======================================*/
  656. /* out: number of fields */
  657. dict_index_t* index); /* in: an internal representation of index
  658. (in the dictionary cache) */
  659. #ifdef UNIV_DEBUG
  660. /************************************************************************
  661. Gets the nth field of an index. */
  662. UNIV_INLINE
  663. dict_field_t*
  664. dict_index_get_nth_field(
  665. /*=====================*/
  666. /* out: pointer to field object */
  667. const dict_index_t* index, /* in: index */
  668. ulint pos); /* in: position of field */
  669. #else /* UNIV_DEBUG */
  670. # define dict_index_get_nth_field(index, pos) ((index)->fields + (pos))
  671. #endif /* UNIV_DEBUG */
  672. /************************************************************************
  673. Gets pointer to the nth column in an index. */
  674. UNIV_INLINE
  675. const dict_col_t*
  676. dict_index_get_nth_col(
  677. /*===================*/
  678. /* out: column */
  679. const dict_index_t* index, /* in: index */
  680. ulint pos); /* in: position of the field */
  681. /************************************************************************
  682. Gets the column number of the nth field in an index. */
  683. UNIV_INLINE
  684. ulint
  685. dict_index_get_nth_col_no(
  686. /*======================*/
  687. /* out: column number */
  688. const dict_index_t* index, /* in: index */
  689. ulint pos); /* in: position of the field */
  690. /************************************************************************
  691. Looks for column n in an index. */
  692. ulint
  693. dict_index_get_nth_col_pos(
  694. /*=======================*/
  695. /* out: position in internal
  696. representation of the index;
  697. if not contained, returns
  698. ULINT_UNDEFINED */
  699. const dict_index_t* index, /* in: index */
  700. ulint n); /* in: column number */
  701. /************************************************************************
  702. Returns TRUE if the index contains a column or a prefix of that column. */
  703. ibool
  704. dict_index_contains_col_or_prefix(
  705. /*==============================*/
  706. /* out: TRUE if contains the column or its
  707. prefix */
  708. dict_index_t* index, /* in: index */
  709. ulint n); /* in: column number */
  710. /************************************************************************
  711. Looks for a matching field in an index. The column has to be the same. The
  712. column in index must be complete, or must contain a prefix longer than the
  713. column in index2. That is, we must be able to construct the prefix in index2
  714. from the prefix in index. */
  715. ulint
  716. dict_index_get_nth_field_pos(
  717. /*=========================*/
  718. /* out: position in internal representation
  719. of the index; if not contained, returns
  720. ULINT_UNDEFINED */
  721. dict_index_t* index, /* in: index from which to search */
  722. dict_index_t* index2, /* in: index */
  723. ulint n); /* in: field number in index2 */
  724. /************************************************************************
  725. Looks for column n position in the clustered index. */
  726. ulint
  727. dict_table_get_nth_col_pos(
  728. /*=======================*/
  729. /* out: position in internal representation
  730. of the clustered index */
  731. dict_table_t* table, /* in: table */
  732. ulint n); /* in: column number */
  733. /************************************************************************
  734. Returns the position of a system column in an index. */
  735. UNIV_INLINE
  736. ulint
  737. dict_index_get_sys_col_pos(
  738. /*=======================*/
  739. /* out: position,
  740. ULINT_UNDEFINED if not contained */
  741. const dict_index_t* index, /* in: index */
  742. ulint type); /* in: DATA_ROW_ID, ... */
  743. /***********************************************************************
  744. Adds a column to index. */
  745. void
  746. dict_index_add_col(
  747. /*===============*/
  748. dict_index_t* index, /* in: index */
  749. dict_table_t* table, /* in: table */
  750. dict_col_t* col, /* in: column */
  751. ulint prefix_len); /* in: column prefix length */
  752. /***********************************************************************
  753. Copies types of fields contained in index to tuple. */
  754. void
  755. dict_index_copy_types(
  756. /*==================*/
  757. dtuple_t* tuple, /* in/out: data tuple */
  758. const dict_index_t* index, /* in: index */
  759. ulint n_fields); /* in: number of
  760. field types to copy */
  761. /*************************************************************************
  762. Gets the field column. */
  763. UNIV_INLINE
  764. const dict_col_t*
  765. dict_field_get_col(
  766. /*===============*/
  767. const dict_field_t* field);
  768. /**************************************************************************
  769. Returns an index object if it is found in the dictionary cache.
  770. Assumes that dict_sys->mutex is already being held. */
  771. dict_index_t*
  772. dict_index_get_if_in_cache_low(
  773. /*===========================*/
  774. /* out: index, NULL if not found */
  775. dulint index_id); /* in: index id */
  776. #if defined UNIV_DEBUG || defined UNIV_BUF_DEBUG
  777. /**************************************************************************
  778. Returns an index object if it is found in the dictionary cache. */
  779. dict_index_t*
  780. dict_index_get_if_in_cache(
  781. /*=======================*/
  782. /* out: index, NULL if not found */
  783. dulint index_id); /* in: index id */
  784. #endif /* UNIV_DEBUG || UNIV_BUF_DEBUG */
  785. #ifdef UNIV_DEBUG
  786. /**************************************************************************
  787. Checks that a tuple has n_fields_cmp value in a sensible range, so that
  788. no comparison can occur with the page number field in a node pointer. */
  789. ibool
  790. dict_index_check_search_tuple(
  791. /*==========================*/
  792. /* out: TRUE if ok */
  793. dict_index_t* index, /* in: index */
  794. const dtuple_t* tuple); /* in: tuple used in a search */
  795. #endif /* UNIV_DEBUG */
  796. /**************************************************************************
  797. Builds a node pointer out of a physical record and a page number. */
  798. dtuple_t*
  799. dict_index_build_node_ptr(
  800. /*======================*/
  801. /* out, own: node pointer */
  802. dict_index_t* index, /* in: index */
  803. rec_t* rec, /* in: record for which to build node
  804. pointer */
  805. ulint page_no,/* in: page number to put in node pointer */
  806. mem_heap_t* heap, /* in: memory heap where pointer created */
  807. ulint level); /* in: level of rec in tree: 0 means leaf
  808. level */
  809. /**************************************************************************
  810. Copies an initial segment of a physical record, long enough to specify an
  811. index entry uniquely. */
  812. rec_t*
  813. dict_index_copy_rec_order_prefix(
  814. /*=============================*/
  815. /* out: pointer to the prefix record */
  816. dict_index_t* index, /* in: index */
  817. rec_t* rec, /* in: record for which to copy prefix */
  818. ulint* n_fields,/* out: number of fields copied */
  819. byte** buf, /* in/out: memory buffer for the copied prefix,
  820. or NULL */
  821. ulint* buf_size);/* in/out: buffer size */
  822. /**************************************************************************
  823. Builds a typed data tuple out of a physical record. */
  824. dtuple_t*
  825. dict_index_build_data_tuple(
  826. /*========================*/
  827. /* out, own: data tuple */
  828. dict_index_t* index, /* in: index */
  829. rec_t* rec, /* in: record for which to build data tuple */
  830. ulint n_fields,/* in: number of data fields */
  831. mem_heap_t* heap); /* in: memory heap where tuple created */
  832. /*************************************************************************
  833. Gets the space id of the root of the index tree. */
  834. UNIV_INLINE
  835. ulint
  836. dict_index_get_space(
  837. /*=================*/
  838. /* out: space id */
  839. dict_index_t* index); /* in: index */
  840. /*************************************************************************
  841. Sets the space id of the root of the index tree. */
  842. UNIV_INLINE
  843. void
  844. dict_index_set_space(
  845. /*=================*/
  846. dict_index_t* index, /* in: index */
  847. ulint space); /* in: space id */
  848. /*************************************************************************
  849. Gets the page number of the root of the index tree. */
  850. UNIV_INLINE
  851. ulint
  852. dict_index_get_page(
  853. /*================*/
  854. /* out: page number */
  855. dict_index_t* tree); /* in: index */
  856. /*************************************************************************
  857. Sets the page number of the root of index tree. */
  858. UNIV_INLINE
  859. void
  860. dict_index_set_page(
  861. /*================*/
  862. dict_index_t* index, /* in: index */
  863. ulint page); /* in: page number */
  864. /*************************************************************************
  865. Gets the type of the index tree. */
  866. UNIV_INLINE
  867. ulint
  868. dict_index_get_type(
  869. /*================*/
  870. /* out: type */
  871. dict_index_t* index); /* in: index */
  872. /*************************************************************************
  873. Gets the read-write lock of the index tree. */
  874. UNIV_INLINE
  875. rw_lock_t*
  876. dict_index_get_lock(
  877. /*================*/
  878. /* out: read-write lock */
  879. dict_index_t* index); /* in: index */
  880. /************************************************************************
  881. Returns free space reserved for future updates of records. This is
  882. relevant only in the case of many consecutive inserts, as updates
  883. which make the records bigger might fragment the index. */
  884. UNIV_INLINE
  885. ulint
  886. dict_index_get_space_reserve(void);
  887. /*==============================*/
  888. /* out: number of free bytes on page,
  889. reserved for updates */
  890. /*************************************************************************
  891. Calculates the minimum record length in an index. */
  892. ulint
  893. dict_index_calc_min_rec_len(
  894. /*========================*/
  895. dict_index_t* index); /* in: index */
  896. /*************************************************************************
  897. Calculates new estimates for table and index statistics. The statistics
  898. are used in query optimization. */
  899. void
  900. dict_update_statistics_low(
  901. /*=======================*/
  902. dict_table_t* table, /* in: table */
  903. ibool has_dict_mutex);/* in: TRUE if the caller has the
  904. dictionary mutex */
  905. /*************************************************************************
  906. Calculates new estimates for table and index statistics. The statistics
  907. are used in query optimization. */
  908. void
  909. dict_update_statistics(
  910. /*===================*/
  911. dict_table_t* table); /* in: table */
  912. /************************************************************************
  913. Reserves the dictionary system mutex for MySQL. */
  914. void
  915. dict_mutex_enter_for_mysql(void);
  916. /*============================*/
  917. /************************************************************************
  918. Releases the dictionary system mutex for MySQL. */
  919. void
  920. dict_mutex_exit_for_mysql(void);
  921. /*===========================*/
  922. /************************************************************************
  923. Checks if the database name in two table names is the same. */
  924. ibool
  925. dict_tables_have_same_db(
  926. /*=====================*/
  927. /* out: TRUE if same db name */
  928. const char* name1, /* in: table name in the form
  929. dbname '/' tablename */
  930. const char* name2); /* in: table name in the form
  931. dbname '/' tablename */
  932. /*************************************************************************
  933. Scans from pointer onwards. Stops if is at the start of a copy of
  934. 'string' where characters are compared without case sensitivity. Stops
  935. also at '\0'. */
  936. const char*
  937. dict_scan_to(
  938. /*=========*/
  939. /* out: scanned up to this */
  940. const char* ptr, /* in: scan from */
  941. const char* string);/* in: look for this */
  942. /* Buffers for storing detailed information about the latest foreign key
  943. and unique key errors */
  944. extern FILE* dict_foreign_err_file;
  945. extern mutex_t dict_foreign_err_mutex; /* mutex protecting the buffers */
  946. extern dict_sys_t* dict_sys; /* the dictionary system */
  947. extern rw_lock_t dict_operation_lock;
  948. /* Dictionary system struct */
  949. struct dict_sys_struct{
  950. mutex_t mutex; /* mutex protecting the data
  951. dictionary; protects also the
  952. disk-based dictionary system tables;
  953. this mutex serializes CREATE TABLE
  954. and DROP TABLE, as well as reading
  955. the dictionary data for a table from
  956. system tables */
  957. dulint row_id; /* the next row id to assign;
  958. NOTE that at a checkpoint this
  959. must be written to the dict system
  960. header and flushed to a file; in
  961. recovery this must be derived from
  962. the log records */
  963. hash_table_t* table_hash; /* hash table of the tables, based
  964. on name */
  965. hash_table_t* table_id_hash; /* hash table of the tables, based
  966. on id */
  967. UT_LIST_BASE_NODE_T(dict_table_t)
  968. table_LRU; /* LRU list of tables */
  969. ulint size; /* varying space in bytes occupied
  970. by the data dictionary table and
  971. index objects */
  972. dict_table_t* sys_tables; /* SYS_TABLES table */
  973. dict_table_t* sys_columns; /* SYS_COLUMNS table */
  974. dict_table_t* sys_indexes; /* SYS_INDEXES table */
  975. dict_table_t* sys_fields; /* SYS_FIELDS table */
  976. };
  977. #ifndef UNIV_NONINL
  978. #include "dict0dict.ic"
  979. #endif
  980. #endif