My Project
|
00001 /* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. 00002 00003 This program is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU General Public License as published by 00005 the Free Software Foundation; version 2 of the License. 00006 00007 This program is distributed in the hope that it will be useful, 00008 but WITHOUT ANY WARRANTY; without even the implied warranty of 00009 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00010 GNU General Public License for more details. 00011 00012 You should have received a copy of the GNU General Public License 00013 along with this program; if not, write to the Free Software Foundation, 00014 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */ 00015 00016 #ifndef TABLE_MAPPING_H 00017 #define TABLE_MAPPING_H 00018 00019 /* Forward declarations */ 00020 #ifndef MYSQL_CLIENT 00021 struct TABLE; 00022 #else 00023 class Table_map_log_event; 00024 typedef Table_map_log_event TABLE; 00025 void free_table_map_log_event(TABLE *table); 00026 #endif 00027 00028 00029 /* 00030 CLASS table_mapping 00031 00032 RESPONSIBILITIES 00033 The table mapping is used to map table id's to table pointers 00034 00035 COLLABORATION 00036 RELAY_LOG For mapping table id:s to tables when receiving events. 00037 */ 00038 00039 /* 00040 Guilhem to Mats: 00041 in the table_mapping class, the memory is allocated and never freed (until 00042 destruction). So this is a good candidate for allocating inside a MEM_ROOT: 00043 it gives the efficient allocation in chunks (like in expand()). So I have 00044 introduced a MEM_ROOT. 00045 00046 Note that inheriting from Sql_alloc had no effect: it has effects only when 00047 "ptr= new table_mapping" is called, and this is never called. And it would 00048 then allocate from thd->mem_root which is a highly volatile object (reset 00049 from example after executing each query, see dispatch_command(), it has a 00050 free_root() at end); as the table_mapping object is supposed to live longer 00051 than a query, it was dangerous. 00052 A dedicated MEM_ROOT needs to be used, see below. 00053 */ 00054 00055 #include "hash.h" /* HASH */ 00056 00057 class table_mapping { 00058 00059 private: 00060 MEM_ROOT m_mem_root; 00061 00062 public: 00063 00064 enum enum_error { 00065 ERR_NO_ERROR = 0, 00066 ERR_LIMIT_EXCEEDED, 00067 ERR_MEMORY_ALLOCATION 00068 }; 00069 00070 table_mapping(); 00071 ~table_mapping(); 00072 00073 TABLE* get_table(ulonglong table_id); 00074 00075 int set_table(ulonglong table_id, TABLE* table); 00076 int remove_table(ulonglong table_id); 00077 void clear_tables(); 00078 ulong count() const { return m_table_ids.records; } 00079 00080 private: 00081 /* 00082 This is a POD (Plain Old Data). Keep it that way (we apply offsetof() to 00083 it, which only works for PODs) 00084 */ 00085 struct entry { 00086 ulonglong table_id; 00087 union { 00088 TABLE *table; 00089 entry *next; 00090 }; 00091 }; 00092 00093 entry *find_entry(ulonglong table_id) 00094 { 00095 return (entry *) my_hash_search(&m_table_ids, 00096 (uchar*)&table_id, 00097 sizeof(table_id)); 00098 } 00099 int expand(); 00100 00101 /* 00102 Head of the list of free entries; "free" in the sense that it's an 00103 allocated entry free for use, NOT in the sense that it's freed 00104 memory. 00105 */ 00106 entry *m_free; 00107 00108 /* Correspondance between an id (a number) and a TABLE object */ 00109 HASH m_table_ids; 00110 }; 00111 00112 #endif