ha_federated.h 9.53 KB
Newer Older
Marc Alff's avatar
Marc Alff committed
1
/* Copyright (C) 2003 MySQL AB, 2008-2009 Sun Microsystems, Inc
2 3 4

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
unknown's avatar
unknown committed
5
  the Free Software Foundation; version 2 of the License.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/*
  Please read ha_exmple.cc before reading this file.
  Please keep in mind that the federated storage engine implements all methods
  that are required to be implemented. handler.h has a full list of methods
  that you can implement.
*/

23
#ifdef USE_PRAGMA_INTERFACE
24 25 26 27
#pragma interface			/* gcc class implementation */
#endif

#include <mysql.h>
28

29 30 31 32 33 34 35 36
/* 
  handler::print_error has a case statement for error numbers.
  This value is (10000) is far out of range and will envoke the 
  default: case.  
  (Current error range is 120-159 from include/my_base.h)
*/
#define HA_FEDERATED_ERROR_WITH_REMOTE_SYSTEM 10000

37 38
#define FEDERATED_QUERY_BUFFER_SIZE STRING_BUFFER_USUAL_SIZE * 5
#define FEDERATED_RECORDS_IN_RANGE 2
unknown's avatar
unknown committed
39
#define FEDERATED_MAX_KEY_LENGTH 3500 // Same as innodb
40 41 42 43 44 45

/*
  FEDERATED_SHARE is a structure that will be shared amoung all open handlers
  The example implements the minimum of what you will probably need.
*/
typedef struct st_federated_share {
46 47
  MEM_ROOT mem_root;

unknown's avatar
unknown committed
48 49 50
  bool parsed;
  /* this key is unique db/tablename */
  const char *share_key;
51
  /*
52 53
    the primary select query to be used in rnd_init
  */
54
  char *select_query;
55 56 57
  /*
    remote host info, parse_url supplies
  */
unknown's avatar
unknown committed
58 59
  char *server_name;
  char *connection_string;
60
  char *scheme;
unknown's avatar
unknown committed
61
  char *connect_string;
62 63 64 65
  char *hostname;
  char *username;
  char *password;
  char *database;
66
  char *table_name;
67 68 69
  char *table;
  char *socket;
  char *sport;
unknown's avatar
unknown committed
70
  int share_key_length;
71
  ushort port;
72

73
  size_t table_name_length, server_name_length, connect_string_length, use_count;
Marc Alff's avatar
Marc Alff committed
74
  mysql_mutex_t mutex;
75 76 77 78 79 80 81 82 83 84
  THR_LOCK lock;
} FEDERATED_SHARE;

