| 1 | #ifndef HA_SEQUENCE_INCLUDED |
| 2 | #define HA_SEQUENCE_INCLUDED |
| 3 | /* |
| 4 | Copyright (c) 2017 Aliyun and/or its affiliates. |
| 5 | Copyright (c) 2017 MariaDB corporation |
| 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 as published by |
| 9 | the Free Software Foundation; version 2 of the License. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include "sql_sequence.h" |
| 22 | #include "table.h" |
| 23 | #include "handler.h" |
| 24 | |
| 25 | extern handlerton *sql_sequence_hton; |
| 26 | |
| 27 | /* |
| 28 | Sequence engine handler. |
| 29 | |
| 30 | The sequence engine is a logic engine. It doesn't store any data. |
| 31 | All the sequence data stored into the base table which must support |
| 32 | non rollback writes (HA_CAN_TABLES_WITHOUT_ROLLBACK) |
| 33 | |
| 34 | The sequence data (SEQUENCE class) is stored in TABLE_SHARE->sequence |
| 35 | |
| 36 | TABLE RULES: |
| 37 | 1. When table is created, one row is automaticlly inserted into |
| 38 | the table. The table will always have one and only one row. |
| 39 | 2. Any inserts or updates to the table will be validated. |
| 40 | 3. Inserts will overwrite the original row. |
| 41 | 4. DELETE and TRUNCATE will not affect the table. |
| 42 | Instead a warning will be given. |
| 43 | 5. Cache will be reset for any updates. |
| 44 | |
| 45 | CACHE RULES: |
| 46 | SEQUENCE class is used to cache values that sequence defined. |
| 47 | 1. If hit cache, we can query back the sequence nextval directly |
| 48 | instead of reading the underlying table. |
| 49 | |
| 50 | 2. When run out of values, the sequence engine will reserve new values |
| 51 | in update the base table. |
| 52 | |
| 53 | 3. The cache is invalidated if any update on based table. |
| 54 | */ |
| 55 | |
| 56 | class ha_sequence :public handler |
| 57 | { |
| 58 | private: |
| 59 | handler *file; |
| 60 | SEQUENCE *sequence; /* From table_share->sequence */ |
| 61 | |
| 62 | public: |
| 63 | /* Set when handler is write locked */ |
| 64 | bool write_locked; |
| 65 | |
| 66 | ha_sequence(handlerton *hton, TABLE_SHARE *share); |
| 67 | ~ha_sequence(); |
| 68 | |
| 69 | /* virtual function that are re-implemented for sequence */ |
| 70 | int open(const char *name, int mode, uint test_if_locked); |
| 71 | int create(const char *name, TABLE *form, |
| 72 | HA_CREATE_INFO *create_info); |
| 73 | handler *clone(const char *name, MEM_ROOT *mem_root); |
| 74 | int write_row(uchar *buf); |
| 75 | Table_flags table_flags() const; |
| 76 | /* One can't update or delete from sequence engine */ |
| 77 | int update_row(const uchar *old_data, const uchar *new_data) |
| 78 | { return HA_ERR_WRONG_COMMAND; } |
| 79 | int delete_row(const uchar *buf) |
| 80 | { return HA_ERR_WRONG_COMMAND; } |
| 81 | /* One can't delete from sequence engine */ |
| 82 | int truncate() |
| 83 | { return HA_ERR_WRONG_COMMAND; } |
| 84 | /* Can't use query cache */ |
| 85 | uint8 table_cache_type() |
| 86 | { return HA_CACHE_TBL_NOCACHE; } |
| 87 | void print_error(int error, myf errflag); |
| 88 | int info(uint); |
| 89 | LEX_CSTRING *engine_name() { return hton_name(file->ht); } |
| 90 | int external_lock(THD *thd, int lock_type); |
| 91 | int (enum ha_extra_function operation); |
| 92 | /* For ALTER ONLINE TABLE */ |
| 93 | bool check_if_incompatible_data(HA_CREATE_INFO *create_info, |
| 94 | uint table_changes); |
| 95 | void write_lock() { write_locked= 1;} |
| 96 | void unlock() { write_locked= 0; } |
| 97 | bool is_locked() { return write_locked; } |
| 98 | |
| 99 | /* Functions that are directly mapped to the underlying handler */ |
| 100 | int rnd_init(bool scan) |
| 101 | { return file->rnd_init(scan); } |
| 102 | /* |
| 103 | We need to have a lock here to protect engines like MyISAM from |
| 104 | simultaneous read and write. For sequence's this is not critical |
| 105 | as this function is used extremely seldom. |
| 106 | */ |
| 107 | int rnd_next(uchar *buf) |
| 108 | { |
| 109 | int error; |
| 110 | table->s->sequence->read_lock(table); |
| 111 | error= file->rnd_next(buf); |
| 112 | table->s->sequence->read_unlock(table); |
| 113 | return error; |
| 114 | } |
| 115 | int rnd_end() |
| 116 | { return file->rnd_end(); } |
| 117 | int rnd_pos(uchar *buf, uchar *pos) |
| 118 | { |
| 119 | int error; |
| 120 | table->s->sequence->read_lock(table); |
| 121 | error= file->rnd_pos(buf, pos); |
| 122 | table->s->sequence->read_unlock(table); |
| 123 | return error; |
| 124 | } |
| 125 | void position(const uchar *record) |
| 126 | { return file->position(record); } |
| 127 | const char *table_type() const |
| 128 | { return file->table_type(); } |
| 129 | ulong index_flags(uint inx, uint part, bool all_parts) const |
| 130 | { return file->index_flags(inx, part, all_parts); } |
| 131 | THR_LOCK_DATA **store_lock(THD *thd, THR_LOCK_DATA **to, |
| 132 | enum thr_lock_type lock_type) |
| 133 | { return file->store_lock(thd, to, lock_type); } |
| 134 | int close(void) |
| 135 | { return file->close(); } |
| 136 | const char **bas_ext() const |
| 137 | { return file->bas_ext(); } |
| 138 | int delete_table(const char*name) |
| 139 | { return file->delete_table(name); } |
| 140 | int rename_table(const char *from, const char *to) |
| 141 | { return file->rename_table(from, to); } |
| 142 | void unbind_psi() |
| 143 | { return file->unbind_psi(); } |
| 144 | void rebind_psi() |
| 145 | { return file->rebind_psi(); } |
| 146 | |
| 147 | bool auto_repair(int error) const |
| 148 | { return file->auto_repair(error); } |
| 149 | int repair(THD* thd, HA_CHECK_OPT* check_opt) |
| 150 | { return file->repair(thd, check_opt); } |
| 151 | bool check_and_repair(THD *thd) |
| 152 | { return file->check_and_repair(thd); } |
| 153 | bool is_crashed() const |
| 154 | { return file->is_crashed(); } |
| 155 | void column_bitmaps_signal() |
| 156 | { return file->column_bitmaps_signal(); } |
| 157 | |
| 158 | /* New methods */ |
| 159 | void register_original_handler(handler *file_arg) |
| 160 | { |
| 161 | file= file_arg; |
| 162 | init(); /* Update cached_table_flags */ |
| 163 | } |
| 164 | }; |
| 165 | #endif |
| 166 | |