mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / sql / ha_ndbcluster.h
blob07f288ff7c5185377345ed07bfea670ae90e9064
1 /* Copyright (c) 2000-2008 MySQL AB
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.
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.
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 Street, Fifth Floor, Boston, MA 02110-1301, USA */
17 This file defines the NDB Cluster handler: the interface between MySQL and
18 NDB Cluster
21 /* The class defining a handle to an NDB Cluster table */
23 #ifdef USE_PRAGMA_INTERFACE
24 #pragma interface /* gcc class implementation */
25 #endif
27 /* Blob tables and events are internal to NDB and must never be accessed */
28 #define IS_NDB_BLOB_PREFIX(A) is_prefix(A, "NDB$BLOB")
30 #include <NdbApi.hpp>
31 #include <ndbapi_limits.h>
33 #define NDB_HIDDEN_PRIMARY_KEY_LENGTH 8
34 #define NDB_DEFAULT_AUTO_PREFETCH 32
37 class Ndb; // Forward declaration
38 class NdbOperation; // Forward declaration
39 class NdbTransaction; // Forward declaration
40 class NdbRecAttr; // Forward declaration
41 class NdbScanOperation;
42 class NdbIndexScanOperation;
43 class NdbBlob;
44 class NdbIndexStat;
45 class NdbEventOperation;
46 class ha_ndbcluster_cond;
48 // connectstring to cluster if given by mysqld
49 extern const char *ndbcluster_connectstring;
50 extern ulong ndb_cache_check_time;
51 #ifdef HAVE_NDB_BINLOG
52 extern ulong ndb_report_thresh_binlog_epoch_slip;
53 extern ulong ndb_report_thresh_binlog_mem_usage;
54 #endif
56 typedef enum ndb_index_type {
57 UNDEFINED_INDEX = 0,
58 PRIMARY_KEY_INDEX = 1,
59 PRIMARY_KEY_ORDERED_INDEX = 2,
60 UNIQUE_INDEX = 3,
61 UNIQUE_ORDERED_INDEX = 4,
62 ORDERED_INDEX = 5
63 } NDB_INDEX_TYPE;
65 typedef enum ndb_index_status {
66 UNDEFINED = 0,
67 ACTIVE = 1,
68 TO_BE_DROPPED = 2
69 } NDB_INDEX_STATUS;
71 typedef struct ndb_index_data {
72 NDB_INDEX_TYPE type;
73 NDB_INDEX_STATUS status;
74 const NdbDictionary::Index *index;
75 const NdbDictionary::Index *unique_index;
76 unsigned char *unique_index_attrid_map;
77 bool null_in_unique_index;
78 // In this version stats are not shared between threads
79 NdbIndexStat* index_stat;
80 uint index_stat_cache_entries;
81 // Simple counter mechanism to decide when to connect to db
82 uint index_stat_update_freq;
83 uint index_stat_query_count;
84 } NDB_INDEX_DATA;
86 typedef enum ndb_write_op {
87 NDB_INSERT = 0,
88 NDB_UPDATE = 1,
89 NDB_PK_UPDATE = 2
90 } NDB_WRITE_OP;
92 typedef union { const NdbRecAttr *rec; NdbBlob *blob; void *ptr; } NdbValue;
94 int get_ndb_blobs_value(TABLE* table, NdbValue* value_array,
95 uchar*& buffer, uint& buffer_size,
96 my_ptrdiff_t ptrdiff);
98 typedef enum {
99 NSS_INITIAL= 0,
100 NSS_DROPPED,
101 NSS_ALTERED
102 } NDB_SHARE_STATE;
104 typedef struct st_ndbcluster_share {
105 NDB_SHARE_STATE state;
106 MEM_ROOT mem_root;
107 THR_LOCK lock;
108 pthread_mutex_t mutex;
109 char *key;
110 uint key_length;
111 THD *util_lock;
112 uint use_count;
113 uint commit_count_lock;
114 ulonglong commit_count;
115 char *db;
116 char *table_name;
117 Ndb::TupleIdRange tuple_id_range;
118 #ifdef HAVE_NDB_BINLOG
119 uint32 connect_count;
120 uint32 flags;
121 NdbEventOperation *op;
122 NdbEventOperation *op_old; // for rename table
123 char *old_names; // for rename table
124 TABLE_SHARE *table_share;
125 TABLE *table;
126 uchar *record[2]; // pointer to allocated records for receiving data
127 NdbValue *ndb_value[2];
128 MY_BITMAP *subscriber_bitmap;
129 #endif
130 } NDB_SHARE;
132 inline
133 NDB_SHARE_STATE
134 get_ndb_share_state(NDB_SHARE *share)
136 NDB_SHARE_STATE state;
137 pthread_mutex_lock(&share->mutex);
138 state= share->state;
139 pthread_mutex_unlock(&share->mutex);
140 return state;
143 inline
144 void
145 set_ndb_share_state(NDB_SHARE *share, NDB_SHARE_STATE state)
147 pthread_mutex_lock(&share->mutex);
148 share->state= state;
149 pthread_mutex_unlock(&share->mutex);
152 struct Ndb_tuple_id_range_guard {
153 Ndb_tuple_id_range_guard(NDB_SHARE* _share) :
154 share(_share),
155 range(share->tuple_id_range) {
156 pthread_mutex_lock(&share->mutex);
158 ~Ndb_tuple_id_range_guard() {
159 pthread_mutex_unlock(&share->mutex);
161 NDB_SHARE* share;
162 Ndb::TupleIdRange& range;
165 #ifdef HAVE_NDB_BINLOG
166 /* NDB_SHARE.flags */
167 #define NSF_HIDDEN_PK 1 /* table has hidden primary key */
168 #define NSF_BLOB_FLAG 2 /* table has blob attributes */
169 #define NSF_NO_BINLOG 4 /* table should not be binlogged */
170 #endif
172 typedef enum ndb_query_state_bits {
173 NDB_QUERY_NORMAL = 0,
174 NDB_QUERY_MULTI_READ_RANGE = 1
175 } NDB_QUERY_STATE_BITS;
178 Place holder for ha_ndbcluster thread specific data
181 enum THD_NDB_OPTIONS
183 TNO_NO_LOG_SCHEMA_OP= 1 << 0
186 enum THD_NDB_TRANS_OPTIONS
188 TNTO_INJECTED_APPLY_STATUS= 1 << 0
189 ,TNTO_NO_LOGGING= 1 << 1
192 struct Ndb_local_table_statistics {
193 int no_uncommitted_rows_count;
194 ulong last_count;
195 ha_rows records;
198 class Thd_ndb
200 public:
201 Thd_ndb();
202 ~Thd_ndb();
204 void init_open_tables();
206 Ndb *ndb;
207 ulong count;
208 uint lock_count;
209 uint start_stmt_count;
210 NdbTransaction *trans;
211 bool m_error;
212 bool m_slow_path;
213 int m_error_code;
214 uint32 m_query_id; /* query id whn m_error_code was set */
215 uint32 options;
216 uint32 trans_options;
217 List<NDB_SHARE> changed_tables;
218 uint query_state;
219 HASH open_tables;
222 class ha_ndbcluster: public handler
224 public:
225 ha_ndbcluster(handlerton *hton, TABLE_SHARE *table);
226 ~ha_ndbcluster();
228 int ha_initialise();
229 int open(const char *name, int mode, uint test_if_locked);
230 int close(void);
232 int write_row(uchar *buf);
233 int update_row(const uchar *old_data, uchar *new_data);
234 int delete_row(const uchar *buf);
235 int index_init(uint index, bool sorted);
236 int index_end();
237 int index_read(uchar *buf, const uchar *key, uint key_len,
238 enum ha_rkey_function find_flag);
239 int index_next(uchar *buf);
240 int index_prev(uchar *buf);
241 int index_first(uchar *buf);
242 int index_last(uchar *buf);
243 int index_read_last(uchar * buf, const uchar * key, uint key_len);
244 int rnd_init(bool scan);
245 int rnd_end();
246 int rnd_next(uchar *buf);
247 int rnd_pos(uchar *buf, uchar *pos);
248 void position(const uchar *record);
249 int read_range_first(const key_range *start_key,
250 const key_range *end_key,
251 bool eq_range, bool sorted);
252 int read_range_first_to_buf(const key_range *start_key,
253 const key_range *end_key,
254 bool eq_range, bool sorted,
255 uchar* buf);
256 int read_range_next();
257 int alter_tablespace(st_alter_tablespace *info);
260 * Multi range stuff
262 int read_multi_range_first(KEY_MULTI_RANGE **found_range_p,
263 KEY_MULTI_RANGE*ranges, uint range_count,
264 bool sorted, HANDLER_BUFFER *buffer);
265 int read_multi_range_next(KEY_MULTI_RANGE **found_range_p);
266 bool null_value_index_search(KEY_MULTI_RANGE *ranges,
267 KEY_MULTI_RANGE *end_range,
268 HANDLER_BUFFER *buffer);
270 bool get_error_message(int error, String *buf);
271 ha_rows records();
272 ha_rows estimate_rows_upper_bound()
273 { return HA_POS_ERROR; }
274 int info(uint);
275 void get_dynamic_partition_info(PARTITION_INFO *stat_info, uint part_id);
276 int extra(enum ha_extra_function operation);
277 int extra_opt(enum ha_extra_function operation, ulong cache_size);
278 int reset();
279 int external_lock(THD *thd, int lock_type);
280 void unlock_row();
281 int start_stmt(THD *thd, thr_lock_type lock_type);
282 void print_error(int error, myf errflag);
283 const char * table_type() const;
284 const char ** bas_ext() const;
285 ulonglong table_flags(void) const;
286 void prepare_for_alter();
287 int add_index(TABLE *table_arg, KEY *key_info, uint num_of_keys);
288 int prepare_drop_index(TABLE *table_arg, uint *key_num, uint num_of_keys);
289 int final_drop_index(TABLE *table_arg);
290 void set_part_info(partition_info *part_info);
291 ulong index_flags(uint idx, uint part, bool all_parts) const;
292 uint max_supported_record_length() const;
293 uint max_supported_keys() const;
294 uint max_supported_key_parts() const;
295 uint max_supported_key_length() const;
296 uint max_supported_key_part_length() const;
298 int rename_table(const char *from, const char *to);
299 int delete_table(const char *name);
300 int create(const char *name, TABLE *form, HA_CREATE_INFO *info);
301 int create_handler_files(const char *file, const char *old_name,
302 int action_flag, HA_CREATE_INFO *info);
303 int get_default_no_partitions(HA_CREATE_INFO *info);
304 bool get_no_parts(const char *name, uint *no_parts);
305 void set_auto_partitions(partition_info *part_info);
306 virtual bool is_fatal_error(int error, uint flags)
308 if (!handler::is_fatal_error(error, flags) ||
309 error == HA_ERR_NO_PARTITION_FOUND)
310 return FALSE;
311 return TRUE;
314 THR_LOCK_DATA **store_lock(THD *thd,
315 THR_LOCK_DATA **to,
316 enum thr_lock_type lock_type);
318 bool low_byte_first() const;
320 const char* index_type(uint key_number);
322 double scan_time();
323 ha_rows records_in_range(uint inx, key_range *min_key, key_range *max_key);
324 void start_bulk_insert(ha_rows rows);
325 int end_bulk_insert();
327 static Thd_ndb* seize_thd_ndb();
328 static void release_thd_ndb(Thd_ndb* thd_ndb);
330 static void set_dbname(const char *pathname, char *dbname);
331 static void set_tabname(const char *pathname, char *tabname);
334 Condition pushdown
338 Push condition down to the table handler.
339 SYNOPSIS
340 cond_push()
341 cond Condition to be pushed. The condition tree must not be
342 modified by the by the caller.
343 RETURN
344 The 'remainder' condition that caller must use to filter out records.
345 NULL means the handler will not return rows that do not match the
346 passed condition.
347 NOTES
348 The pushed conditions form a stack (from which one can remove the
349 last pushed condition using cond_pop).
350 The table handler filters out rows using (pushed_cond1 AND pushed_cond2
351 AND ... AND pushed_condN)
352 or less restrictive condition, depending on handler's capabilities.
354 handler->reset() call empties the condition stack.
355 Calls to rnd_init/rnd_end, index_init/index_end etc do not affect the
356 condition stack.
357 The current implementation supports arbitrary AND/OR nested conditions
358 with comparisons between columns and constants (including constant
359 expressions and function calls) and the following comparison operators:
360 =, !=, >, >=, <, <=, like, "not like", "is null", and "is not null".
361 Negated conditions are supported by NOT which generate NAND/NOR groups.
363 const COND *cond_push(const COND *cond);
365 Pop the top condition from the condition stack of the handler instance.
366 SYNOPSIS
367 cond_pop()
368 Pops the top if condition stack, if stack is not empty
370 void cond_pop();
372 uint8 table_cache_type();
375 * Internal to ha_ndbcluster, used by C functions
377 int ndb_err(NdbTransaction*);
379 my_bool register_query_cache_table(THD *thd, char *table_key,
380 uint key_length,
381 qc_engine_callback *engine_callback,
382 ulonglong *engine_data);
384 bool check_if_incompatible_data(HA_CREATE_INFO *info,
385 uint table_changes);
387 private:
388 friend int ndbcluster_drop_database_impl(const char *path);
389 friend int ndb_handle_schema_change(THD *thd,
390 Ndb *ndb, NdbEventOperation *pOp,
391 NDB_SHARE *share);
393 static int delete_table(ha_ndbcluster *h, Ndb *ndb,
394 const char *path,
395 const char *db,
396 const char *table_name);
397 int create_ndb_index(const char *name, KEY *key_info, bool unique);
398 int create_ordered_index(const char *name, KEY *key_info);
399 int create_unique_index(const char *name, KEY *key_info);
400 int create_index(const char *name, KEY *key_info,
401 NDB_INDEX_TYPE idx_type, uint idx_no);
402 // Index list management
403 int create_indexes(Ndb *ndb, TABLE *tab);
404 int open_indexes(Ndb *ndb, TABLE *tab, bool ignore_error);
405 void renumber_indexes(Ndb *ndb, TABLE *tab);
406 int drop_indexes(Ndb *ndb, TABLE *tab);
407 int add_index_handle(THD *thd, NdbDictionary::Dictionary *dict,
408 KEY *key_info, const char *index_name, uint index_no);
409 int get_metadata(const char* path);
410 void release_metadata(THD *thd, Ndb *ndb);
411 NDB_INDEX_TYPE get_index_type(uint idx_no) const;
412 NDB_INDEX_TYPE get_index_type_from_table(uint index_no) const;
413 NDB_INDEX_TYPE get_index_type_from_key(uint index_no, KEY *key_info,
414 bool primary) const;
415 bool has_null_in_unique_index(uint idx_no) const;
416 bool check_index_fields_not_null(KEY *key_info);
418 uint set_up_partition_info(partition_info *part_info,
419 TABLE *table,
420 void *tab);
421 char* get_tablespace_name(THD *thd, char *name, uint name_len);
422 int set_range_data(void *tab, partition_info* part_info);
423 int set_list_data(void *tab, partition_info* part_info);
424 int complemented_read(const uchar *old_data, uchar *new_data,
425 uint32 old_part_id);
426 int pk_read(const uchar *key, uint key_len, uchar *buf, uint32 part_id);
427 int ordered_index_scan(const key_range *start_key,
428 const key_range *end_key,
429 bool sorted, bool descending, uchar* buf,
430 part_id_range *part_spec);
431 int unique_index_read(const uchar *key, uint key_len,
432 uchar *buf);
433 int unique_index_scan(const KEY* key_info,
434 const uchar *key,
435 uint key_len,
436 uchar *buf);
437 int full_table_scan(uchar * buf);
439 bool check_all_operations_for_error(NdbTransaction *trans,
440 const NdbOperation *first,
441 const NdbOperation *last,
442 uint errcode);
443 int peek_indexed_rows(const uchar *record, NDB_WRITE_OP write_op);
444 int fetch_next(NdbScanOperation* op);
445 int set_auto_inc(Field *field);
446 int next_result(uchar *buf);
447 int define_read_attrs(uchar* buf, NdbOperation* op);
448 int filtered_scan(const uchar *key, uint key_len,
449 uchar *buf,
450 enum ha_rkey_function find_flag);
451 int close_scan();
452 void unpack_record(uchar *buf);
453 int get_ndb_lock_type(enum thr_lock_type type);
455 void set_dbname(const char *pathname);
456 void set_tabname(const char *pathname);
458 bool set_hidden_key(NdbOperation*,
459 uint fieldnr, const uchar* field_ptr);
460 int set_ndb_key(NdbOperation*, Field *field,
461 uint fieldnr, const uchar* field_ptr);
462 int set_ndb_value(NdbOperation*, Field *field, uint fieldnr,
463 int row_offset= 0, bool *set_blob_value= 0);
464 int get_ndb_value(NdbOperation*, Field *field, uint fieldnr, uchar*);
465 int get_ndb_partition_id(NdbOperation *);
466 friend int g_get_ndb_blobs_value(NdbBlob *ndb_blob, void *arg);
467 int set_primary_key(NdbOperation *op, const uchar *key);
468 int set_primary_key_from_record(NdbOperation *op, const uchar *record);
469 bool check_index_fields_in_write_set(uint keyno);
470 int set_index_key_from_record(NdbOperation *op, const uchar *record,
471 uint keyno);
472 int set_bounds(NdbIndexScanOperation*, uint inx, bool rir,
473 const key_range *keys[2], uint= 0);
474 int key_cmp(uint keynr, const uchar * old_row, const uchar * new_row);
475 int set_index_key(NdbOperation *, const KEY *key_info, const uchar *key_ptr);
476 void print_results();
478 virtual void get_auto_increment(ulonglong offset, ulonglong increment,
479 ulonglong nb_desired_values,
480 ulonglong *first_value,
481 ulonglong *nb_reserved_values);
482 bool uses_blob_value();
484 char *update_table_comment(const char * comment);
486 int write_ndb_file(const char *name);
488 int check_ndb_connection(THD* thd= current_thd);
490 void set_rec_per_key();
491 int records_update();
492 void no_uncommitted_rows_execute_failure();
493 void no_uncommitted_rows_update(int);
494 void no_uncommitted_rows_reset(THD *);
496 void release_completed_operations(NdbTransaction*, bool);
498 friend int execute_commit(ha_ndbcluster*, NdbTransaction*);
499 friend int execute_no_commit_ignore_no_key(ha_ndbcluster*, NdbTransaction*);
500 friend int execute_no_commit(ha_ndbcluster*, NdbTransaction*, bool);
501 friend int execute_no_commit_ie(ha_ndbcluster*, NdbTransaction*, bool);
503 void transaction_checks(THD *thd);
504 int start_statement(THD *thd, Thd_ndb *thd_ndb, Ndb* ndb);
505 int init_handler_for_statement(THD *thd, Thd_ndb *thd_ndb);
507 NdbTransaction *m_active_trans;
508 NdbScanOperation *m_active_cursor;
509 const NdbDictionary::Table *m_table;
510 struct Ndb_local_table_statistics *m_table_info;
511 struct Ndb_local_table_statistics m_table_info_instance;
512 char m_dbname[FN_HEADLEN];
513 //char m_schemaname[FN_HEADLEN];
514 char m_tabname[FN_HEADLEN];
515 ulonglong m_table_flags;
516 THR_LOCK_DATA m_lock;
517 bool m_lock_tuple;
518 NDB_SHARE *m_share;
519 NDB_INDEX_DATA m_index[MAX_KEY];
520 // NdbRecAttr has no reference to blob
521 NdbValue m_value[NDB_MAX_ATTRIBUTES_IN_TABLE];
522 uchar m_ref[NDB_HIDDEN_PRIMARY_KEY_LENGTH];
523 partition_info *m_part_info;
524 uint32 m_part_id;
525 uchar *m_rec0;
526 Field **m_part_field_array;
527 bool m_use_partition_function;
528 bool m_sorted;
529 bool m_use_write;
530 bool m_ignore_dup_key;
531 bool m_has_unique_index;
532 bool m_primary_key_update;
533 bool m_write_op;
534 bool m_ignore_no_key;
535 ha_rows m_rows_to_insert; // TODO: merge it with handler::estimation_rows_to_insert?
536 ha_rows m_rows_inserted;
537 ha_rows m_bulk_insert_rows;
538 ha_rows m_rows_changed;
539 bool m_bulk_insert_not_flushed;
540 bool m_delete_cannot_batch;
541 bool m_update_cannot_batch;
542 ha_rows m_ops_pending;
543 bool m_skip_auto_increment;
544 bool m_blobs_pending;
545 bool m_slow_path;
546 my_ptrdiff_t m_blobs_offset;
547 // memory for blobs in one tuple
548 uchar *m_blobs_buffer;
549 uint32 m_blobs_buffer_size;
550 uint m_dupkey;
551 // set from thread variables at external lock
552 bool m_ha_not_exact_count;
553 bool m_force_send;
554 ha_rows m_autoincrement_prefetch;
555 bool m_transaction_on;
557 ha_ndbcluster_cond *m_cond;
558 bool m_disable_multi_read;
559 uchar *m_multi_range_result_ptr;
560 KEY_MULTI_RANGE *m_multi_ranges;
561 KEY_MULTI_RANGE *m_multi_range_defined;
562 const NdbOperation *m_current_multi_operation;
563 NdbIndexScanOperation *m_multi_cursor;
564 uchar *m_multi_range_cursor_result_ptr;
565 int setup_recattr(const NdbRecAttr*);
566 Ndb *get_ndb();
569 extern SHOW_VAR ndb_status_variables[];
571 int ndbcluster_discover(THD* thd, const char* dbname, const char* name,
572 const void** frmblob, uint* frmlen);
573 int ndbcluster_find_files(THD *thd,const char *db,const char *path,
574 const char *wild, bool dir, List<LEX_STRING> *files);
575 int ndbcluster_table_exists_in_engine(THD* thd,
576 const char *db, const char *name);
577 void ndbcluster_print_error(int error, const NdbOperation *error_op);
579 static const char ndbcluster_hton_name[]= "ndbcluster";
580 static const int ndbcluster_hton_name_length=sizeof(ndbcluster_hton_name)-1;
581 extern int ndbcluster_terminating;
582 extern int ndb_util_thread_running;
583 extern pthread_cond_t COND_ndb_util_ready;