| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#ifndef TABLE_HELPER_H |
| 25 |
#define TABLE_HELPER_H |
| 26 |
|
| 27 |
#include <gtest/gtest.h> |
| 28 |
#include <array> |
| 29 |
#include <cstring> |
| 30 |
#include <vector> |
| 31 |
|
| 32 |
#include "my_alloc.h" |
| 33 |
#include "my_inttypes.h" |
| 34 |
#include "sql/current_thd.h" |
| 35 |
#include "sql/field.h" |
| 36 |
#include "sql/handler.h" |
| 37 |
#include "sql/key.h" |
| 38 |
#include "sql/sql_class.h" |
| 39 |
#include "sql/table.h" |
| 40 |
#include "unittest/gunit/mock_field_long.h" |
| 41 |
#include "unittest/gunit/temptable/mock_field_varstring.h" |
| 42 |
|
| 43 |
namespace temptable_test { |
| 44 |
|
| 45 |
class Table_helper { |
| 46 |
public: |
| 47 |
Table_helper(const char *name, THD *thd); |
| 48 |
|
| 49 |
void add_field_long(const char *name, bool is_nullable); |
| 50 |
|
| 51 |
void add_field_varstring(const char *name, uint char_len, bool is_nullable); |
| 52 |
|
| 53 |
void add_index(enum ha_key_alg algorithm, bool unique, |
| 54 |
const std::vector<int> &columns); |
| 55 |
|
| 56 |
void finalize(); |
| 57 |
|
| 58 |
void set_handler(handler *handler); |
| 59 |
|
| 60 |
TABLE *table(); |
| 61 |
|
| 62 |
TABLE_SHARE *table_share(); |
| 63 |
|
| 64 |
uchar *record_0(); |
| 65 |
|
| 66 |
uchar *record_1(); |
| 67 |
|
| 68 |
void copy_record_1_to_0(); |
| 69 |
|
| 70 |
template <typename T> |
| 71 |
T *field(int index); |
| 72 |
|
| 73 |
private: |
| 74 |
struct Index_def { |
| 75 |
enum ha_key_alg algorithm; |
| 76 |
bool unique; |
| 77 |
std::vector<int> columns; |
| 78 |
}; |
| 79 |
|
| 80 |
bool m_finalized; |
| 81 |
char m_name[32]; |
| 82 |
THD *m_thd; |
| 83 |
|
| 84 |
TABLE_SHARE m_table_share; |
| 85 |
TABLE m_table; |
| 86 |
|
| 87 |
std::vector<Index_def> m_index_defs; |
| 88 |
uint32 all_set_buf; |
| 89 |
|
| 90 |
std::vector<Field *> m_fields; |
| 91 |
|
| 92 |
MY_BITMAP write_set_struct; |
| 93 |
uint32 write_set_buf; |
| 94 |
MY_BITMAP read_set_struct; |
| 95 |
uint32 read_set_buf; |
| 96 |
|
| 97 |
std::array<uchar, 512> m_record_0; |
| 98 |
std::array<uchar, 512> m_record_1; |
| 99 |
|
| 100 |
std::vector<KEY> m_indexes; |
| 101 |
std::vector<KEY_PART_INFO> m_index_parts; |
| 102 |
|
| 103 |
void add_field(const Field &field); |
| 104 |
void initialize_share(); |
| 105 |
void initialize_table(); |
| 106 |
void finalize_fields(); |
| 107 |
uint set_field_pointers(uchar *record); |
| 108 |
}; |
| 109 |
|
| 110 |
inline Table_helper::Table_helper(const char *name, THD *thd) |
| 111 |
: m_finalized(false), m_thd(thd) { |
| 112 |
EXPECT_TRUE(name != nullptr); |
| 113 |
EXPECT_LT(std::strlen(name), sizeof(m_name)); |
| 114 |
std::strcpy(m_name, name); |
| 115 |
} |
| 116 |
|
| 117 |
inline void Table_helper::add_field_long(const char *name, bool is_nullable) { |
| 118 |
add_field(Mock_field_long(name, is_nullable, false)); |
| 119 |
} |
| 120 |
|
| 121 |
inline void Table_helper::add_field_varstring(const char *name, uint char_len, |
| 122 |
bool nullable) { |
| 123 |
add_field(Mock_field_varstring(&m_table_share, name, char_len, nullable)); |
| 124 |
} |
| 125 |
|
| 126 |
inline void Table_helper::add_field(const Field &field) { |
| 127 |
EXPECT_FALSE(m_finalized); |
| 128 |
|
| 129 |
auto new_field = field.clone(m_thd->mem_root); |
| 130 |
new_field->set_field_index(static_cast<uint16>(m_fields.size())); |
| 131 |
|
| 132 |
m_fields.push_back(new_field); |
| 133 |
} |
| 134 |
|
| 135 |
inline void Table_helper::add_index(ha_key_alg algorithm, bool unique, |
| 136 |
const std::vector<int> &columns) { |
| 137 |
EXPECT_FALSE(m_finalized); |
| 138 |
EXPECT_GT(columns.size(), 0); |
| 139 |
|
| 140 |
m_index_defs.push_back({algorithm, unique, columns}); |
| 141 |
} |
| 142 |
|
| 143 |
inline void Table_helper::finalize() { |
| 144 |
EXPECT_FALSE(m_finalized); |
| 145 |
m_finalized = true; |
| 146 |
|
| 147 |
|
| 148 |
initialize_share(); |
| 149 |
initialize_table(); |
| 150 |
finalize_fields(); |
| 151 |
|
| 152 |
EXPECT_EQ(0, bitmap_init(&m_table_share.all_set, &all_set_buf, |
| 153 |
m_table_share.fields)); |
| 154 |
bitmap_set_above(&m_table_share.all_set, 0, true); |
| 155 |
|
| 156 |
m_table_share.key_parts = 0; |
| 157 |
for (auto &iter : m_index_defs) { |
| 158 |
m_table_share.key_parts += static_cast<uint>(iter.columns.size()); |
| 159 |
} |
| 160 |
|
| 161 |
m_indexes.resize(m_index_defs.size()); |
| 162 |
m_index_parts.resize(m_table_share.key_parts); |
| 163 |
|
| 164 |
size_t part_index = 0; |
| 165 |
for (size_t i = 0; i < m_index_defs.size(); ++i) { |
| 166 |
m_indexes[i].flags = 0; |
| 167 |
if (m_index_defs[i].unique) { |
| 168 |
m_indexes[i].flags |= HA_NOSAME; |
| 169 |
} |
| 170 |
m_indexes[i].algorithm = m_index_defs[i].algorithm; |
| 171 |
m_indexes[i].user_defined_key_parts = |
| 172 |
static_cast<uint>(m_index_defs[i].columns.size()); |
| 173 |
m_indexes[i].key_part = &m_index_parts[part_index]; |
| 174 |
for (auto field_idx : m_index_defs[i].columns) { |
| 175 |
auto field = m_fields[field_idx]; |
| 176 |
m_index_parts[part_index].init_from_field(field); |
| 177 |
|
| 178 |
++part_index; |
| 179 |
} |
| 180 |
} |
| 181 |
m_table_share.keys = static_cast<uint>(m_index_defs.size()); |
| 182 |
m_table_share.key_info = m_table.key_info = m_indexes.data(); |
| 183 |
} |
| 184 |
|
| 185 |
inline void Table_helper::set_handler(handler *handler) { |
| 186 |
EXPECT_TRUE(handler != nullptr); |
| 187 |
|
| 188 |
m_table.file = handler; |
| 189 |
handler->change_table_ptr(&m_table, m_table.s); |
| 190 |
} |
| 191 |
|
| 192 |
inline TABLE *Table_helper::table() { return &m_table; } |
| 193 |
|
| 194 |
inline TABLE_SHARE *Table_helper::table_share() { return m_table.s; } |
| 195 |
|
| 196 |
inline uchar *Table_helper::record_0() { return m_record_0.data(); } |
| 197 |
|
| 198 |
inline uchar *Table_helper::record_1() { return m_record_1.data(); } |
| 199 |
|
| 200 |
inline void Table_helper::copy_record_1_to_0() { |
| 201 |
std::memcpy(record_0(), record_1(), m_table_share.rec_buff_length); |
| 202 |
} |
| 203 |
|
| 204 |
template <typename T> |
| 205 |
inline T *Table_helper::field(int index) { |
| 206 |
return static_cast<T *>(m_table.field[index]); |
| 207 |
} |
| 208 |
|
| 209 |
inline void Table_helper::initialize_share() { |
| 210 |
m_table_share.table_name.str = m_name; |
| 211 |
m_table_share.table_name.length = std::strlen(m_name); |
| 212 |
|
| 213 |
m_table_share.field = m_fields.data(); |
| 214 |
m_table_share.fields = static_cast<uint>(m_fields.size()); |
| 215 |
m_table_share.db_create_options = 0; |
| 216 |
m_table_share.primary_key = 0; |
| 217 |
m_table_share.column_bitmap_size = sizeof(int); |
| 218 |
m_table_share.tmp_table = NO_TMP_TABLE; |
| 219 |
m_table_share.db_low_byte_first = true; |
| 220 |
|
| 221 |
static const char *fakepath = "fakepath"; |
| 222 |
m_table_share.path.str = const_cast<char *>(fakepath); |
| 223 |
m_table_share.path.length = std::strlen(fakepath); |
| 224 |
} |
| 225 |
|
| 226 |
inline void Table_helper::initialize_table() { |
| 227 |
m_table.s = &m_table_share; |
| 228 |
m_table.in_use = m_thd; |
| 229 |
|
| 230 |
m_table.alias = m_name; |
| 231 |
|
| 232 |
m_table.field = m_table_share.field; |
| 233 |
|
| 234 |
m_table.null_row = '\0'; |
| 235 |
m_table.read_set = &read_set_struct; |
| 236 |
m_table.write_set = &write_set_struct; |
| 237 |
m_table.next_number_field = nullptr; |
| 238 |
m_table.pos_in_table_list = nullptr; |
| 239 |
EXPECT_EQ(0, |
| 240 |
bitmap_init(m_table.write_set, &write_set_buf, m_table.s->fields)); |
| 241 |
EXPECT_EQ(0, bitmap_init(m_table.read_set, &read_set_buf, m_table.s->fields)); |
| 242 |
|
| 243 |
m_table.const_table = false; |
| 244 |
} |
| 245 |
|
| 246 |
inline void Table_helper::finalize_fields() { |
| 247 |
for (uint i = 0; i < m_table_share.fields; ++i) { |
| 248 |
Field *field = m_table_share.field[i]; |
| 249 |
|
| 250 |
field->init(&m_table); |
| 251 |
field->set_field_index(i); |
| 252 |
|
| 253 |
bitmap_set_bit(m_table.read_set, i); |
| 254 |
bitmap_set_bit(m_table.write_set, i); |
| 255 |
} |
| 256 |
|
| 257 |
m_table.record[0] = m_record_0.data(); |
| 258 |
m_table.record[1] = m_record_1.data(); |
| 259 |
m_table.s->rec_buff_length = set_field_pointers(m_table.record[0]); |
| 260 |
} |
| 261 |
|
| 262 |
inline uint Table_helper::set_field_pointers(uchar *record) { |
| 263 |
uchar *nul_ptr = record; |
| 264 |
uint nul_bit = 0; |
| 265 |
uint needed_nul_bytes = (static_cast<uint>(m_fields.size()) + 7) / 8; |
| 266 |
|
| 267 |
uchar *buf_ptr = nul_ptr + needed_nul_bytes; |
| 268 |
|
| 269 |
for (auto &field : m_fields) { |
| 270 |
|
| 271 |
if (field->is_flag_set(NOT_NULL_FLAG)) { |
| 272 |
field->set_null_ptr(nullptr, 0); |
| 273 |
} else { |
| 274 |
field->set_null_ptr(nul_ptr, nul_bit); |
| 275 |
if (++nul_bit == 8) { |
| 276 |
++nul_ptr; |
| 277 |
nul_bit = 0; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
field->set_field_ptr(buf_ptr); |
| 282 |
buf_ptr += field->pack_length(); |
| 283 |
} |
| 284 |
|
| 285 |
return (buf_ptr - record); |
| 286 |
} |
| 287 |
|
| 288 |
} |
| 289 |
|
| 290 |
#endif |