| 1 | #ifndef FAKE_KEY_INCLUDED |
| 2 | #define FAKE_KEY_INCLUDED |
| 3 | |
| 4 | /* |
| 5 | Copyright (c) 2014, 2026, Oracle and/or its affiliates. |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License, version 2.0, |
| 9 | as published by the Free Software Foundation. |
| 10 | |
| 11 | This program is designed to work with certain software (including |
| 12 | but not limited to OpenSSL) that is licensed under separate terms, |
| 13 | as designated in a particular file or component or in included license |
| 14 | documentation. The authors of MySQL hereby grant you an additional |
| 15 | permission to link the program and your derivative works with the |
| 16 | separately licensed software that they have either included with |
| 17 | the program or referenced in the documentation. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, |
| 20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | GNU General Public License, version 2.0, for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with this program; if not, write to the Free Software |
| 26 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 27 | |
| 28 | #include "my_config.h" |
| 29 | |
| 30 | #include <assert.h> |
| 31 | #include <gtest/gtest.h> |
| 32 | |
| 33 | #include "sql/key.h" // KEY |
| 34 | |
| 35 | /** |
| 36 | A fake class to make it easy to set up a KEY object. |
| 37 | |
| 38 | Note that in this version the KEY object is only initialized with necessary |
| 39 | information to do operations on rec_per_key. |
| 40 | */ |
| 41 | |
| 42 | class Fake_KEY : public KEY { |
| 43 | public: |
| 44 | /** |
| 45 | Initialize the KEY object. |
| 46 | |
| 47 | Only member variables needed for the rec_per_key interface are |
| 48 | currently initialized. |
| 49 | |
| 50 | @param key_parts_arg number of key parts this index should have |
| 51 | @param unique unique or non-unique key |
| 52 | */ |
| 53 | Fake_KEY(unsigned int key_parts_arg, bool unique) { |
| 54 | init(key_parts_arg, key_parts_arg, unique); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | Initialize the KEY object. |
| 59 | |
| 60 | Only member variables needed for the rec_per_key interface are |
| 61 | currently initialized. |
| 62 | |
| 63 | @param user_defined_parts_arg number of key parts defined for index |
| 64 | @param actual_key_parts_arg number of additional primary key parts |
| 65 | included in index |
| 66 | @param unique unique or non-unique key |
| 67 | */ |
| 68 | Fake_KEY(unsigned int user_defined_parts_arg, |
| 69 | unsigned int actual_key_parts_arg, bool unique) { |
| 70 | init(user_defined_parts_arg, actual_key_parts_arg, unique); |
| 71 | } |
| 72 | |
| 73 | ~Fake_KEY() { |
| 74 | // free the memory for the two rec_per_key arrays |
| 75 | delete[] m_rec_per_key; |
| 76 | delete[] m_rec_per_key_float; |
| 77 | } |
| 78 | |
| 79 | private: |
| 80 | /** |
| 81 | Initialize the KEY object. |
| 82 | |
| 83 | Only member variables needed for the rec_per_key interface are |
| 84 | currently initialized. |
| 85 | |
| 86 | @param user_defined_key_parts_arg number of key parts defined for index |
| 87 | @param actual_key_parts_arg number of additional primary key parts |
| 88 | included in index |
| 89 | @param unique unique or non-unique key |
| 90 | */ |
| 91 | void init(unsigned int user_defined_key_parts_arg, |
| 92 | unsigned int actual_key_parts_arg, bool unique) { |
| 93 | assert(user_defined_key_parts_arg > 0); |
| 94 | assert(actual_key_parts_arg > 0); |
| 95 | |
| 96 | flags = 0; |
| 97 | if (unique) flags |= HA_NOSAME; |
| 98 | actual_flags = flags; |
| 99 | |
| 100 | user_defined_key_parts = user_defined_key_parts_arg; |
| 101 | actual_key_parts = actual_key_parts_arg; |
| 102 | |
| 103 | // Allocate memory for the two rec_per_key arrays |
| 104 | m_rec_per_key = new ulong[actual_key_parts]; |
| 105 | m_rec_per_key_float = new rec_per_key_t[actual_key_parts]; |
| 106 | set_rec_per_key_array(m_rec_per_key, m_rec_per_key_float); |
| 107 | |
| 108 | // Initialize the rec_per_key arrays with default/unknown value |
| 109 | for (uint kp = 0; kp < actual_key_parts; kp++) { |
| 110 | rec_per_key[kp] = 0; |
| 111 | set_records_per_key(kp, REC_PER_KEY_UNKNOWN); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // Storage for the two records per key arrays |
| 116 | ulong *m_rec_per_key; |
| 117 | rec_per_key_t *m_rec_per_key_float; |
| 118 | }; |
| 119 | |
| 120 | #endif /* FAKE_KEY_INCLUDED */ |