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.

105 lines
2.8 KiB

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