/*
  Class definition for the storage engine
*/
class ha_federated: public handler
{
  THR_LOCK_DATA lock;      /* MySQL lock */
  FEDERATED_SHARE *share;    /* Shared lock info */
85
  MYSQL *mysql; /* MySQL connection */
86
  MYSQL_RES *stored_result;
87 88
  uint fetch_num; // stores the fetch num
  MYSQL_ROW_OFFSET current_position;  // Current position used by ::position()
89 90
  int remote_error_number;
  char remote_error_buf[FEDERATED_QUERY_BUFFER_SIZE];
unknown's avatar
unknown committed
91
  bool ignore_duplicates, replace_duplicates;
unknown's avatar
unknown committed
92
  bool insert_dup_update;
unknown's avatar
unknown committed
93
  DYNAMIC_STRING bulk_insert;
94 95 96 97 98 99

private:
  /*
      return 0 on success
      return errorcode otherwise
  */
100
  uint convert_row_to_internal_format(uchar *buf, MYSQL_ROW row,
101
                                      MYSQL_RES *result);
102
  bool create_where_from_key(String *to, KEY *key_info, 
103 104
                             const key_range *start_key,
                             const key_range *end_key,
105
                             bool records_in_range, bool eq_range);
106
  int stash_remote_error();
107

unknown's avatar
unknown committed
108 109
  bool append_stmt_insert(String *query);

unknown's avatar
unknown committed
110 111 112
  int read_next(uchar *buf, MYSQL_RES *result);
  int index_read_idx_with_result_set(uchar *buf, uint index,
                                     const uchar *key,
unknown's avatar
unknown committed
113 114 115
                                     uint key_len,
                                     ha_rkey_function find_flag,
                                     MYSQL_RES **result);
116
  int real_query(const char *query, size_t length);
unknown's avatar
unknown committed
117
  int real_connect();
118
public:
119
  ha_federated(handlerton *hton, TABLE_SHARE *table_arg);
120
  ~ha_federated() {}
121 122
  /* The name that will be used for display purposes */
  const char *table_type() const { return "FEDERATED"; }
123 124 125 126
  /*
    Next pointer used in transaction
  */
  ha_federated *trx_next;
127 128 129 130
  /*
    The name of the index type that will be used for display
    don't implement this method unless you really have indexes
   */
131
  // perhaps get index type
132 133 134 135 136 137 138
  const char *index_type(uint inx) { return "REMOTE"; }
  const char **bas_ext() const;
  /*
    This is a list of flags that says what the storage engine
    implements. The current table flags are documented in
    handler.h
  */
139
  ulonglong table_flags() const
140
  {
141
    /* fix server to be able to get remote server table flags */
142
    return (HA_PRIMARY_KEY_IN_READ_INDEX | HA_FILE_BASED
143
            | HA_REC_NOT_IN_SEQ | HA_AUTO_PART_KEY | HA_CAN_INDEX_BLOBS |
144
            HA_BINLOG_ROW_CAPABLE | HA_BINLOG_STMT_CAPABLE |
145
            HA_NO_PREFIX_CHAR_KEYS | HA_PRIMARY_KEY_REQUIRED_FOR_DELETE |
unknown's avatar
unknown committed
146
            HA_NO_TRANSACTIONS /* until fixed by WL#2952 */ |
147
            HA_PARTIAL_COLUMN_READ | HA_NULL_IN_KEY);
148 149 150 151 152 153 154 155 156 157 158
  }
  /*
    This is a bitmap of flags that says how the storage engine
    implements indexes. The current index flags are documented in
    handler.h. If you do not implement indexes, just return zero
    here.

    part is the key part to check. First key part is 0
    If all_parts it's set, MySQL want to know the flags for the combined
    index up to and including 'part'.
  */
159
    /* fix server to be able to get remote server index flags */
160 161
  ulong index_flags(uint inx, uint part, bool all_parts) const
  {
162
    return (HA_READ_NEXT | HA_READ_RANGE | HA_READ_AFTER_KEY);
163 164 165
  }
  uint max_supported_record_length() const { return HA_MAX_REC_LENGTH; }
  uint max_supported_keys()          const { return MAX_KEY; }
166
  uint max_supported_key_parts()     const { return MAX_REF_PARTS; }
unknown's avatar
unknown committed
167
  uint max_supported_key_length()    const { return FEDERATED_MAX_KEY_LENGTH; }
168
  uint max_supported_key_part_length() const { return FEDERATED_MAX_KEY_LENGTH; }
169 170
  /*
    Called in test_quick_select to determine if indexes should be used.
171 172 173 174 175 176 177 178
    Normally, we need to know number of blocks . For federated we need to
    know number of blocks on remote side, and number of packets and blocks
    on the network side (?)
    Talk to Kostja about this - how to get the
    number of rows * ...
    disk scan time on other side (block size, size of the row) + network time ...
    The reason for "records * 1000" is that such a large number forces 
    this to use indexes "
179
  */
180
  double scan_time()
181
  {
182 183
    DBUG_PRINT("info", ("records %lu", (ulong) stats.records));
    return (double)(stats.records*1000); 
184
  }
185 186 187
  /*
    The next method will never be called if you do not implement indexes.
  */
188 189
  double read_time(uint index, uint ranges, ha_rows rows) 
  {
190 191 192 193
    /*
      Per Brian, this number is bugus, but this method must be implemented,
      and at a later date, he intends to document this issue for handler code
    */
194 195
    return (double) rows /  20.0+1;
  }
196

197
  const key_map *keys_to_use_for_scanning() { return &key_map_full; }
198 199 200 201 202 203 204 205 206
  /*
    Everything below are methods that we implment in ha_federated.cc.

    Most of these methods are not obligatory, skip them and
    MySQL will treat them as not implemented
  */
  int open(const char *name, int mode, uint test_if_locked);    // required
  int close(void);                                              // required

unknown's avatar
unknown committed
207 208
  void start_bulk_insert(ha_rows rows);
  int end_bulk_insert();
209 210 211
  int write_row(uchar *buf);
  int update_row(const uchar *old_data, uchar *new_data);
  int delete_row(const uchar *buf);
212
  int index_init(uint keynr, bool sorted);
213
  ha_rows estimate_rows_upper_bound();
214
  int index_read(uchar *buf, const uchar *key,
215
                 uint key_len, enum ha_rkey_function find_flag);
216
  int index_read_idx(uchar *buf, uint idx, const uchar *key,
217
                     uint key_len, enum ha_rkey_function find_flag);
218
  int index_next(uchar *buf);
219
  int index_end();
220 221 222 223
  int read_range_first(const key_range *start_key,
                               const key_range *end_key,
                               bool eq_range, bool sorted);
  int read_range_next();
224 225 226 227 228 229 230 231 232 233
  /*
    unlike index_init(), rnd_init() can be called two times
    without rnd_end() in between (it only makes sense if scan=1).
    then the second call should prepare for the new table scan
    (e.g if rnd_init allocates the cursor, second call should
    position it to the start of the table, no need to deallocate
    and allocate it again
  */
  int rnd_init(bool scan);                                      //required
  int rnd_end();
234
  int rnd_next(uchar *buf);                                      //required
235
  int rnd_next_int(uchar *buf);
236 237
  int rnd_pos(uchar *buf, uchar *pos);                            //required
  void position(const uchar *record);                            //required
238
  int info(uint);                                              //required
unknown's avatar
unknown committed
239
  int extra(ha_extra_function operation);
240

unknown's avatar
unknown committed
241
  void update_auto_increment(void);
242 243 244
  int repair(THD* thd, HA_CHECK_OPT* check_opt);
  int optimize(THD* thd, HA_CHECK_OPT* check_opt);

245 246 247
  int delete_all_rows(void);
  int create(const char *name, TABLE *form,
             HA_CREATE_INFO *create_info);                      //required
248 249
  ha_rows records_in_range(uint inx, key_range *start_key,
                                   key_range *end_key);
250
  uint8 table_cache_type() { return HA_CACHE_TBL_NOCACHE; }
251 252 253

  THR_LOCK_DATA **store_lock(THD *thd, THR_LOCK_DATA **to,
                             enum thr_lock_type lock_type);     //required
unknown's avatar
unknown committed
254
  bool get_error_message(int error, String *buf);
255 256 257 258 259
  int external_lock(THD *thd, int lock_type);
  int connection_commit();
  int connection_rollback();
  int connection_autocommit(bool state);
  int execute_simple_query(const char *query, int len);
unknown's avatar
unknown committed
260
  int reset(void);
261
};
unknown's avatar
unknown committed
262