1 /*
2    Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 /*
26   This file defines the NDB Cluster handler: the interface between MySQL and
27   NDB Cluster
28 */
29 
30 
31 /* Blob tables and events are internal to NDB and must never be accessed */
32 #define IS_NDB_BLOB_PREFIX(A) is_prefix(A, "NDB$BLOB")
33 
34 #include <ndbapi/NdbApi.hpp>
35 #include <ndbapi/ndbapi_limits.h>
36 #include <kernel/ndb_limits.h>
37 
38 #define NDB_IGNORE_VALUE(x) (void)x
39 
40 #define NDB_HIDDEN_PRIMARY_KEY_LENGTH 8
41 
42 class Ndb;             // Forward declaration
43 class NdbOperation;    // Forward declaration
44 class NdbTransaction;  // Forward declaration
45 class NdbRecAttr;      // Forward declaration
46 class NdbScanOperation;
47 class NdbIndexScanOperation;
48 class NdbBlob;
49 class NdbIndexStat;
50 class NdbEventOperation;
51 class ha_ndbcluster_cond;
52 class Ndb_event_data;
53 class NdbQuery;
54 class NdbQueryOperation;
55 class NdbQueryOperationTypeWrapper;
56 class NdbQueryParamValue;
57 class ndb_pushed_join;
58 
59 typedef enum ndb_index_type {
60   UNDEFINED_INDEX = 0,
61   PRIMARY_KEY_INDEX = 1,
62   PRIMARY_KEY_ORDERED_INDEX = 2,
63   UNIQUE_INDEX = 3,
64   UNIQUE_ORDERED_INDEX = 4,
65   ORDERED_INDEX = 5
66 } NDB_INDEX_TYPE;
67 
68 typedef enum ndb_index_status {
69   UNDEFINED = 0,
70   ACTIVE = 1,
71   TO_BE_DROPPED = 2
72 } NDB_INDEX_STATUS;
73 
74 typedef struct ndb_index_data {
75   NDB_INDEX_TYPE type;
76   NDB_INDEX_STATUS status;
77   const NdbDictionary::Index *index;
78   const NdbDictionary::Index *unique_index;
79   unsigned char *unique_index_attrid_map;
80   bool null_in_unique_index;
81   /*
82     In mysqld, keys and rows are stored differently (using KEY_PART_INFO for
83     keys and Field for rows).
84     So we need to use different NdbRecord for an index for passing values
85     from a key and from a row.
86   */
87   NdbRecord *ndb_record_key;
88   NdbRecord *ndb_unique_record_key;
89   NdbRecord *ndb_unique_record_row;
90 } NDB_INDEX_DATA;
91 
92 typedef enum ndb_write_op {
93   NDB_INSERT = 0,
94   NDB_UPDATE = 1,
95   NDB_PK_UPDATE = 2
96 } NDB_WRITE_OP;
97 
98 class NDB_ALTER_DATA : public Sql_alloc
99 {
100 public:
NDB_ALTER_DATA(NdbDictionary::Dictionary * dict,const NdbDictionary::Table * table)101   NDB_ALTER_DATA(NdbDictionary::Dictionary *dict,
102 		 const NdbDictionary::Table *table) :
103     dictionary(dict),
104     old_table(table),
105     new_table(new NdbDictionary::Table(*table)),
106       table_id(table->getObjectId()),
107       old_table_version(table->getObjectVersion())
108   {}
~NDB_ALTER_DATA()109   ~NDB_ALTER_DATA()
110   { delete new_table; }
111   NdbDictionary::Dictionary *dictionary;
112   const  NdbDictionary::Table *old_table;
113   NdbDictionary::Table *new_table;
114   Uint32 table_id;
115   Uint32 old_table_version;
116 };
117 
118 typedef union { const NdbRecAttr *rec; NdbBlob *blob; void *ptr; } NdbValue;
119 
120 int get_ndb_blobs_value(TABLE* table, NdbValue* value_array,
121                         uchar*& buffer, uint& buffer_size,
122                         my_ptrdiff_t ptrdiff);
123 
124 #include "ndb_share.h"
125 
126 struct Ndb_tuple_id_range_guard {
Ndb_tuple_id_range_guardNdb_tuple_id_range_guard127   Ndb_tuple_id_range_guard(NDB_SHARE* _share) :
128     share(_share),
129     range(share->tuple_id_range) {
130     pthread_mutex_lock(&share->mutex);
131   }
~Ndb_tuple_id_range_guardNdb_tuple_id_range_guard132   ~Ndb_tuple_id_range_guard() {
133     pthread_mutex_unlock(&share->mutex);
134   }
135   NDB_SHARE* share;
136   Ndb::TupleIdRange& range;
137 };
138 
139 /* NDB_SHARE.flags */
140 #define NSF_HIDDEN_PK   1u /* table has hidden primary key */
141 #define NSF_BLOB_FLAG   2u /* table has blob attributes */
142 #define NSF_NO_BINLOG   4u /* table should not be binlogged */
143 #define NSF_BINLOG_FULL 8u /* table should be binlogged with full rows */
144 #define NSF_BINLOG_USE_UPDATE 16u  /* table update should be binlogged using
145                                      update log event */
set_binlog_logging(NDB_SHARE * share)146 inline void set_binlog_logging(NDB_SHARE *share)
147 {
148   DBUG_PRINT("info", ("set_binlog_logging"));
149   share->flags&= ~NSF_NO_BINLOG;
150 }
set_binlog_nologging(NDB_SHARE * share)151 inline void set_binlog_nologging(NDB_SHARE *share)
152 {
153   DBUG_PRINT("info", ("set_binlog_nologging"));
154   share->flags|= NSF_NO_BINLOG;
155 }
get_binlog_nologging(NDB_SHARE * share)156 inline my_bool get_binlog_nologging(NDB_SHARE *share)
157 { return (share->flags & NSF_NO_BINLOG) != 0; }
set_binlog_updated_only(NDB_SHARE * share)158 inline void set_binlog_updated_only(NDB_SHARE *share)
159 {
160   DBUG_PRINT("info", ("set_binlog_updated_only"));
161   share->flags&= ~NSF_BINLOG_FULL;
162 }
set_binlog_full(NDB_SHARE * share)163 inline void set_binlog_full(NDB_SHARE *share)
164 {
165   DBUG_PRINT("info", ("set_binlog_full"));
166   share->flags|= NSF_BINLOG_FULL;
167 }
get_binlog_full(NDB_SHARE * share)168 inline my_bool get_binlog_full(NDB_SHARE *share)
169 { return (share->flags & NSF_BINLOG_FULL) != 0; }
set_binlog_use_write(NDB_SHARE * share)170 inline void set_binlog_use_write(NDB_SHARE *share)
171 {
172   DBUG_PRINT("info", ("set_binlog_use_write"));
173   share->flags&= ~NSF_BINLOG_USE_UPDATE;
174 }
set_binlog_use_update(NDB_SHARE * share)175 inline void set_binlog_use_update(NDB_SHARE *share)
176 {
177   DBUG_PRINT("info", ("set_binlog_use_update"));
178   share->flags|= NSF_BINLOG_USE_UPDATE;
179 }
get_binlog_use_update(NDB_SHARE * share)180 inline my_bool get_binlog_use_update(NDB_SHARE *share)
181 { return (share->flags & NSF_BINLOG_USE_UPDATE) != 0; }
182 
183 /*
184   State associated with the Slave thread
185   (From the Ndb handler's point of view)
186 */
187 struct st_ndb_slave_state
188 {
189   /* Counter values for current slave transaction */
190   Uint32 current_conflict_defined_op_count;
191   Uint32 current_violation_count[CFT_NUMBER_OF_CFTS];
192   Uint64 current_master_server_epoch;
193   Uint64 current_max_rep_epoch;
194 
195   /* Cumulative counter values */
196   Uint64 total_violation_count[CFT_NUMBER_OF_CFTS];
197   Uint64 max_rep_epoch;
198   Uint32 sql_run_id;
199 
200   /* Methods */
201   void atTransactionCommit();
202   void atTransactionAbort();
203   void atResetSlave();
204 
205   void atApplyStatusWrite(Uint32 master_server_id,
206                           Uint32 row_server_id,
207                           Uint64 row_epoch,
208                           bool is_row_server_id_local);
209 
210   st_ndb_slave_state();
211 };
212 
213 struct Ndb_local_table_statistics {
214   int no_uncommitted_rows_count;
215   ulong last_count;
216   ha_rows records;
217 };
218 
219 #include "ndb_thd_ndb.h"
220 
221 struct st_ndb_status {
st_ndb_statusst_ndb_status222   st_ndb_status() { memset(this, 0, sizeof(struct st_ndb_status)); }
223   long cluster_node_id;
224   const char * connected_host;
225   long connected_port;
226   long number_of_replicas;
227   long number_of_data_nodes;
228   long number_of_ready_data_nodes;
229   long connect_count;
230   long execute_count;
231   long scan_count;
232   long pruned_scan_count;
233   long schema_locks_count;
234   long sorted_scan_count;
235   long pushed_queries_defined;
236   long pushed_queries_dropped;
237   long pushed_queries_executed;
238   long pushed_reads;
239   long transaction_no_hint_count[MAX_NDB_NODES];
240   long transaction_hint_count[MAX_NDB_NODES];
241   long long api_client_stats[Ndb::NumClientStatistics];
242 };
243 
244 int ndbcluster_commit(handlerton *hton, THD *thd, bool all);
245 class ha_ndbcluster: public handler
246 {
247   friend class ndb_pushed_builder_ctx;
248 
249  public:
250   ha_ndbcluster(handlerton *hton, TABLE_SHARE *table);
251   ~ha_ndbcluster();
252 
253   int open(const char *name, int mode, uint test_if_locked);
254   int close(void);
255   void local_close(THD *thd, bool release_metadata);
256 
257   int optimize(THD* thd, HA_CHECK_OPT* check_opt);
258   int analyze(THD* thd, HA_CHECK_OPT* check_opt);
259   int analyze_index(THD* thd);
260 
261   int write_row(uchar *buf);
262   int update_row(const uchar *old_data, uchar *new_data);
263   int delete_row(const uchar *buf);
264   int index_init(uint index, bool sorted);
265   int index_end();
266   int index_read_idx_map(uchar *buf, uint index, const uchar *key,
267                          key_part_map keypart_map,
268                          enum ha_rkey_function find_flag);
269   int index_read(uchar *buf, const uchar *key, uint key_len,
270                  enum ha_rkey_function find_flag);
271   int index_next(uchar *buf);
272   int index_prev(uchar *buf);
273   int index_first(uchar *buf);
274   int index_last(uchar *buf);
275   int index_read_last(uchar * buf, const uchar * key, uint key_len);
276   int rnd_init(bool scan);
277   int rnd_end();
278   int rnd_next(uchar *buf);
279   int rnd_pos(uchar *buf, uchar *pos);
280   void position(const uchar *record);
281   int read_first_row(uchar *buf, uint primary_key);
282   virtual int cmp_ref(const uchar * ref1, const uchar * ref2);
283   int read_range_first(const key_range *start_key,
284                        const key_range *end_key,
285                        bool eq_range, bool sorted);
286   int read_range_first_to_buf(const key_range *start_key,
287                               const key_range *end_key,
288                               bool eq_range, bool sorted,
289                               uchar* buf);
290   int read_range_next();
291 
292 #ifndef NDB_WITH_NEW_MRR_INTERFACE
293   /**
294    * Multi range stuff
295    */
296   int read_multi_range_first(KEY_MULTI_RANGE **found_range_p,
297                              KEY_MULTI_RANGE*ranges, uint range_count,
298                              bool sorted, HANDLER_BUFFER *buffer);
299   int read_multi_range_next(KEY_MULTI_RANGE **found_range_p);
300   bool null_value_index_search(KEY_MULTI_RANGE *ranges,
301 			       KEY_MULTI_RANGE *end_range,
302 			       HANDLER_BUFFER *buffer);
303 #endif
304 
305   bool get_error_message(int error, String *buf);
306   ha_rows records();
estimate_rows_upper_bound()307   ha_rows estimate_rows_upper_bound()
308     { return HA_POS_ERROR; }
309   int info(uint);
310 #if MYSQL_VERSION_ID < 50501
311   typedef PARTITION_INFO PARTITION_STATS;
312 #endif
313   void get_dynamic_partition_info(PARTITION_STATS *stat_info, uint part_id);
314   uint32 calculate_key_hash_value(Field **field_array);
315   bool start_read_removal(void);
316   ha_rows end_read_removal(void);
317   int extra(enum ha_extra_function operation);
318   int extra_opt(enum ha_extra_function operation, ulong cache_size);
319   int reset();
320   int external_lock(THD *thd, int lock_type);
321   void unlock_row();
322   int start_stmt(THD *thd, thr_lock_type lock_type);
323   void update_create_info(HA_CREATE_INFO *create_info);
324   void print_error(int error, myf errflag);
325   const char * table_type() const;
326   const char ** bas_ext() const;
327   ulonglong table_flags(void) const;
328   void set_part_info(partition_info *part_info, bool early);
329   ulong index_flags(uint idx, uint part, bool all_parts) const;
keys_to_use_for_scanning()330   virtual const key_map *keys_to_use_for_scanning() { return &btree_keys; }
331   bool primary_key_is_clustered();
332   uint max_supported_record_length() const;
333   uint max_supported_keys() const;
334   uint max_supported_key_parts() const;
335   uint max_supported_key_length() const;
336   uint max_supported_key_part_length() const;
337 
338   int rename_table(const char *from, const char *to);
339   int delete_table(const char *name);
340   int create(const char *name, TABLE *form, HA_CREATE_INFO *info);
341   int get_default_no_partitions(HA_CREATE_INFO *info);
342   bool get_no_parts(const char *name, uint *no_parts);
343   void set_auto_partitions(partition_info *part_info);
is_fatal_error(int error,uint flags)344   virtual bool is_fatal_error(int error, uint flags)
345   {
346     if (!handler::is_fatal_error(error, flags) ||
347         error == HA_ERR_NO_PARTITION_FOUND)
348       return FALSE;
349     return TRUE;
350   }
351 
352   THR_LOCK_DATA **store_lock(THD *thd,
353                              THR_LOCK_DATA **to,
354                              enum thr_lock_type lock_type);
355 
356   bool low_byte_first() const;
357 
358   const char* index_type(uint key_number);
359 
360   double scan_time();
361   ha_rows records_in_range(uint inx, key_range *min_key, key_range *max_key);
362   void start_bulk_insert(ha_rows rows);
363   int end_bulk_insert();
364 
365   bool start_bulk_update();
366   int bulk_update_row(const uchar *old_data, uchar *new_data,
367                       uint *dup_key_found);
368   int exec_bulk_update(uint *dup_key_found);
369   void end_bulk_update();
370   int ndb_update_row(const uchar *old_data, uchar *new_data,
371                      int is_bulk_update);
372 
373 static void set_dbname(const char *pathname, char *dbname);
374 static void set_tabname(const char *pathname, char *tabname);
375 
376   /*
377     static member function as it needs to access private
378     NdbTransaction methods
379   */
380   static void release_completed_operations(NdbTransaction*);
381 
382   /*
383     Condition pushdown
384   */
385 
386  /*
387    Push condition down to the table handler.
388    SYNOPSIS
389      cond_push()
390      cond   Condition to be pushed. The condition tree must not be
391      modified by the by the caller.
392    RETURN
393      The 'remainder' condition that caller must use to filter out records.
394      NULL means the handler will not return rows that do not match the
395      passed condition.
396    NOTES
397    The pushed conditions form a stack (from which one can remove the
398    last pushed condition using cond_pop).
399    The table handler filters out rows using (pushed_cond1 AND pushed_cond2
400    AND ... AND pushed_condN)
401    or less restrictive condition, depending on handler's capabilities.
402 
403    handler->reset() call empties the condition stack.
404    Calls to rnd_init/rnd_end, index_init/index_end etc do not affect the
405    condition stack.
406    The current implementation supports arbitrary AND/OR nested conditions
407    with comparisons between columns and constants (including constant
408    expressions and function calls) and the following comparison operators:
409    =, !=, >, >=, <, <=, like, "not like", "is null", and "is not null".
410    Negated conditions are supported by NOT which generate NAND/NOR groups.
411  */
412   const Item *cond_push(const Item *cond);
413  /*
414    Pop the top condition from the condition stack of the handler instance.
415    SYNOPSIS
416      cond_pop()
417      Pops the top if condition stack, if stack is not empty
418  */
419   void cond_pop();
420 
421   bool maybe_pushable_join(const char*& reason) const;
422   int assign_pushed_join(const ndb_pushed_join* pushed_join);
423 
424   uint number_of_pushed_joins() const;
425   const TABLE* root_of_pushed_join() const;
426   const TABLE* parent_of_pushed_join() const;
427 
428   int index_read_pushed(uchar *buf, const uchar *key,
429                         key_part_map keypart_map);
430 
431   int index_next_pushed(uchar * buf);
432 
433   uint8 table_cache_type();
434 
435   /*
436    * Internal to ha_ndbcluster, used by C functions
437    */
438   int ndb_err(NdbTransaction*, bool have_lock= FALSE);
439 
440   my_bool register_query_cache_table(THD *thd, char *table_key,
441                                      uint key_length,
442                                      qc_engine_callback *engine_callback,
443                                      ulonglong *engine_data);
444 
445 #ifndef NDB_WITHOUT_ONLINE_ALTER
446   int check_if_supported_alter(TABLE *altered_table,
447                                HA_CREATE_INFO *create_info,
448                                Alter_info *alter_info,
449                                HA_ALTER_FLAGS *alter_flags,
450                                uint table_changes);
451 
452   int alter_table_phase1(THD *thd,
453                          TABLE *altered_table,
454                          HA_CREATE_INFO *create_info,
455                          HA_ALTER_INFO *alter_info,
456                          HA_ALTER_FLAGS *alter_flags);
457 
458   int alter_table_phase2(THD *thd,
459                          TABLE *altered_table,
460                          HA_CREATE_INFO *create_info,
461                          HA_ALTER_INFO *alter_info,
462                          HA_ALTER_FLAGS *alter_flags);
463 
464   int alter_table_phase3(THD *thd, TABLE *table,
465                          HA_CREATE_INFO *create_info,
466                          HA_ALTER_INFO *alter_info,
467                          HA_ALTER_FLAGS *alter_flags);
468 #endif
469 
470 private:
471 #ifdef HAVE_NDB_BINLOG
472   int prepare_conflict_detection(enum_conflicting_op_type op_type,
473                                  const NdbRecord* key_rec,
474                                  const uchar* old_data,
475                                  const uchar* new_data,
476                                  NdbInterpretedCode* code,
477                                  NdbOperation::OperationOptions* options);
478 #endif
479   void setup_key_ref_for_ndb_record(const NdbRecord **key_rec,
480                                     const uchar **key_row,
481                                     const uchar *record,
482                                     bool use_active_index);
483   friend int ndbcluster_drop_database_impl(THD *thd, const char *path);
484   friend int ndb_handle_schema_change(THD *thd,
485                                       Ndb *ndb, NdbEventOperation *pOp,
486                                       NDB_SHARE *share);
487 
488   void check_read_before_write_removal();
489   static int drop_table_impl(THD *thd, ha_ndbcluster *h, Ndb *ndb,
490                              const char *path,
491                              const char *db,
492                              const char *table_name);
493 
494   int add_index_impl(THD *thd, TABLE *table_arg,
495                      KEY *key_info, uint num_of_keys);
496   int create_ndb_index(THD *thd, const char *name, KEY *key_info, bool unique);
497   int create_ordered_index(THD *thd, const char *name, KEY *key_info);
498   int create_unique_index(THD *thd, const char *name, KEY *key_info);
499   int create_index(THD *thd, const char *name, KEY *key_info,
500                    NDB_INDEX_TYPE idx_type, uint idx_no);
501 // Index list management
502   int create_indexes(THD *thd, Ndb *ndb, TABLE *tab);
503   int open_indexes(THD *thd, Ndb *ndb, TABLE *tab, bool ignore_error);
504   void renumber_indexes(Ndb *ndb, TABLE *tab);
505   int drop_indexes(Ndb *ndb, TABLE *tab);
506   int add_index_handle(THD *thd, NdbDictionary::Dictionary *dict,
507                        KEY *key_info, const char *key_name, uint index_no);
508   int add_table_ndb_record(NdbDictionary::Dictionary *dict);
509   int add_hidden_pk_ndb_record(NdbDictionary::Dictionary *dict);
510   int add_index_ndb_record(NdbDictionary::Dictionary *dict,
511                            KEY *key_info, uint index_no);
512   int check_default_values(const NdbDictionary::Table* ndbtab);
513   int get_metadata(THD *thd, const char* path);
514   void release_metadata(THD *thd, Ndb *ndb);
515   NDB_INDEX_TYPE get_index_type(uint idx_no) const;
516   NDB_INDEX_TYPE get_index_type_from_table(uint index_no) const;
517   NDB_INDEX_TYPE get_index_type_from_key(uint index_no, KEY *key_info,
518                                          bool primary) const;
519   bool has_null_in_unique_index(uint idx_no) const;
520   bool check_index_fields_not_null(KEY *key_info);
521 
522   bool check_if_pushable(int type, //NdbQueryOperationDef::Type,
523                          uint idx= MAX_KEY,
524                          bool rootSorted= false) const;
525   bool check_is_pushed() const;
526   int create_pushed_join(const NdbQueryParamValue* keyFieldParams=NULL,
527                          uint paramCnt= 0);
528 
529   int set_up_partition_info(partition_info *part_info,
530                             NdbDictionary::Table&) const;
531   int set_range_data(const partition_info* part_info,
532                      NdbDictionary::Table&) const;
533   int set_list_data(const partition_info* part_info,
534                     NdbDictionary::Table&) const;
535   int ndb_pk_update_row(THD *thd,
536                         const uchar *old_data, uchar *new_data,
537                         uint32 old_part_id);
538   int pk_read(const uchar *key, uint key_len, uchar *buf, uint32 *part_id);
539   int ordered_index_scan(const key_range *start_key,
540                          const key_range *end_key,
541                          bool sorted, bool descending, uchar* buf,
542                          part_id_range *part_spec);
543   int unique_index_read(const uchar *key, uint key_len,
544                         uchar *buf);
545   int full_table_scan(const KEY* key_info,
546                       const key_range *start_key,
547                       const key_range *end_key,
548                       uchar *buf);
549   int flush_bulk_insert(bool allow_batch= FALSE);
550   int ndb_write_row(uchar *record, bool primary_key_update,
551                     bool batched_update);
552 
553   bool start_bulk_delete();
554   int end_bulk_delete();
555   int ndb_delete_row(const uchar *record, bool primary_key_update);
556 
557   int ndb_optimize_table(THD* thd, uint delay);
558 
559 #ifndef NDB_WITHOUT_ONLINE_ALTER
560   int alter_frm(THD *thd, const char *file, NDB_ALTER_DATA *alter_data);
561 #endif
562 
563   bool check_all_operations_for_error(NdbTransaction *trans,
564                                       const NdbOperation *first,
565                                       const NdbOperation *last,
566                                       uint errcode);
567   int peek_indexed_rows(const uchar *record, NDB_WRITE_OP write_op);
568   int scan_handle_lock_tuple(NdbScanOperation *scanOp, NdbTransaction *trans);
569   int fetch_next(NdbScanOperation* op);
570   int fetch_next_pushed();
571   int set_auto_inc(THD *thd, Field *field);
572   int set_auto_inc_val(THD *thd, Uint64 value);
573   int next_result(uchar *buf);
574   int close_scan();
575   void unpack_record(uchar *dst_row, const uchar *src_row);
576 
577   void set_dbname(const char *pathname);
578   void set_tabname(const char *pathname);
579 
get_hidden_key_column()580   const NdbDictionary::Column *get_hidden_key_column() {
581     return m_table->getColumn(table_share->fields);
582   }
get_partition_id_column()583   const NdbDictionary::Column *get_partition_id_column() {
584     Uint32 index= table_share->fields + (table_share->primary_key == MAX_KEY);
585     return m_table->getColumn(index);
586   }
587 
588   bool add_row_check_if_batch_full_size(Thd_ndb *thd_ndb, uint size);
add_row_check_if_batch_full(Thd_ndb * thd_ndb)589   bool add_row_check_if_batch_full(Thd_ndb *thd_ndb) {
590     return add_row_check_if_batch_full_size(thd_ndb, m_bytes_per_write);
591   }
592   uchar *get_buffer(Thd_ndb *thd_ndb, uint size);
593   uchar *copy_row_to_buffer(Thd_ndb *thd_ndb, const uchar *record);
594 
595   int get_blob_values(const NdbOperation *ndb_op, uchar *dst_record,
596                       const MY_BITMAP *bitmap);
597   int set_blob_values(const NdbOperation *ndb_op, my_ptrdiff_t row_offset,
598                       const MY_BITMAP *bitmap, uint *set_count, bool batch);
599   friend int g_get_ndb_blobs_value(NdbBlob *ndb_blob, void *arg);
600   void release_blobs_buffer();
601   Uint32 setup_get_hidden_fields(NdbOperation::GetValueSpec gets[2]);
602   void get_hidden_fields_keyop(NdbOperation::OperationOptions *options,
603                                NdbOperation::GetValueSpec gets[2]);
604   void get_hidden_fields_scan(NdbScanOperation::ScanOptions *options,
605                               NdbOperation::GetValueSpec gets[2]);
606   void eventSetAnyValue(THD *thd,
607                         NdbOperation::OperationOptions *options) const;
608   bool check_index_fields_in_write_set(uint keyno);
609 
610   const NdbOperation *pk_unique_index_read_key(uint idx,
611                                                const uchar *key, uchar *buf,
612                                                NdbOperation::LockMode lm,
613                                                Uint32 *ppartition_id);
614   int pk_unique_index_read_key_pushed(uint idx,
615                                       const uchar *key,
616                                       Uint32 *ppartition_id);
617 
618   int read_multi_range_fetch_next();
619 
620   int primary_key_cmp(const uchar * old_row, const uchar * new_row);
621   void print_results();
622 
623   virtual void get_auto_increment(ulonglong offset, ulonglong increment,
624                                   ulonglong nb_desired_values,
625                                   ulonglong *first_value,
626                                   ulonglong *nb_reserved_values);
627   bool uses_blob_value(const MY_BITMAP *bitmap) const;
628 
629   static inline bool isManualBinlogExec(THD *thd);
630 
631   char *update_table_comment(const char * comment);
632 
633   int write_ndb_file(const char *name);
634 
635   int check_ndb_connection(THD* thd);
636 
637   void set_rec_per_key();
638   int records_update();
639   void no_uncommitted_rows_execute_failure();
640   void no_uncommitted_rows_update(int);
641   void no_uncommitted_rows_reset(THD *);
642 
643   /* Ordered index statistics v4 */
644   int ndb_index_stat_query(uint inx,
645                            const key_range *min_key,
646                            const key_range *max_key,
647                            NdbIndexStat::Stat& stat,
648                            int from);
649   int ndb_index_stat_get_rir(uint inx,
650                              key_range *min_key,
651                              key_range *max_key,
652                              ha_rows *rows_out);
653   int ndb_index_stat_set_rpk(uint inx);
654   int ndb_index_stat_analyze(Ndb *ndb,
655                              uint *inx_list,
656                              uint inx_count);
657 
658   NdbTransaction *start_transaction_part_id(uint32 part_id, int &error);
get_transaction_part_id(uint32 part_id,int & error)659   inline NdbTransaction *get_transaction_part_id(uint32 part_id, int &error)
660   {
661     if (m_thd_ndb->trans)
662       return m_thd_ndb->trans;
663     return start_transaction_part_id(part_id, error);
664   }
665 
666   NdbTransaction *start_transaction(int &error);
get_transaction(int & error)667   inline NdbTransaction *get_transaction(int &error)
668   {
669     if (m_thd_ndb->trans)
670       return m_thd_ndb->trans;
671     return start_transaction(error);
672   }
673 
674   NdbTransaction *start_transaction_row(const NdbRecord *ndb_record,
675                                         const uchar *record,
676                                         int &error);
677   NdbTransaction *start_transaction_key(uint index,
678                                         const uchar *key_data,
679                                         int &error);
680 
681   friend int check_completed_operations_pre_commit(Thd_ndb*,
682                                                    NdbTransaction*,
683                                                    const NdbOperation*,
684                                                    uint *ignore_count);
685   friend int ndbcluster_commit(handlerton *hton, THD *thd, bool all);
686   int start_statement(THD *thd, Thd_ndb *thd_ndb, uint table_count);
687   int init_handler_for_statement(THD *thd);
688 
689   Thd_ndb *m_thd_ndb;
690   NdbScanOperation *m_active_cursor;
691   const NdbDictionary::Table *m_table;
692   /*
693     Normal NdbRecord for accessing rows, with all fields including hidden
694     fields (hidden primary key, user-defined partitioning function value).
695   */
696   NdbRecord *m_ndb_record;
697   /* NdbRecord for accessing tuple by hidden Uint64 primary key. */
698   NdbRecord *m_ndb_hidden_key_record;
699 
700   /* Bitmap used for NdbRecord operation column mask. */
701   MY_BITMAP m_bitmap;
702   my_bitmap_map m_bitmap_buf[(NDB_MAX_ATTRIBUTES_IN_TABLE +
703                               8*sizeof(my_bitmap_map) - 1) /
704                              (8*sizeof(my_bitmap_map))]; // Buffer for m_bitmap
705   /* Bitmap with bit set for all primary key columns. */
706   MY_BITMAP *m_pk_bitmap_p;
707   my_bitmap_map m_pk_bitmap_buf[(NDB_MAX_ATTRIBUTES_IN_TABLE +
708                                  8*sizeof(my_bitmap_map) - 1) /
709                                 (8*sizeof(my_bitmap_map))]; // Buffer for m_pk_bitmap
710   struct Ndb_local_table_statistics *m_table_info;
711   struct Ndb_local_table_statistics m_table_info_instance;
712   char m_dbname[FN_HEADLEN];
713   //char m_schemaname[FN_HEADLEN];
714   char m_tabname[FN_HEADLEN];
715   THR_LOCK_DATA m_lock;
716   bool m_lock_tuple;
717   NDB_SHARE *m_share;
718   NDB_INDEX_DATA  m_index[MAX_KEY];
719   key_map btree_keys;
720 
721   /*
722     Pointer to row returned from scan nextResult().
723   */
724   union
725   {
726     const char *_m_next_row;
727     const uchar *m_next_row;
728   };
729   /* For read_multi_range scans, the get_range_no() of current row. */
730   int m_current_range_no;
731 
732   MY_BITMAP **m_key_fields;
733   // NdbRecAttr has no reference to blob
734   NdbValue m_value[NDB_MAX_ATTRIBUTES_IN_TABLE];
735   Uint64 m_ref;
736   partition_info *m_part_info;
737   uint32 m_part_id;
738   bool m_user_defined_partitioning;
739   bool m_use_partition_pruning;
740   bool m_sorted;
741   bool m_use_write;
742   bool m_ignore_dup_key;
743   bool m_has_unique_index;
744   bool m_ignore_no_key;
745   bool m_read_before_write_removal_possible;
746   bool m_read_before_write_removal_used;
747   ha_rows m_rows_updated;
748   ha_rows m_rows_deleted;
749   ha_rows m_rows_to_insert; // TODO: merge it with handler::estimation_rows_to_insert?
750   ha_rows m_rows_inserted;
751   ha_rows m_rows_changed;
752   bool m_delete_cannot_batch;
753   bool m_update_cannot_batch;
754   uint m_bytes_per_write;
755   bool m_skip_auto_increment;
756   bool m_blobs_pending;
757   bool m_slow_path;
758   bool m_is_bulk_delete;
759 
760   /* State for setActiveHook() callback for reading blob data. */
761   uint m_blob_counter;
762   uint m_blob_expected_count_per_row;
763   uchar *m_blob_destination_record;
764   Uint64 m_blobs_row_total_size; /* Bytes needed for all blobs in current row */
765 
766   // memory for blobs in one tuple
767   uchar *m_blobs_buffer;
768   Uint64 m_blobs_buffer_size;
769   uint m_dupkey;
770   // set from thread variables at external lock
771   ha_rows m_autoincrement_prefetch;
772 
773   // Joins pushed to NDB.
774   const ndb_pushed_join
775        *m_pushed_join_member;            // Pushed join def. I am member of
776   int m_pushed_join_operation;           // Op. id. in above pushed join
777   static const int PUSHED_ROOT= 0;       // Op. id. if I'm root
778 
779   bool m_disable_pushed_join;            // Pushed execution allowed?
780   NdbQuery* m_active_query;              // Pushed query instance executing
781   NdbQueryOperation* m_pushed_operation; // Pushed operation instance
782 
783   ha_ndbcluster_cond *m_cond;
784   bool m_disable_multi_read;
785   const uchar *m_multi_range_result_ptr;
786   KEY_MULTI_RANGE *m_multi_ranges;
787   /*
788     Points 1 past the end of last multi range operation currently being
789     executed, to support splitting large multi range reands into manageable
790     pieces.
791   */
792   KEY_MULTI_RANGE *m_multi_range_defined_end;
793   NdbIndexScanOperation *m_multi_cursor;
794   Ndb *get_ndb(THD *thd);
795 
796   int update_stats(THD *thd, bool do_read_stat, bool have_lock= FALSE,
797                    uint part_id= ~(uint)0);
798   int add_handler_to_open_tables(THD*, Thd_ndb*, ha_ndbcluster* handler);
799 };
800 
801 int ndbcluster_discover(THD* thd, const char* dbname, const char* name,
802                         const void** frmblob, uint* frmlen);
803 int ndbcluster_table_exists_in_engine(THD* thd,
804                                       const char *db, const char *name);
805 void ndbcluster_print_error(int error, const NdbOperation *error_op);
806 
807 static const char ndbcluster_hton_name[]= "ndbcluster";
808 static const int ndbcluster_hton_name_length=sizeof(ndbcluster_hton_name)-1;
809 extern int ndbcluster_terminating;
810 extern int ndb_util_thread_running;
811 extern pthread_cond_t COND_ndb_util_ready;
812 extern int ndb_index_stat_thread_running;
813 extern pthread_cond_t COND_ndb_index_stat_ready;
814