ha_federated.h 8.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* Copyright (C) 2003 MySQL AB

  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
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  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.
*/

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

#include <mysql.h>
29

30 31 32 33 34 35 36 37
/* 
  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

38 39
#define FEDERATED_QUERY_BUFFER_SIZE STRING_BUFFER_USUAL_SIZE * 5
#define FEDERATED_RECORDS_IN_RANGE 2
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 48
    the primary select query to be used in rnd_init
  */
49
  char *select_query;
50 51 52
  /*
    remote host info, parse_url supplies
  */
53
  char *scheme;
unknown's avatar
unknown committed
54
  char *connect_string;
55 56 57 58
  char *hostname;
  char *username;
  char *password;
  char *database;
59
  char *table_name;
60 61 62
  char *table;
  char *socket;
  char *sport;
63
  ushort port;
unknown's avatar
unknown committed
64
  uint table_name_length, connect_string_length, use_count;
65 66 67 68 69 70 71 72 73 74 75
  pthread_mutex_t mutex;
  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 */
76
  MYSQL *mysql; /* MySQL connection */
77
  MYSQL_RES *stored_result;
78 79
  uint fetch_num; // stores the fetch num
  MYSQL_ROW_OFFSET current_position;  // Current position used by ::position()
80 81
  int remote_error_number;
  char remote_error_buf[FEDERATED_QUERY_BUFFER_SIZE];
82 83 84 85 86 87

private:
  /*
      return 0 on success
      return errorcode otherwise
  */
unknown's avatar
unknown committed
88
  uint convert_row_to_internal_format(byte *buf, MYSQL_ROW row,
89
                                      MYSQL_RES *result);
90
  bool create_where_from_key(String *to, KEY *key_info, 
91 92
                             const key_range *start_key,
                             const key_range *end_key,
93
                             bool records_in_range, bool eq_range);
94
  int stash_remote_error();
95 96

public:
unknown's avatar
unknown committed
97
  ha_federated(TABLE_SHARE *table_arg);
98
  ~ha_federated() {}
99 100
  /* The name that will be used for display purposes */
  const char *table_type() const { return "FEDERATED"; }
101 102 103 104
  /*
    Next pointer used in transaction
  */
  ha_federated *trx_next;
105 106 107 108
  /*
    The name of the index type that will be used for display
    don't implement this method unless you really have indexes
   */
109
  // perhaps get index type
110 111 112 113 114 115 116
  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
  */
117
  ulonglong table_flags() const
118
  {
119
    /* fix server to be able to get remote server table flags */
120 121 122
    return (HA_PRIMARY_KEY_IN_READ_INDEX | HA_FILE_BASED | HA_REC_NOT_IN_SEQ |
            HA_AUTO_PART_KEY | HA_CAN_INDEX_BLOBS| HA_NO_PREFIX_CHAR_KEYS |
            HA_PRIMARY_KEY_REQUIRED_FOR_DELETE | HA_PARTIAL_COLUMN_READ);
123 124 125 126 127 128 129 130 131 132 133
  }
  /*
    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'.
  */
134
    /* fix server to be able to get remote server index flags */
135 136
  ulong index_flags(uint inx, uint part, bool all_parts) const
  {
137
    return (HA_READ_NEXT | HA_READ_RANGE | HA_READ_AFTER_KEY);
138 139 140
  }
  uint max_supported_record_length() const { return HA_MAX_REC_LENGTH; }
  uint max_supported_keys()          const { return MAX_KEY; }
141 142
  uint max_supported_key_parts()     const { return MAX_REF_PARTS; }
  uint max_supported_key_length()    const { return MAX_KEY_LENGTH; }
143 144
  /*
    Called in test_quick_select to determine if indexes should be used.
145 146 147 148 149 150 151 152
    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 "
153
  */
154
  double scan_time()
155
  {
156 157
    DBUG_PRINT("info", ("records %lu", (ulong) stats.records));
    return (double)(stats.records*1000); 
158
  }
159 160 161
  /*
    The next method will never be called if you do not implement indexes.
  */
162 163
  double read_time(uint index, uint ranges, ha_rows rows) 
  {
164 165 166 167
    /*
      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
    */
168 169
    return (double) rows /  20.0+1;
  }
170

171
  const key_map *keys_to_use_for_scanning() { return &key_map_full; }
172 173 174 175 176 177 178 179 180
  /*
    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

181 182 183
  int write_row(byte *buf);
  int update_row(const byte *old_data, byte *new_data);
  int delete_row(const byte *buf);
184
  int index_init(uint keynr, bool sorted);
185
  int index_read(byte *buf, const byte *key,
186
                 uint key_len, enum ha_rkey_function find_flag);
187
  int index_read_idx(byte *buf, uint idx, const byte *key,
188
                     uint key_len, enum ha_rkey_function find_flag);
189
  int index_next(byte *buf);
190
  int index_end();
191 192 193 194
  int read_range_first(const key_range *start_key,
                               const key_range *end_key,
                               bool eq_range, bool sorted);
  int read_range_next();
195 196 197 198 199 200 201 202 203 204 205
  /*
    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();
  int rnd_next(byte *buf);                                      //required
206
  int rnd_pos(byte *buf, byte *pos);                            //required
207 208 209
  void position(const byte *record);                            //required
  void info(uint);                                              //required

unknown's avatar
unknown committed
210
  void update_auto_increment(void);
211 212 213
  int repair(THD* thd, HA_CHECK_OPT* check_opt);
  int optimize(THD* thd, HA_CHECK_OPT* check_opt);

214 215 216
  int delete_all_rows(void);
  int create(const char *name, TABLE *form,
             HA_CREATE_INFO *create_info);                      //required
217 218
  ha_rows records_in_range(uint inx, key_range *start_key,
                                   key_range *end_key);
219
  uint8 table_cache_type() { return HA_CACHE_TBL_NOCACHE; }
220 221 222

  THR_LOCK_DATA **store_lock(THD *thd, THR_LOCK_DATA **to,
                             enum thr_lock_type lock_type);     //required
223
  virtual bool get_error_message(int error, String *buf);
224 225 226 227 228
  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
229 230 231 232 233 234 235

  int read_next(byte *buf, MYSQL_RES *result);
  int index_read_idx_with_result_set(byte *buf, uint index,
                                     const byte *key,
                                     uint key_len,
                                     ha_rkey_function find_flag,
                                     MYSQL_RES **result);
236
};
unknown's avatar
unknown committed
237

238
int federated_db_init(void);
239
int federated_db_end(ha_panic_function type);
240