| 1 | /* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License as published by |
| 5 | the Free Software Foundation; version 2 of the License. |
| 6 | |
| 7 | This program is distributed in the hope that it will be useful, |
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | GNU General Public License for more details. |
| 11 | |
| 12 | You should have received a copy of the GNU General Public License |
| 13 | along with this program; if not, write to the Free Software |
| 14 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 15 | |
| 16 | #include <sys/types.h> |
| 17 | #include <sys/stat.h> |
| 18 | #include <my_dir.h> |
| 19 | #include "transparent_file.h" |
| 20 | |
| 21 | #define DEFAULT_CHAIN_LENGTH 512 |
| 22 | /* |
| 23 | Version for file format. |
| 24 | 1 - Initial Version. That is, the version when the metafile was introduced. |
| 25 | */ |
| 26 | |
| 27 | #define TINA_VERSION 1 |
| 28 | |
| 29 | typedef struct st_tina_share { |
| 30 | char *table_name; |
| 31 | char data_file_name[FN_REFLEN]; |
| 32 | uint table_name_length, use_count; |
| 33 | /* |
| 34 | Below flag is needed to make log tables work with concurrent insert. |
| 35 | For more details see comment to ha_tina::update_status. |
| 36 | */ |
| 37 | my_bool is_log_table; |
| 38 | /* |
| 39 | Here we save the length of the file for readers. This is updated by |
| 40 | inserts, updates and deletes. The var is initialized along with the |
| 41 | share initialization. |
| 42 | */ |
| 43 | my_off_t saved_data_file_length; |
| 44 | mysql_mutex_t mutex; |
| 45 | THR_LOCK lock; |
| 46 | bool update_file_opened; |
| 47 | bool tina_write_opened; |
| 48 | File meta_file; /* Meta file we use */ |
| 49 | File tina_write_filedes; /* File handler for readers */ |
| 50 | bool crashed; /* Meta file is crashed */ |
| 51 | ha_rows rows_recorded; /* Number of rows in tables */ |
| 52 | uint data_file_version; /* Version of the data file used */ |
| 53 | } TINA_SHARE; |
| 54 | |
| 55 | struct tina_set { |
| 56 | my_off_t begin; |
| 57 | my_off_t end; |
| 58 | }; |
| 59 | |
| 60 | class ha_tina: public handler |
| 61 | { |
| 62 | THR_LOCK_DATA lock; /* MySQL lock */ |
| 63 | TINA_SHARE *share; /* Shared lock info */ |
| 64 | my_off_t current_position; /* Current position in the file during a file scan */ |
| 65 | my_off_t next_position; /* Next position in the file scan */ |
| 66 | my_off_t local_saved_data_file_length; /* save position for reads */ |
| 67 | my_off_t temp_file_length; |
| 68 | uchar byte_buffer[IO_SIZE]; |
| 69 | Transparent_file *file_buff; |
| 70 | File data_file; /* File handler for readers */ |
| 71 | File update_temp_file; |
| 72 | String buffer; |
| 73 | /* |
| 74 | The chain contains "holes" in the file, occurred because of |
| 75 | deletes/updates. It is used in rnd_end() to get rid of them |
| 76 | in the end of the query. |
| 77 | */ |
| 78 | tina_set chain_buffer[DEFAULT_CHAIN_LENGTH]; |
| 79 | tina_set *chain; |
| 80 | tina_set *chain_ptr; |
| 81 | uchar chain_alloced; |
| 82 | uint32 chain_size; |
| 83 | uint local_data_file_version; /* Saved version of the data file used */ |
| 84 | bool records_is_known, found_end_of_file; |
| 85 | MEM_ROOT blobroot; |
| 86 | |
| 87 | private: |
| 88 | int curr_lock_type; |
| 89 | |
| 90 | bool get_write_pos(my_off_t *end_pos, tina_set *closest_hole); |
| 91 | int open_update_temp_file_if_needed(); |
| 92 | int init_tina_writer(); |
| 93 | int init_data_file(); |
| 94 | |
| 95 | public: |
| 96 | ha_tina(handlerton *hton, TABLE_SHARE *table_arg); |
| 97 | ~ha_tina() |
| 98 | { |
| 99 | if (chain_alloced) |
| 100 | my_free(chain); |
| 101 | if (file_buff) |
| 102 | delete file_buff; |
| 103 | free_root(&blobroot, MYF(0)); |
| 104 | } |
| 105 | const char *index_type(uint inx) { return "NONE" ; } |
| 106 | ulonglong table_flags() const |
| 107 | { |
| 108 | return (HA_NO_TRANSACTIONS | HA_REC_NOT_IN_SEQ | HA_NO_AUTO_INCREMENT | |
| 109 | HA_BINLOG_ROW_CAPABLE | HA_BINLOG_STMT_CAPABLE | HA_CAN_EXPORT | |
| 110 | HA_CAN_REPAIR); |
| 111 | } |
| 112 | ulong index_flags(uint idx, uint part, bool all_parts) const |
| 113 | { |
| 114 | /* |
| 115 | We will never have indexes so this will never be called(AKA we return |
| 116 | zero) |
| 117 | */ |
| 118 | return 0; |
| 119 | } |
| 120 | uint max_record_length() const { return HA_MAX_REC_LENGTH; } |
| 121 | uint max_keys() const { return 0; } |
| 122 | uint max_key_parts() const { return 0; } |
| 123 | uint max_key_length() const { return 0; } |
| 124 | /* |
| 125 | Called in test_quick_select to determine if indexes should be used. |
| 126 | */ |
| 127 | virtual double scan_time() { return (double) (stats.records+stats.deleted) / 20.0+10; } |
| 128 | /* The next method will never be called */ |
| 129 | virtual bool fast_key_read() { return 1;} |
| 130 | /* |
| 131 | TODO: return actual upper bound of number of records in the table. |
| 132 | (e.g. save number of records seen on full table scan and/or use file size |
| 133 | as upper bound) |
| 134 | */ |
| 135 | ha_rows estimate_rows_upper_bound() { return HA_POS_ERROR; } |
| 136 | |
| 137 | int open(const char *name, int mode, uint open_options); |
| 138 | int close(void); |
| 139 | int write_row(uchar * buf); |
| 140 | int update_row(const uchar * old_data, const uchar * new_data); |
| 141 | int delete_row(const uchar * buf); |
| 142 | int rnd_init(bool scan=1); |
| 143 | int rnd_next(uchar *buf); |
| 144 | int rnd_pos(uchar * buf, uchar *pos); |
| 145 | bool check_and_repair(THD *thd); |
| 146 | int check(THD* thd, HA_CHECK_OPT* check_opt); |
| 147 | bool is_crashed() const; |
| 148 | int rnd_end(); |
| 149 | int repair(THD* thd, HA_CHECK_OPT* check_opt); |
| 150 | /* This is required for SQL layer to know that we support autorepair */ |
| 151 | bool auto_repair(int error) const |
| 152 | { return error == HA_ERR_CRASHED_ON_USAGE; } |
| 153 | bool auto_repair() const { return 1; } |
| 154 | void position(const uchar *record); |
| 155 | int info(uint); |
| 156 | int reset(); |
| 157 | int (enum ha_extra_function operation); |
| 158 | int delete_all_rows(void); |
| 159 | int create(const char *name, TABLE *form, HA_CREATE_INFO *create_info); |
| 160 | bool check_if_incompatible_data(HA_CREATE_INFO *info, |
| 161 | uint table_changes); |
| 162 | |
| 163 | int external_lock(THD *thd, int lock_type); |
| 164 | |
| 165 | THR_LOCK_DATA **store_lock(THD *thd, THR_LOCK_DATA **to, |
| 166 | enum thr_lock_type lock_type); |
| 167 | |
| 168 | /* |
| 169 | These functions used to get/update status of the handler. |
| 170 | Needed to enable concurrent inserts. |
| 171 | */ |
| 172 | void get_status(); |
| 173 | void update_status(); |
| 174 | |
| 175 | /* The following methods were added just for TINA */ |
| 176 | int encode_quote(const uchar *buf); |
| 177 | int find_current_row(uchar *buf); |
| 178 | int chain_append(); |
| 179 | }; |
| 180 | |
| 181 | |