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.

112 lines
3.1 KiB

  1. /* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  12. #ifndef TABLE_MAPPING_H
  13. #define TABLE_MAPPING_H
  14. /* Forward declarations */
  15. #ifndef MYSQL_CLIENT
  16. struct TABLE;
  17. #else
  18. class Table_map_log_event;
  19. typedef Table_map_log_event TABLE;
  20. void free_table_map_log_event(TABLE *table);
  21. #endif
  22. /*
  23. CLASS table_mapping
  24. RESPONSIBILITIES
  25. The table mapping is used to map table id's to table pointers
  26. COLLABORATION
  27. RELAY_LOG For mapping table id:s to tables when receiving events.
  28. */
  29. /*
  30. Guilhem to Mats:
  31. in the table_mapping class, the memory is allocated and never freed (until
  32. destruction). So this is a good candidate for allocating inside a MEM_ROOT:
  33. it gives the efficient allocation in chunks (like in expand()). So I have
  34. introduced a MEM_ROOT.
  35. Note that inheriting from Sql_alloc had no effect: it has effects only when
  36. "ptr= new table_mapping" is called, and this is never called. And it would
  37. then allocate from thd->mem_root which is a highly volatile object (reset
  38. from example after executing each query, see dispatch_command(), it has a
  39. free_root() at end); as the table_mapping object is supposed to live longer
  40. than a query, it was dangerous.
  41. A dedicated MEM_ROOT needs to be used, see below.
  42. */
  43. #include "hash.h" /* HASH */
  44. class table_mapping {
  45. private:
  46. MEM_ROOT m_mem_root;
  47. public:
  48. enum enum_error {
  49. ERR_NO_ERROR = 0,
  50. ERR_LIMIT_EXCEEDED,
  51. ERR_MEMORY_ALLOCATION
  52. };
  53. table_mapping();
  54. ~table_mapping();
  55. TABLE* get_table(ulong table_id);
  56. int set_table(ulong table_id, TABLE* table);
  57. int remove_table(ulong table_id);
  58. void clear_tables();
  59. ulong count() const { return m_table_ids.records; }
  60. private:
  61. /*
  62. This is a POD (Plain Old Data). Keep it that way (we apply offsetof() to
  63. it, which only works for PODs)
  64. */
  65. struct entry {
  66. ulong table_id;
  67. union {
  68. TABLE *table;
  69. entry *next;
  70. };
  71. };
  72. entry *find_entry(ulong table_id)
  73. {
  74. return (entry *) my_hash_search(&m_table_ids,
  75. (uchar*)&table_id,
  76. sizeof(table_id));
  77. }
  78. int expand();
  79. /*
  80. Head of the list of free entries; "free" in the sense that it's an
  81. allocated entry free for use, NOT in the sense that it's freed
  82. memory.
  83. */
  84. entry *m_free;
  85. /* Correspondance between an id (a number) and a TABLE object */
  86. HASH m_table_ids;
  87. };
  88. #endif