ha_ndbcluster.cc 194 KB
Newer Older
1
/* Copyright (C) 2000-2003 MySQL AB
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
5
  the Free Software Foundation; version 2 of the License.
6 7 8 9 10 11 12 13

  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
14
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15 16 17 18 19 20 21
*/

/*
  This file defines the NDB Cluster handler: the interface between MySQL and
  NDB Cluster
*/

22
#ifdef USE_PRAGMA_IMPLEMENTATION
23
#pragma implementation				// gcc: Class implementation
24 25 26 27 28 29 30 31
#endif

#include "mysql_priv.h"

#ifdef HAVE_NDBCLUSTER_DB
#include <my_dir.h>
#include "ha_ndbcluster.h"
#include <ndbapi/NdbApi.hpp>
32
#include "ha_ndbcluster_cond.h"
33

34 35 36
// options from from mysqld.cc
extern my_bool opt_ndb_optimized_node_selection;
extern const char *opt_ndbcluster_connectstring;
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
37
extern ulong opt_ndb_cache_check_time;
38

39
// Default value for parallelism
40
static const int parallelism= 0;
41

42 43
// Default value for max number of transactions
// createable against NDB from this handler
44
static const int max_transactions= 2;
45

46 47
static const char *ha_ndb_ext=".ndb";

48 49 50 51
static int ndbcluster_close_connection(THD *thd);
static int ndbcluster_commit(THD *thd, bool all);
static int ndbcluster_rollback(THD *thd, bool all);

52
handlerton ndbcluster_hton = {
serg@serg.mylan's avatar
serg@serg.mylan committed
53
  "ndbcluster",
54 55 56 57
  SHOW_OPTION_YES,
  "Clustered, fault-tolerant, memory-based tables", 
  DB_TYPE_NDBCLUSTER,
  ndbcluster_init,
58 59 60 61 62 63 64 65 66
  0, /* slot */
  0, /* savepoint size */
  ndbcluster_close_connection,
  NULL, /* savepoint_set */
  NULL, /* savepoint_rollback */
  NULL, /* savepoint_release */
  ndbcluster_commit,
  ndbcluster_rollback,
  NULL, /* prepare */
67 68
  NULL, /* recover */
  NULL, /* commit_by_xid */
69
  NULL, /* rollback_by_xid */
70 71 72
  NULL, /* create_cursor_read_view */
  NULL, /* set_cursor_read_view */
  NULL, /* close_cursor_read_view */
73
  HTON_CAN_RECREATE
74 75
};

76
#define NDB_AUTO_INCREMENT_RETRIES 10
77

78 79
#define NDB_INVALID_SCHEMA_OBJECT 241

80
#define ERR_PRINT(err) \
81
  DBUG_PRINT("error", ("%d  message: %s", err.code, err.message))
82

83 84
#define ERR_RETURN(err)                  \
{                                        \
85
  const NdbError& tmp= err;              \
86
  ERR_PRINT(tmp);                        \
87
  DBUG_RETURN(ndb_to_mysql_error(&tmp)); \
88 89 90 91
}

// Typedefs for long names
typedef NdbDictionary::Column NDBCOL;
joreland@mysql.com's avatar
joreland@mysql.com committed
92
typedef NdbDictionary::Table NDBTAB;
93 94 95
typedef NdbDictionary::Index  NDBINDEX;
typedef NdbDictionary::Dictionary  NDBDICT;

96
bool ndbcluster_inited= FALSE;
97

98
static Ndb* g_ndb= NULL;
99
static Ndb_cluster_connection* g_ndb_cluster_connection= NULL;
100

101 102 103 104 105 106 107 108 109 110 111 112 113
// Handler synchronization
pthread_mutex_t ndbcluster_mutex;

// Table lock handling
static HASH ndbcluster_open_tables;

static byte *ndbcluster_get_key(NDB_SHARE *share,uint *length,
                                my_bool not_used __attribute__((unused)));
static NDB_SHARE *get_share(const char *table_name);
static void free_share(NDB_SHARE *share);

static int packfrm(const void *data, uint len, const void **pack_data, uint *pack_len);
static int unpackfrm(const void **data, uint *len,
114
                     const void* pack_data);
115

116
static int ndb_get_table_statistics(ha_ndbcluster*, bool, Ndb*, const char *,
117
                                    struct Ndb_statistics *);
118

mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
119 120 121 122
// Util thread variables
static pthread_t ndb_util_thread;
pthread_mutex_t LOCK_ndb_util_thread;
pthread_cond_t COND_ndb_util_thread;
123
pthread_handler_t ndb_util_thread_func(void *arg);
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
124
ulong ndb_cache_check_time;
125

126 127 128 129
/*
  Dummy buffer to read zero pack_length fields
  which are mapped to 1 char
*/
130
static uint32 dummy_buf;
131

132 133 134 135 136 137 138 139 140 141 142
/*
  Stats that can be retrieved from ndb
*/

struct Ndb_statistics {
  Uint64 row_count;
  Uint64 commit_count;
  Uint64 row_size;
  Uint64 fragment_memory;
};

143 144 145 146 147 148
/* Status variables shown with 'show status like 'Ndb%' */

static long ndb_cluster_node_id= 0;
static const char * ndb_connected_host= 0;
static long ndb_connected_port= 0;
static long ndb_number_of_replicas= 0;
149
static long ndb_number_of_data_nodes= 0;
150 151 152 153 154 155 156

static int update_status_variables(Ndb_cluster_connection *c)
{
  ndb_cluster_node_id=         c->node_id();
  ndb_connected_port=          c->get_connected_port();
  ndb_connected_host=          c->get_connected_host();
  ndb_number_of_replicas=      0;
157
  ndb_number_of_data_nodes= c->no_db_nodes();
158 159 160 161 162
  return 0;
}

struct show_var_st ndb_status_variables[]= {
  {"cluster_node_id",        (char*) &ndb_cluster_node_id,         SHOW_LONG},
163 164
  {"config_from_host",         (char*) &ndb_connected_host,      SHOW_CHAR_PTR},
  {"config_from_port",         (char*) &ndb_connected_port,          SHOW_LONG},
165
//  {"number_of_replicas",     (char*) &ndb_number_of_replicas,      SHOW_LONG},
166
  {"number_of_data_nodes",(char*) &ndb_number_of_data_nodes, SHOW_LONG},
167 168 169
  {NullS, NullS, SHOW_LONG}
};

170 171 172 173 174 175 176 177
/*
  Error handling functions
*/

struct err_code_mapping
{
  int ndb_err;
  int my_err;
178
  int show_warning;
179 180 181 182
};

static const err_code_mapping err_map[]= 
{
183 184
  { 626, HA_ERR_KEY_NOT_FOUND, 0 },
  { 630, HA_ERR_FOUND_DUPP_KEY, 0 },
185
  { 893, HA_ERR_FOUND_DUPP_KEY, 0 },
186 187 188
  { 721, HA_ERR_TABLE_EXIST, 1 },
  { 4244, HA_ERR_TABLE_EXIST, 1 },

189
  { 709, HA_ERR_NO_SUCH_TABLE, 0 },
190 191 192 193 194 195 196 197 198 199 200 201 202 203

  { 266, HA_ERR_LOCK_WAIT_TIMEOUT, 1 },
  { 274, HA_ERR_LOCK_WAIT_TIMEOUT, 1 },
  { 296, HA_ERR_LOCK_WAIT_TIMEOUT, 1 },
  { 297, HA_ERR_LOCK_WAIT_TIMEOUT, 1 },
  { 237, HA_ERR_LOCK_WAIT_TIMEOUT, 1 },

  { 623, HA_ERR_RECORD_FILE_FULL, 1 },
  { 624, HA_ERR_RECORD_FILE_FULL, 1 },
  { 625, HA_ERR_RECORD_FILE_FULL, 1 },
  { 826, HA_ERR_RECORD_FILE_FULL, 1 },
  { 827, HA_ERR_RECORD_FILE_FULL, 1 },
  { 832, HA_ERR_RECORD_FILE_FULL, 1 },

204 205
  { 284, HA_ERR_TABLE_DEF_CHANGED, 0 },

206
  {4000, HA_ERR_OUT_OF_MEM, 1 },
207 208
  {4009, HA_ERR_NO_CONNECTION, 1 },

209 210 211
  { 0, 1, 0 },

  { -1, -1, 1 }
212 213 214 215 216 217
};


static int ndb_to_mysql_error(const NdbError *err)
{
  uint i;
218 219
  for (i=0; err_map[i].ndb_err != err->code && err_map[i].my_err != -1; i++);
  if (err_map[i].show_warning)
220
  {
221 222
    // Push the NDB error message as warning
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
223 224
                        ER_GET_ERRMSG, ER(ER_GET_ERRMSG),
                        err->code, err->message, "NDB");
225
  }
226 227
  if (err_map[i].my_err == -1)
    return err->code;
228 229 230 231
  return err_map[i].my_err;
}


232 233

inline
234 235
int execute_no_commit(ha_ndbcluster *h, NdbTransaction *trans,
		      bool force_release)
236
{
237
#ifdef NOT_USED
238
  int m_batch_execute= 0;
239
  if (m_batch_execute)
240
    return 0;
241
#endif
242
  h->release_completed_operations(trans, force_release);
243
  return trans->execute(NdbTransaction::NoCommit,
244 245
                        NdbTransaction::AbortOnError,
                        h->m_force_send);
246 247 248
}

inline
249
int execute_commit(ha_ndbcluster *h, NdbTransaction *trans)
250
{
251
#ifdef NOT_USED
252
  int m_batch_execute= 0;
253
  if (m_batch_execute)
254
    return 0;
255
#endif
256
  return trans->execute(NdbTransaction::Commit,
257 258
                        NdbTransaction::AbortOnError,
                        h->m_force_send);
259 260 261
}

inline
262
int execute_commit(THD *thd, NdbTransaction *trans)
263 264
{
#ifdef NOT_USED
265
  int m_batch_execute= 0;
266 267 268
  if (m_batch_execute)
    return 0;
#endif
269
  return trans->execute(NdbTransaction::Commit,
270 271
                        NdbTransaction::AbortOnError,
                        thd->variables.ndb_force_send);
272 273 274
}

inline
275 276
int execute_no_commit_ie(ha_ndbcluster *h, NdbTransaction *trans,
			 bool force_release)
277
{
278
#ifdef NOT_USED
279
  int m_batch_execute= 0;
280
  if (m_batch_execute)
281
    return 0;
282
#endif
283
  h->release_completed_operations(trans, force_release);
284
  return trans->execute(NdbTransaction::NoCommit,
285 286
                        NdbTransaction::AO_IgnoreError,
                        h->m_force_send);
287 288
}

289 290 291
/*
  Place holder for ha_ndbcluster thread specific data
*/
292 293
Thd_ndb::Thd_ndb()
{
294
  ndb= new Ndb(g_ndb_cluster_connection, "");
295 296
  lock_count= 0;
  count= 0;
297 298
  all= NULL;
  stmt= NULL;
299
  error= 0;
300
  query_state&= NDB_QUERY_NORMAL;
301 302 303 304
}

Thd_ndb::~Thd_ndb()
{
305
  if (ndb)
306 307
  {
#ifndef DBUG_OFF
308 309
    Ndb::Free_list_usage tmp;
    tmp.m_name= 0;
310 311 312 313 314 315 316 317 318 319
    while (ndb->get_free_list_usage(&tmp))
    {
      uint leaked= (uint) tmp.m_created - tmp.m_free;
      if (leaked)
        fprintf(stderr, "NDB: Found %u %s%s that %s not been released\n",
                leaked, tmp.m_name,
                (leaked == 1)?"":"'s",
                (leaked == 1)?"has":"have");
    }
#endif
320
    delete ndb;
321
    ndb= NULL;
322
  }
323
  changed_tables.empty();
324 325
}

326 327 328 329 330 331 332 333
inline
Thd_ndb *
get_thd_ndb(THD *thd) { return (Thd_ndb *) thd->ha_data[ndbcluster_hton.slot]; }

inline
void
set_thd_ndb(THD *thd, Thd_ndb *thd_ndb) { thd->ha_data[ndbcluster_hton.slot]= thd_ndb; }

334 335 336
inline
Ndb *ha_ndbcluster::get_ndb()
{
337
  return get_thd_ndb(current_thd)->ndb;
338 339 340 341 342 343
}

/*
 * manage uncommitted insert/deletes during transactio to get records correct
 */

344
struct Ndb_local_table_statistics {
345
  int no_uncommitted_rows_count;
346
  ulong last_count;
347 348 349
  ha_rows records;
};

tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
350 351 352
void ha_ndbcluster::set_rec_per_key()
{
  DBUG_ENTER("ha_ndbcluster::get_status_const");
353
  for (uint i=0 ; i < table->s->keys ; i++)
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
354 355 356 357 358 359
  {
    table->key_info[i].rec_per_key[table->key_info[i].key_parts-1]= 1;
  }
  DBUG_VOID_RETURN;
}

360
int ha_ndbcluster::records_update()
361
{
362
  if (m_ha_not_exact_count)
363
    return 0;
364
  DBUG_ENTER("ha_ndbcluster::records_update");
365 366
  int result= 0;

367
  struct Ndb_local_table_statistics *local_info= 
368
    (struct Ndb_local_table_statistics *)m_table_info;
369
  DBUG_PRINT("info", ("id=%d, no_uncommitted_rows_count=%d",
370
                      ((const NDBTAB *)m_table)->getTableId(),
371
                      local_info->no_uncommitted_rows_count));
372
  //  if (info->records == ~(ha_rows)0)
373
  {
374
    Ndb *ndb= get_ndb();
375
    struct Ndb_statistics stat;
376 377 378 379
    if (ndb->setDatabaseName(m_dbname))
    {
      return my_errno= HA_ERR_OUT_OF_MEM;
    }
stewart@willster.(none)'s avatar
stewart@willster.(none) committed
380 381 382
    result= ndb_get_table_statistics(this, true, ndb, m_tabname, &stat);
    if (result == 0)
    {
383 384
      mean_rec_length= stat.row_size;
      data_file_length= stat.fragment_memory;
385
      local_info->records= stat.row_count;
386 387
    }
  }
388 389
  {
    THD *thd= current_thd;
390
    if (get_thd_ndb(thd)->error)
391
      local_info->no_uncommitted_rows_count= 0;
392
  }
393
  if(result==0)
394
    records= local_info->records+ local_info->no_uncommitted_rows_count;
395
  DBUG_RETURN(result);
396 397
}

398 399
void ha_ndbcluster::no_uncommitted_rows_execute_failure()
{
400 401
  if (m_ha_not_exact_count)
    return;
402
  DBUG_ENTER("ha_ndbcluster::no_uncommitted_rows_execute_failure");
403
  get_thd_ndb(current_thd)->error= 1;
404 405 406
  DBUG_VOID_RETURN;
}

407 408
void ha_ndbcluster::no_uncommitted_rows_init(THD *thd)
{
409 410
  if (m_ha_not_exact_count)
    return;
411
  DBUG_ENTER("ha_ndbcluster::no_uncommitted_rows_init");
412
  struct Ndb_local_table_statistics *local_info= 
413
    (struct Ndb_local_table_statistics *)m_table_info;
414
  Thd_ndb *thd_ndb= get_thd_ndb(thd);
415
  if (local_info->last_count != thd_ndb->count)
416
  {
417 418 419
    local_info->last_count= thd_ndb->count;
    local_info->no_uncommitted_rows_count= 0;
    local_info->records= ~(ha_rows)0;
420
    DBUG_PRINT("info", ("id=%d, no_uncommitted_rows_count=%d",
421
                        ((const NDBTAB *)m_table)->getTableId(),
422
                        local_info->no_uncommitted_rows_count));
423 424 425 426 427 428
  }
  DBUG_VOID_RETURN;
}

void ha_ndbcluster::no_uncommitted_rows_update(int c)
{
429 430
  if (m_ha_not_exact_count)
    return;
431
  DBUG_ENTER("ha_ndbcluster::no_uncommitted_rows_update");
432
  struct Ndb_local_table_statistics *local_info=
433
    (struct Ndb_local_table_statistics *)m_table_info;
434
  local_info->no_uncommitted_rows_count+= c;
435
  DBUG_PRINT("info", ("id=%d, no_uncommitted_rows_count=%d",
436
                      ((const NDBTAB *)m_table)->getTableId(),
437
                      local_info->no_uncommitted_rows_count));
438 439 440 441 442
  DBUG_VOID_RETURN;
}

void ha_ndbcluster::no_uncommitted_rows_reset(THD *thd)
{
443 444
  if (m_ha_not_exact_count)
    return;
445
  DBUG_ENTER("ha_ndbcluster::no_uncommitted_rows_reset");
446 447 448
  Thd_ndb *thd_ndb= get_thd_ndb(thd);
  thd_ndb->count++;
  thd_ndb->error= 0;
449 450 451
  DBUG_VOID_RETURN;
}

452 453
/*
  Take care of the error that occured in NDB
454

455
  RETURN
456
    0   No error
457 458 459
    #   The mapped error code
*/

460
void ha_ndbcluster::invalidate_dictionary_cache(bool global)
461 462
{
  NDBDICT *dict= get_ndb()->getDictionary();
463
  DBUG_ENTER("invalidate_dictionary_cache");
464
  DBUG_PRINT("info", ("invalidating %s", m_tabname));
465

466
  if (global)
467
  {
468 469 470 471
    const NDBTAB *tab= dict->getTable(m_tabname);
    if (!tab)
      DBUG_VOID_RETURN;
    if (tab->getObjectStatus() == NdbDictionary::Object::Invalid)
472 473 474 475 476 477 478 479
    {
      // Global cache has already been invalidated
      dict->removeCachedTable(m_tabname);
      global= FALSE;
    }
    else
      dict->invalidateTable(m_tabname);
  }
480 481
  else
    dict->removeCachedTable(m_tabname);
482
  table->s->version=0L;			/* Free when thread is ready */
483
  /* Invalidate indexes */
484
  for (uint i= 0; i < table->s->keys; i++)
485 486 487 488 489
  {
    NDBINDEX *index = (NDBINDEX *) m_index[i].index;
    NDBINDEX *unique_index = (NDBINDEX *) m_index[i].unique_index;
    NDB_INDEX_TYPE idx_type= m_index[i].type;

490 491 492
    switch (idx_type) {
    case PRIMARY_KEY_ORDERED_INDEX:
    case ORDERED_INDEX:
493 494 495 496
      if (global)
        dict->invalidateIndex(index->getName(), m_tabname);
      else
        dict->removeCachedIndex(index->getName(), m_tabname);
serg@serg.mylan's avatar
serg@serg.mylan committed
497
      break;
498
    case UNIQUE_ORDERED_INDEX:
499 500 501 502
      if (global)
        dict->invalidateIndex(index->getName(), m_tabname);
      else
        dict->removeCachedIndex(index->getName(), m_tabname);
503
    case UNIQUE_INDEX:
504 505 506 507
      if (global)
        dict->invalidateIndex(unique_index->getName(), m_tabname);
      else
        dict->removeCachedIndex(unique_index->getName(), m_tabname);
508
      break;
509 510
    case PRIMARY_KEY_INDEX:
    case UNDEFINED_INDEX:
511 512 513
      break;
    }
  }
514
  DBUG_VOID_RETURN;
515
}
516

517
int ha_ndbcluster::ndb_err(NdbTransaction *trans)
518
{
519
  int res;
520
  NdbError err= trans->getNdbError();
521 522 523 524 525
  DBUG_ENTER("ndb_err");
  
  ERR_PRINT(err);
  switch (err.classification) {
  case NdbError::SchemaError:
526
  {
527 528 529 530 531 532 533
    /* Close other open handlers not used by any thread */
    TABLE_LIST table_list;
    bzero((char*) &table_list,sizeof(table_list));
    table_list.db= m_dbname;
    table_list.alias= table_list.table_name= m_tabname;
    close_cached_tables(current_thd, 0, &table_list);

534 535
    invalidate_dictionary_cache(TRUE);

536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
    if (err.code==284)
    {
      /*
         Check if the table is _really_ gone or if the table has
         been alterend and thus changed table id
       */
      NDBDICT *dict= get_ndb()->getDictionary();
      DBUG_PRINT("info", ("Check if table %s is really gone", m_tabname));
      if (!(dict->getTable(m_tabname)))
      {
        err= dict->getNdbError();
        DBUG_PRINT("info", ("Table not found, error: %d", err.code));
        if (err.code != 709)
          DBUG_RETURN(1);
      }
551
      DBUG_PRINT("info", ("Table exists but must have changed"));
552
    }
553
    break;
554
  }
555 556 557
  default:
    break;
  }
558 559
  res= ndb_to_mysql_error(&err);
  DBUG_PRINT("info", ("transformed ndbcluster error %d to mysql error %d", 
560
                      err.code, res));
561
  if (res == HA_ERR_FOUND_DUPP_KEY)
562 563
  {
    if (m_rows_to_insert == 1)
564 565 566 567 568 569
    {
      /*
	We can only distinguish between primary and non-primary
	violations here, so we need to return MAX_KEY for non-primary
	to signal that key is unknown
      */
570
      m_dupkey= err.code == 630 ? table->s->primary_key : MAX_KEY; 
571
    }
572
    else
monty@mishka.local's avatar
monty@mishka.local committed
573 574
    {
      /* We are batching inserts, offending key is not available */
575
      m_dupkey= (uint) -1;
monty@mishka.local's avatar
monty@mishka.local committed
576
    }
577
  }
578
  DBUG_RETURN(res);
579 580 581
}


582
/*
583
  Override the default get_error_message in order to add the 
584 585 586
  error message of NDB 
 */

587
bool ha_ndbcluster::get_error_message(int error, 
588
                                      String *buf)
589
{
590
  DBUG_ENTER("ha_ndbcluster::get_error_message");
591
  DBUG_PRINT("enter", ("error: %d", error));
592

593
  Ndb *ndb= get_ndb();
594
  if (!ndb)
595
    DBUG_RETURN(FALSE);
596

597
  const NdbError err= ndb->getNdbError(error);
598 599 600 601
  bool temporary= err.status==NdbError::TemporaryError;
  buf->set(err.message, strlen(err.message), &my_charset_bin);
  DBUG_PRINT("exit", ("message: %s, temporary: %d", buf->ptr(), temporary));
  DBUG_RETURN(temporary);
602 603 604
}


tulin@dl145c.mysql.com's avatar
tulin@dl145c.mysql.com committed
605
#ifndef DBUG_OFF
pekka@mysql.com's avatar
pekka@mysql.com committed
606 607 608 609
/*
  Check if type is supported by NDB.
*/

tulin@dl145c.mysql.com's avatar
tulin@dl145c.mysql.com committed
610
static bool ndb_supported_type(enum_field_types type)
pekka@mysql.com's avatar
pekka@mysql.com committed
611 612
{
  switch (type) {
pekka@mysql.com's avatar
pekka@mysql.com committed
613 614 615 616 617 618 619
  case MYSQL_TYPE_TINY:        
  case MYSQL_TYPE_SHORT:
  case MYSQL_TYPE_LONG:
  case MYSQL_TYPE_INT24:       
  case MYSQL_TYPE_LONGLONG:
  case MYSQL_TYPE_FLOAT:
  case MYSQL_TYPE_DOUBLE:
620 621
  case MYSQL_TYPE_DECIMAL:    
  case MYSQL_TYPE_NEWDECIMAL:
pekka@mysql.com's avatar
pekka@mysql.com committed
622 623 624 625 626 627 628 629
  case MYSQL_TYPE_TIMESTAMP:
  case MYSQL_TYPE_DATETIME:    
  case MYSQL_TYPE_DATE:
  case MYSQL_TYPE_NEWDATE:
  case MYSQL_TYPE_TIME:        
  case MYSQL_TYPE_YEAR:        
  case MYSQL_TYPE_STRING:      
  case MYSQL_TYPE_VAR_STRING:
pekka@mysql.com's avatar
pekka@mysql.com committed
630
  case MYSQL_TYPE_VARCHAR:
pekka@mysql.com's avatar
pekka@mysql.com committed
631 632 633 634 635 636
  case MYSQL_TYPE_TINY_BLOB:
  case MYSQL_TYPE_BLOB:    
  case MYSQL_TYPE_MEDIUM_BLOB:   
  case MYSQL_TYPE_LONG_BLOB:  
  case MYSQL_TYPE_ENUM:
  case MYSQL_TYPE_SET:         
637
  case MYSQL_TYPE_BIT:
638
  case MYSQL_TYPE_GEOMETRY:
639
    return TRUE;
pekka@mysql.com's avatar
pekka@mysql.com committed
640
  case MYSQL_TYPE_NULL:   
pekka@mysql.com's avatar
pekka@mysql.com committed
641
    break;
pekka@mysql.com's avatar
pekka@mysql.com committed
642
  }
643
  return FALSE;
pekka@mysql.com's avatar
pekka@mysql.com committed
644
}
tulin@dl145c.mysql.com's avatar
tulin@dl145c.mysql.com committed
645
#endif /* !DBUG_OFF */
pekka@mysql.com's avatar
pekka@mysql.com committed
646 647


648 649 650 651 652
/*
  Instruct NDB to set the value of the hidden primary key
*/

bool ha_ndbcluster::set_hidden_key(NdbOperation *ndb_op,
653
                                   uint fieldnr, const byte *field_ptr)
654 655 656
{
  DBUG_ENTER("set_hidden_key");
  DBUG_RETURN(ndb_op->equal(fieldnr, (char*)field_ptr,
657
                            NDB_HIDDEN_PRIMARY_KEY_LENGTH) != 0);
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
}


/*
  Instruct NDB to set the value of one primary key attribute
*/

int ha_ndbcluster::set_ndb_key(NdbOperation *ndb_op, Field *field,
                               uint fieldnr, const byte *field_ptr)
{
  uint32 pack_len= field->pack_length();
  DBUG_ENTER("set_ndb_key");
  DBUG_PRINT("enter", ("%d: %s, ndb_type: %u, len=%d", 
                       fieldnr, field->field_name, field->type(),
                       pack_len));
  DBUG_DUMP("key", (char*)field_ptr, pack_len);
  
tulin@dl145c.mysql.com's avatar
tulin@dl145c.mysql.com committed
675 676 677 678
  DBUG_ASSERT(ndb_supported_type(field->type()));
  DBUG_ASSERT(! (field->flags & BLOB_FLAG));
  // Common implementation for most field types
  DBUG_RETURN(ndb_op->equal(fieldnr, (char*) field_ptr, pack_len) != 0);
679 680 681 682 683 684 685 686
}


/*
 Instruct NDB to set the value of one attribute
*/

int ha_ndbcluster::set_ndb_value(NdbOperation *ndb_op, Field *field, 
687
                                 uint fieldnr, bool *set_blob_value)
688 689 690 691 692 693 694 695
{
  const byte* field_ptr= field->ptr;
  uint32 pack_len=  field->pack_length();
  DBUG_ENTER("set_ndb_value");
  DBUG_PRINT("enter", ("%d: %s, type: %u, len=%d, is_null=%s", 
                       fieldnr, field->field_name, field->type(), 
                       pack_len, field->is_null()?"Y":"N"));
  DBUG_DUMP("value", (char*) field_ptr, pack_len);
pekka@mysql.com's avatar
pekka@mysql.com committed
696

tulin@dl145c.mysql.com's avatar
tulin@dl145c.mysql.com committed
697
  DBUG_ASSERT(ndb_supported_type(field->type()));
698
  {
699
    // ndb currently does not support size 0
700
    uint32 empty_field;
701 702
    if (pack_len == 0)
    {
703 704 705
      pack_len= sizeof(empty_field);
      field_ptr= (byte *)&empty_field;
      if (field->is_null())
706
        empty_field= 0;
707
      else
708
        empty_field= 1;
709
    }
pekka@mysql.com's avatar
pekka@mysql.com committed
710 711
    if (! (field->flags & BLOB_FLAG))
    {
712 713
      if (field->type() != MYSQL_TYPE_BIT)
      {
714 715 716 717 718 719 720
        if (field->is_null())
          // Set value to NULL
          DBUG_RETURN((ndb_op->setValue(fieldnr, 
                                        (char*)NULL, pack_len) != 0));
        // Common implementation for most field types
        DBUG_RETURN(ndb_op->setValue(fieldnr, 
                                     (char*)field_ptr, pack_len) != 0);
721 722 723
      }
      else // if (field->type() == MYSQL_TYPE_BIT)
      {
724
        longlong bits= field->val_int();
725
 
726 727
        // Round up bit field length to nearest word boundry
        pack_len= ((pack_len + 3) >> 2) << 2;
728 729 730 731 732
        DBUG_ASSERT(pack_len <= 8);
        if (field->is_null())
          // Set value to NULL
          DBUG_RETURN((ndb_op->setValue(fieldnr, (char*)NULL, pack_len) != 0));
        DBUG_PRINT("info", ("bit field"));
733
        DBUG_DUMP("value", (char*)&bits, pack_len);
734
#ifdef WORDS_BIGENDIAN
735
        /* store lsw first */
joerg@trift2's avatar
joerg@trift2 committed
736 737
        bits = ((bits >> 32) & 0x00000000FFFFFFFFLL)
          |    ((bits << 32) & 0xFFFFFFFF00000000LL);
738
#endif
739
        DBUG_RETURN(ndb_op->setValue(fieldnr, (char*)&bits, pack_len) != 0);
740
      }
pekka@mysql.com's avatar
pekka@mysql.com committed
741 742
    }
    // Blob type
743
    NdbBlob *ndb_blob= ndb_op->getBlobHandle(fieldnr);
pekka@mysql.com's avatar
pekka@mysql.com committed
744 745 746 747 748 749 750 751 752 753 754 755
    if (ndb_blob != NULL)
    {
      if (field->is_null())
        DBUG_RETURN(ndb_blob->setNull() != 0);

      Field_blob *field_blob= (Field_blob*)field;

      // Get length and pointer to data
      uint32 blob_len= field_blob->get_length(field_ptr);
      char* blob_ptr= NULL;
      field_blob->get_ptr(&blob_ptr);

756 757 758
      // Looks like NULL ptr signals length 0 blob
      if (blob_ptr == NULL) {
        DBUG_ASSERT(blob_len == 0);
759
        blob_ptr= (char*)"";
760
      }
pekka@mysql.com's avatar
pekka@mysql.com committed
761

762
      DBUG_PRINT("value", ("set blob ptr: %p  len: %u",
elliot@mysql.com's avatar
elliot@mysql.com committed
763
                           blob_ptr, blob_len));
pekka@mysql.com's avatar
pekka@mysql.com committed
764 765
      DBUG_DUMP("value", (char*)blob_ptr, min(blob_len, 26));

766
      if (set_blob_value)
767
        *set_blob_value= TRUE;
pekka@mysql.com's avatar
pekka@mysql.com committed
768 769 770 771
      // No callback needed to write value
      DBUG_RETURN(ndb_blob->setValue(blob_ptr, blob_len) != 0);
    }
    DBUG_RETURN(1);
772
  }
pekka@mysql.com's avatar
pekka@mysql.com committed
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
}


/*
  Callback to read all blob values.
  - not done in unpack_record because unpack_record is valid
    after execute(Commit) but reading blobs is not
  - may only generate read operations; they have to be executed
    somewhere before the data is available
  - due to single buffer for all blobs, we let the last blob
    process all blobs (last so that all are active)
  - null bit is still set in unpack_record
  - TODO allocate blob part aligned buffers
*/

788
NdbBlob::ActiveHook g_get_ndb_blobs_value;
pekka@mysql.com's avatar
pekka@mysql.com committed
789

790
int g_get_ndb_blobs_value(NdbBlob *ndb_blob, void *arg)
pekka@mysql.com's avatar
pekka@mysql.com committed
791
{
792
  DBUG_ENTER("g_get_ndb_blobs_value");
pekka@mysql.com's avatar
pekka@mysql.com committed
793 794 795
  if (ndb_blob->blobsNextBlob() != NULL)
    DBUG_RETURN(0);
  ha_ndbcluster *ha= (ha_ndbcluster *)arg;
796
  DBUG_RETURN(ha->get_ndb_blobs_value(ndb_blob, ha->m_blobs_offset));
pekka@mysql.com's avatar
pekka@mysql.com committed
797 798
}

799 800
int ha_ndbcluster::get_ndb_blobs_value(NdbBlob *last_ndb_blob,
				       my_ptrdiff_t ptrdiff)
pekka@mysql.com's avatar
pekka@mysql.com committed
801 802 803 804 805 806 807 808
{
  DBUG_ENTER("get_ndb_blobs_value");

  // Field has no field number so cannot use TABLE blob_field
  // Loop twice, first only counting total buffer size
  for (int loop= 0; loop <= 1; loop++)
  {
    uint32 offset= 0;
809
    for (uint i= 0; i < table->s->fields; i++)
pekka@mysql.com's avatar
pekka@mysql.com committed
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
    {
      Field *field= table->field[i];
      NdbValue value= m_value[i];
      if (value.ptr != NULL && (field->flags & BLOB_FLAG))
      {
        Field_blob *field_blob= (Field_blob *)field;
        NdbBlob *ndb_blob= value.blob;
        Uint64 blob_len= 0;
        if (ndb_blob->getLength(blob_len) != 0)
          DBUG_RETURN(-1);
        // Align to Uint64
        uint32 blob_size= blob_len;
        if (blob_size % 8 != 0)
          blob_size+= 8 - blob_size % 8;
        if (loop == 1)
        {
826
          char *buf= m_blobs_buffer + offset;
pekka@mysql.com's avatar
pekka@mysql.com committed
827
          uint32 len= 0xffffffff;  // Max uint32
828 829
          DBUG_PRINT("value", ("read blob ptr: 0x%lx  len: %u",
                               (long)buf, (uint)blob_len));
pekka@mysql.com's avatar
pekka@mysql.com committed
830 831 832
          if (ndb_blob->readData(buf, len) != 0)
            DBUG_RETURN(-1);
          DBUG_ASSERT(len == blob_len);
833 834
          // Ugly hack assumes only ptr needs to be changed
          field_blob->ptr+= ptrdiff;
pekka@mysql.com's avatar
pekka@mysql.com committed
835
          field_blob->set_ptr(len, buf);
836
          field_blob->ptr-= ptrdiff;
pekka@mysql.com's avatar
pekka@mysql.com committed
837 838 839 840
        }
        offset+= blob_size;
      }
    }
841
    if (loop == 0 && offset > m_blobs_buffer_size)
pekka@mysql.com's avatar
pekka@mysql.com committed
842
    {
843 844
      my_free(m_blobs_buffer, MYF(MY_ALLOW_ZERO_PTR));
      m_blobs_buffer_size= 0;
pekka@mysql.com's avatar
pekka@mysql.com committed
845
      DBUG_PRINT("value", ("allocate blobs buffer size %u", offset));
846 847
      m_blobs_buffer= my_malloc(offset, MYF(MY_WME));
      if (m_blobs_buffer == NULL)
848 849 850
      {
        sql_print_error("ha_ndbcluster::get_ndb_blobs_value: "
                        "my_malloc(%u) failed", offset);
pekka@mysql.com's avatar
pekka@mysql.com committed
851
        DBUG_RETURN(-1);
852
      }
853
      m_blobs_buffer_size= offset;
pekka@mysql.com's avatar
pekka@mysql.com committed
854
    }
855
  }
pekka@mysql.com's avatar
pekka@mysql.com committed
856
  DBUG_RETURN(0);
857 858 859 860 861
}


/*
  Instruct NDB to fetch one field
pekka@mysql.com's avatar
pekka@mysql.com committed
862 863
  - data is read directly into buffer provided by field
    if field is NULL, data is read into memory provided by NDBAPI
864 865
*/

pekka@mysql.com's avatar
pekka@mysql.com committed
866
int ha_ndbcluster::get_ndb_value(NdbOperation *ndb_op, Field *field,
867
                                 uint fieldnr, byte* buf)
868 869
{
  DBUG_ENTER("get_ndb_value");
pekka@mysql.com's avatar
pekka@mysql.com committed
870 871 872 873 874
  DBUG_PRINT("enter", ("fieldnr: %d flags: %o", fieldnr,
                       (int)(field != NULL ? field->flags : 0)));

  if (field != NULL)
  {
tulin@dl145c.mysql.com's avatar
tulin@dl145c.mysql.com committed
875 876
      DBUG_ASSERT(buf);
      DBUG_ASSERT(ndb_supported_type(field->type()));
pekka@mysql.com's avatar
pekka@mysql.com committed
877 878
      DBUG_ASSERT(field->ptr != NULL);
      if (! (field->flags & BLOB_FLAG))
879
      { 
880 881
        if (field->type() != MYSQL_TYPE_BIT)
        {
882 883 884 885 886 887 888 889
          byte *field_buf;
          if (field->pack_length() != 0)
            field_buf= buf + (field->ptr - table->record[0]);
          else
            field_buf= (byte *)&dummy_buf;
          m_value[fieldnr].rec= ndb_op->getValue(fieldnr, 
                                                 field_buf);
        }
890 891 892 893
        else // if (field->type() == MYSQL_TYPE_BIT)
        {
          m_value[fieldnr].rec= ndb_op->getValue(fieldnr);
        }
pekka@mysql.com's avatar
pekka@mysql.com committed
894 895 896 897 898 899 900 901 902
        DBUG_RETURN(m_value[fieldnr].rec == NULL);
      }

      // Blob type
      NdbBlob *ndb_blob= ndb_op->getBlobHandle(fieldnr);
      m_value[fieldnr].blob= ndb_blob;
      if (ndb_blob != NULL)
      {
        // Set callback
903
	m_blobs_offset= buf - (byte*) table->record[0];
pekka@mysql.com's avatar
pekka@mysql.com committed
904
        void *arg= (void *)this;
905
        DBUG_RETURN(ndb_blob->setActiveHook(g_get_ndb_blobs_value, arg) != 0);
pekka@mysql.com's avatar
pekka@mysql.com committed
906 907 908 909 910
      }
      DBUG_RETURN(1);
  }

  // Used for hidden key only
911
  m_value[fieldnr].rec= ndb_op->getValue(fieldnr, m_ref);
pekka@mysql.com's avatar
pekka@mysql.com committed
912 913 914 915 916 917 918 919 920
  DBUG_RETURN(m_value[fieldnr].rec == NULL);
}


/*
  Check if any set or get of blob value in current query.
*/
bool ha_ndbcluster::uses_blob_value(bool all_fields)
{
921
  if (table->s->blob_fields == 0)
922
    return FALSE;
pekka@mysql.com's avatar
pekka@mysql.com committed
923
  if (all_fields)
924
    return TRUE;
pekka@mysql.com's avatar
pekka@mysql.com committed
925
  {
926
    uint no_fields= table->s->fields;
pekka@mysql.com's avatar
pekka@mysql.com committed
927
    int i;
928
    THD *thd= current_thd;
pekka@mysql.com's avatar
pekka@mysql.com committed
929 930 931 932 933 934
    // They always put blobs at the end..
    for (i= no_fields - 1; i >= 0; i--)
    {
      Field *field= table->field[i];
      if (thd->query_id == field->query_id)
      {
935
        return TRUE;
pekka@mysql.com's avatar
pekka@mysql.com committed
936 937 938
      }
    }
  }
939
  return FALSE;
940 941 942 943 944 945 946 947 948 949 950 951 952
}


/*
  Get metadata for this table from NDB 

  IMPLEMENTATION
    - check that frm-file on disk is equal to frm-file
      of table accessed in NDB
*/

int ha_ndbcluster::get_metadata(const char *path)
{
953 954
  Ndb *ndb= get_ndb();
  NDBDICT *dict= ndb->getDictionary();
955 956
  const NDBTAB *tab;
  int error;
957
  bool invalidating_ndb_table= FALSE;
958

959 960 961
  DBUG_ENTER("get_metadata");
  DBUG_PRINT("enter", ("m_tabname: %s, path: %s", m_tabname, path));

962
  do {
963
    const void *data= NULL, *pack_data= NULL;
964 965 966 967
    uint length, pack_length;

    if (!(tab= dict->getTable(m_tabname)))
      ERR_RETURN(dict->getNdbError());
968
    // Check if thread has stale local cache
969 970 971 972 973 974 975
    if (tab->getObjectStatus() == NdbDictionary::Object::Invalid)
    {
      invalidate_dictionary_cache(FALSE);
      if (!(tab= dict->getTable(m_tabname)))
         ERR_RETURN(dict->getNdbError());
      DBUG_PRINT("info", ("Table schema version: %d", tab->getObjectVersion()));
    }
976 977 978 979 980
    /*
      Compare FrmData in NDB with frm file from disk.
    */
    error= 0;
    if (readfrm(path, &data, &length) ||
981
        packfrm(data, length, &pack_data, &pack_length))
982 983 984 985 986
    {
      my_free((char*)data, MYF(MY_ALLOW_ZERO_PTR));
      my_free((char*)pack_data, MYF(MY_ALLOW_ZERO_PTR));
      DBUG_RETURN(1);
    }
987
    
988
    if ((pack_length != tab->getFrmLength()) || 
989
        (memcmp(pack_data, tab->getFrmData(), pack_length)))
990 991 992
    {
      if (!invalidating_ndb_table)
      {
993
        DBUG_PRINT("info", ("Invalidating table"));
994
        invalidate_dictionary_cache(TRUE);
995
        invalidating_ndb_table= TRUE;
996 997 998
      }
      else
      {
999 1000 1001 1002 1003 1004 1005 1006
        DBUG_PRINT("error", 
                   ("metadata, pack_length: %d getFrmLength: %d memcmp: %d", 
                    pack_length, tab->getFrmLength(),
                    memcmp(pack_data, tab->getFrmData(), pack_length)));      
        DBUG_DUMP("pack_data", (char*)pack_data, pack_length);
        DBUG_DUMP("frm", (char*)tab->getFrmData(), tab->getFrmLength());
        error= 3;
        invalidating_ndb_table= FALSE;
1007 1008 1009 1010
      }
    }
    else
    {
1011
      invalidating_ndb_table= FALSE;
1012 1013 1014 1015 1016
    }
    my_free((char*)data, MYF(0));
    my_free((char*)pack_data, MYF(0));
  } while (invalidating_ndb_table);

1017 1018
  if (error)
    DBUG_RETURN(error);
1019
  
1020
  m_table_version= tab->getObjectVersion();
1021 1022 1023 1024
  m_table= (void *)tab; 
  m_table_info= NULL; // Set in external lock
  
  DBUG_RETURN(build_index_list(ndb, table, ILBP_OPEN));
1025
}
1026

1027
static int fix_unique_index_attr_order(NDB_INDEX_DATA &data,
1028 1029
                                       const NDBINDEX *index,
                                       KEY *key_info)
1030 1031 1032 1033 1034 1035 1036
{
  DBUG_ENTER("fix_unique_index_attr_order");
  unsigned sz= index->getNoOfIndexColumns();

  if (data.unique_index_attrid_map)
    my_free((char*)data.unique_index_attrid_map, MYF(0));
  data.unique_index_attrid_map= (unsigned char*)my_malloc(sz,MYF(MY_WME));
1037 1038 1039 1040 1041 1042
  if (data.unique_index_attrid_map == 0)
  {
    sql_print_error("fix_unique_index_attr_order: my_malloc(%u) failure",
                    (unsigned int)sz);
    DBUG_RETURN(HA_ERR_OUT_OF_MEM);
  }
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054

  KEY_PART_INFO* key_part= key_info->key_part;
  KEY_PART_INFO* end= key_part+key_info->key_parts;
  DBUG_ASSERT(key_info->key_parts == sz);
  for (unsigned i= 0; key_part != end; key_part++, i++) 
  {
    const char *field_name= key_part->field->field_name;
#ifndef DBUG_OFF
   data.unique_index_attrid_map[i]= 255;
#endif
    for (unsigned j= 0; j < sz; j++)
    {
1055
      const NDBCOL *c= index->getColumn(j);
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
1056
      if (strcmp(field_name, c->getName()) == 0)
1057
      {
1058 1059
        data.unique_index_attrid_map[i]= j;
        break;
1060 1061 1062 1063 1064 1065
      }
    }
    DBUG_ASSERT(data.unique_index_attrid_map[i] != 255);
  }
  DBUG_RETURN(0);
}
1066

1067 1068


1069
int ha_ndbcluster::build_index_list(Ndb *ndb, TABLE *tab, enum ILBP phase)
1070
{
1071
  uint i;
1072
  int error= 0;
1073
  const char *index_name;
1074
  char unique_index_name[FN_LEN];
1075
  bool null_in_unique_index= false;
1076
  static const char* unique_suffix= "$unique";
1077
  KEY* key_info= tab->key_info;
1078
  const char **key_name= tab->s->keynames.type_names;
1079
  NDBDICT *dict= ndb->getDictionary();
1080
  DBUG_ENTER("ha_ndbcluster::build_index_list");
1081
  
1082
  m_has_unique_index= FALSE;
1083
  // Save information about all known indexes
1084
  for (i= 0; i < tab->s->keys; i++, key_info++, key_name++)
1085
  {
1086
    index_name= *key_name;
1087
    NDB_INDEX_TYPE idx_type= get_index_type_from_table(i);
1088
    m_index[i].type= idx_type;
1089
    if (idx_type == UNIQUE_ORDERED_INDEX || idx_type == UNIQUE_INDEX)
1090
    {
1091
      m_has_unique_index= TRUE;
1092 1093
      strxnmov(unique_index_name, FN_LEN, index_name, unique_suffix, NullS);
      DBUG_PRINT("info", ("Created unique index name \'%s\' for index %d",
1094
                          unique_index_name, i));
1095
    }
1096 1097 1098
    // Create secondary indexes if in create phase
    if (phase == ILBP_CREATE)
    {
1099 1100
      DBUG_PRINT("info", ("Creating index %u: %s", i, index_name));      
      switch (idx_type){
1101
        
1102
      case PRIMARY_KEY_INDEX:
1103 1104
        // Do nothing, already created
        break;
1105
      case PRIMARY_KEY_ORDERED_INDEX:
1106 1107
        error= create_ordered_index(index_name, key_info);
        break;
1108
      case UNIQUE_ORDERED_INDEX:
1109 1110 1111
        if (!(error= create_ordered_index(index_name, key_info)))
          error= create_unique_index(unique_index_name, key_info);
        break;
1112
      case UNIQUE_INDEX:
1113 1114 1115 1116 1117 1118 1119 1120
	if (check_index_fields_not_null(i))
	{
	  push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
			      ER_NULL_COLUMN_IN_INDEX,
			      "Ndb does not support unique index on NULL valued attributes, index access with NULL value will become full table scan");
	  null_in_unique_index= true;
	}
	error= create_unique_index(unique_index_name, key_info);
1121
        break;
1122
      case ORDERED_INDEX:
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
	if (key_info->algorithm == HA_KEY_ALG_HASH)
	{
	  push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
			      ER_UNSUPPORTED_EXTENSION,
			      ER(ER_UNSUPPORTED_EXTENSION),
			      "Ndb does not support non-unique "
			      "hash based indexes");
	  error= HA_ERR_UNSUPPORTED;
	  break;
	}
1133 1134
        error= create_ordered_index(index_name, key_info);
        break;
1135
      default:
1136 1137
        DBUG_ASSERT(FALSE);
        break;
1138 1139 1140
      }
      if (error)
      {
1141 1142 1143
        DBUG_PRINT("error", ("Failed to create index %u", i));
        drop_table();
        break;
1144 1145 1146
      }
    }
    // Add handles to index objects
1147
    if (idx_type != PRIMARY_KEY_INDEX && idx_type != UNIQUE_INDEX)
1148
    {
1149
      DBUG_PRINT("info", ("Get handle to index %s", index_name));
1150
      const NDBINDEX *index= dict->getIndex(index_name, m_tabname);
1151 1152
      if (!index)
        ERR_RETURN(dict->getNdbError());
mskold@mysql.com's avatar
mskold@mysql.com committed
1153
      m_index[i].index= (void *) index;
1154
    }
1155
    if (idx_type == UNIQUE_ORDERED_INDEX || idx_type == UNIQUE_INDEX)
1156
    {
1157 1158
      DBUG_PRINT("info", ("Get handle to unique_index %s", unique_index_name));
      const NDBINDEX *index= dict->getIndex(unique_index_name, m_tabname);
1159 1160
      if (!index)
        ERR_RETURN(dict->getNdbError());
mskold@mysql.com's avatar
mskold@mysql.com committed
1161
      m_index[i].unique_index= (void *) index;
1162 1163
      error= fix_unique_index_attr_order(m_index[i], index, key_info);
    }
1164 1165 1166 1167 1168
    if (idx_type == UNIQUE_INDEX && 
	phase != ILBP_CREATE &&
	check_index_fields_not_null(i))
      null_in_unique_index= true;
    m_index[i].null_in_unique_index= null_in_unique_index;
1169
  }
1170 1171
  
  DBUG_RETURN(error);
1172 1173
}

1174

1175 1176 1177 1178
/*
  Decode the type of an index from information 
  provided in table object
*/
1179
NDB_INDEX_TYPE ha_ndbcluster::get_index_type_from_table(uint inx) const
1180
{
1181
  bool is_hash_index=  (table->key_info[inx].algorithm == HA_KEY_ALG_HASH);
1182
  if (inx == table->s->primary_key)
1183
    return is_hash_index ? PRIMARY_KEY_INDEX : PRIMARY_KEY_ORDERED_INDEX;
1184 1185 1186 1187

  return ((table->key_info[inx].flags & HA_NOSAME) ? 
          (is_hash_index ? UNIQUE_INDEX : UNIQUE_ORDERED_INDEX) :
          ORDERED_INDEX);
1188
} 
1189

1190
bool ha_ndbcluster::check_index_fields_not_null(uint inx)
1191 1192 1193 1194
{
  KEY* key_info= table->key_info + inx;
  KEY_PART_INFO* key_part= key_info->key_part;
  KEY_PART_INFO* end= key_part+key_info->key_parts;
1195
  DBUG_ENTER("ha_ndbcluster::check_index_fields_not_null");
1196 1197 1198 1199 1200
  
  for (; key_part != end; key_part++) 
    {
      Field* field= key_part->field;
      if (field->maybe_null())
1201
        DBUG_RETURN(true);
1202 1203
    }
  
1204
  DBUG_RETURN(false);
1205
}
1206 1207 1208

void ha_ndbcluster::release_metadata()
{
1209
  uint i;
1210

1211 1212 1213 1214
  DBUG_ENTER("release_metadata");
  DBUG_PRINT("enter", ("m_tabname: %s", m_tabname));

  m_table= NULL;
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
1215
  m_table_info= NULL;
1216

1217
  // Release index list 
1218 1219
  for (i= 0; i < MAX_KEY; i++)
  {
1220 1221
    m_index[i].unique_index= NULL;      
    m_index[i].index= NULL;      
1222 1223 1224 1225 1226
    if (m_index[i].unique_index_attrid_map)
    {
      my_free((char *)m_index[i].unique_index_attrid_map, MYF(0));
      m_index[i].unique_index_attrid_map= NULL;
    }
1227 1228
  }

1229 1230 1231
  DBUG_VOID_RETURN;
}

pekka@mysql.com's avatar
pekka@mysql.com committed
1232
int ha_ndbcluster::get_ndb_lock_type(enum thr_lock_type type)
1233
{
1234
  DBUG_ENTER("ha_ndbcluster::get_ndb_lock_type");
1235
  if (type >= TL_WRITE_ALLOW_WRITE)
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
  {
    DBUG_PRINT("info", ("Using exclusive lock"));
    DBUG_RETURN(NdbOperation::LM_Exclusive);
  }
  else if (type ==  TL_READ_WITH_SHARED_LOCKS ||
	   uses_blob_value(m_retrieve_all_fields))
  {
    DBUG_PRINT("info", ("Using read lock"));
    DBUG_RETURN(NdbOperation::LM_Read);
  }
pekka@mysql.com's avatar
pekka@mysql.com committed
1246
  else
1247 1248 1249 1250
  {
    DBUG_PRINT("info", ("Using committed read"));
    DBUG_RETURN(NdbOperation::LM_CommittedRead);
  }
1251 1252
}

1253 1254 1255 1256 1257 1258
static const ulong index_type_flags[]=
{
  /* UNDEFINED_INDEX */
  0,                         

  /* PRIMARY_KEY_INDEX */
1259
  HA_ONLY_WHOLE_INDEX, 
1260 1261

  /* PRIMARY_KEY_ORDERED_INDEX */
1262
  /* 
mskold@mysql.com's avatar
mskold@mysql.com committed
1263
     Enable HA_KEYREAD_ONLY when "sorted" indexes are supported, 
1264 1265 1266
     thus ORDERD BY clauses can be optimized by reading directly 
     through the index.
  */
mskold@mysql.com's avatar
mskold@mysql.com committed
1267
  // HA_KEYREAD_ONLY | 
1268
  HA_READ_NEXT |
1269
  HA_READ_PREV |
1270 1271
  HA_READ_RANGE |
  HA_READ_ORDER,
1272 1273

  /* UNIQUE_INDEX */
1274
  HA_ONLY_WHOLE_INDEX,
1275

1276
  /* UNIQUE_ORDERED_INDEX */
1277
  HA_READ_NEXT |
1278
  HA_READ_PREV |
1279 1280
  HA_READ_RANGE |
  HA_READ_ORDER,
1281

1282
  /* ORDERED_INDEX */
1283
  HA_READ_NEXT |
1284
  HA_READ_PREV |
1285 1286
  HA_READ_RANGE |
  HA_READ_ORDER
1287 1288 1289 1290 1291 1292 1293
};

static const int index_flags_size= sizeof(index_type_flags)/sizeof(ulong);

inline NDB_INDEX_TYPE ha_ndbcluster::get_index_type(uint idx_no) const
{
  DBUG_ASSERT(idx_no < MAX_KEY);
1294
  return m_index[idx_no].type;
1295 1296
}

1297 1298 1299 1300 1301 1302
inline bool ha_ndbcluster::has_null_in_unique_index(uint idx_no) const
{
  DBUG_ASSERT(idx_no < MAX_KEY);
  return m_index[idx_no].null_in_unique_index;
}

1303 1304 1305 1306 1307 1308 1309 1310

/*
  Get the flags for an index

  RETURN
    flags depending on the type of the index.
*/

1311 1312
inline ulong ha_ndbcluster::index_flags(uint idx_no, uint part,
                                        bool all_parts) const 
1313
{ 
1314
  DBUG_ENTER("ha_ndbcluster::index_flags");
1315
  DBUG_PRINT("info", ("idx_no: %d", idx_no));
1316
  DBUG_ASSERT(get_index_type_from_table(idx_no) < index_flags_size);
1317 1318
  DBUG_RETURN(index_type_flags[get_index_type_from_table(idx_no)] | 
              HA_KEY_SCAN_NOT_ROR);
1319 1320
}

pekka@mysql.com's avatar
pekka@mysql.com committed
1321 1322
static void shrink_varchar(Field* field, const byte* & ptr, char* buf)
{
1323
  if (field->type() == MYSQL_TYPE_VARCHAR && ptr != NULL) {
pekka@mysql.com's avatar
pekka@mysql.com committed
1324
    Field_varstring* f= (Field_varstring*)field;
pekka@mysql.com's avatar
pekka@mysql.com committed
1325
    if (f->length_bytes == 1) {
pekka@mysql.com's avatar
pekka@mysql.com committed
1326 1327 1328 1329 1330
      uint pack_len= field->pack_length();
      DBUG_ASSERT(1 <= pack_len && pack_len <= 256);
      if (ptr[1] == 0) {
        buf[0]= ptr[0];
      } else {
1331
        DBUG_ASSERT(FALSE);
pekka@mysql.com's avatar
pekka@mysql.com committed
1332 1333 1334 1335 1336 1337 1338
        buf[0]= 255;
      }
      memmove(buf + 1, ptr + 2, pack_len - 1);
      ptr= buf;
    }
  }
}
1339 1340 1341

int ha_ndbcluster::set_primary_key(NdbOperation *op, const byte *key)
{
1342
  KEY* key_info= table->key_info + table->s->primary_key;
1343 1344 1345 1346 1347 1348 1349
  KEY_PART_INFO* key_part= key_info->key_part;
  KEY_PART_INFO* end= key_part+key_info->key_parts;
  DBUG_ENTER("set_primary_key");

  for (; key_part != end; key_part++) 
  {
    Field* field= key_part->field;
pekka@mysql.com's avatar
pekka@mysql.com committed
1350 1351 1352
    const byte* ptr= key;
    char buf[256];
    shrink_varchar(field, ptr, buf);
1353
    if (set_ndb_key(op, field, 
1354
                    key_part->fieldnr-1, ptr))
1355
      ERR_RETURN(op->getNdbError());
pekka@mysql.com's avatar
pekka@mysql.com committed
1356
    key += key_part->store_length;
1357 1358 1359 1360 1361
  }
  DBUG_RETURN(0);
}


1362
int ha_ndbcluster::set_primary_key_from_record(NdbOperation *op, const byte *record)
1363
{
1364
  KEY* key_info= table->key_info + table->s->primary_key;
1365 1366
  KEY_PART_INFO* key_part= key_info->key_part;
  KEY_PART_INFO* end= key_part+key_info->key_parts;
1367
  DBUG_ENTER("set_primary_key_from_record");
1368 1369 1370 1371 1372

  for (; key_part != end; key_part++) 
  {
    Field* field= key_part->field;
    if (set_ndb_key(op, field, 
1373
		    key_part->fieldnr-1, record+key_part->offset))
1374 1375 1376 1377 1378
      ERR_RETURN(op->getNdbError());
  }
  DBUG_RETURN(0);
}

1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
int ha_ndbcluster::set_index_key_from_record(NdbOperation *op, const byte *record, uint keyno)
{
  KEY* key_info= table->key_info + keyno;
  KEY_PART_INFO* key_part= key_info->key_part;
  KEY_PART_INFO* end= key_part+key_info->key_parts;
  uint i;
  DBUG_ENTER("set_index_key_from_record");
                                                                                
  for (i= 0; key_part != end; key_part++, i++)
  {
    Field* field= key_part->field;
    if (set_ndb_key(op, field, m_index[keyno].unique_index_attrid_map[i],
                    record+key_part->offset))
      ERR_RETURN(m_active_trans->getNdbError());
  }
  DBUG_RETURN(0);
}

1397 1398
int 
ha_ndbcluster::set_index_key(NdbOperation *op, 
1399 1400
                             const KEY *key_info, 
                             const byte * key_ptr)
1401
{
1402
  DBUG_ENTER("ha_ndbcluster::set_index_key");
1403 1404 1405 1406 1407 1408
  uint i;
  KEY_PART_INFO* key_part= key_info->key_part;
  KEY_PART_INFO* end= key_part+key_info->key_parts;
  
  for (i= 0; key_part != end; key_part++, i++) 
  {
pekka@mysql.com's avatar
pekka@mysql.com committed
1409 1410 1411 1412
    Field* field= key_part->field;
    const byte* ptr= key_part->null_bit ? key_ptr + 1 : key_ptr;
    char buf[256];
    shrink_varchar(field, ptr, buf);
tomas@poseidon.ndb.mysql.com's avatar
Merge  
tomas@poseidon.ndb.mysql.com committed
1413
    if (set_ndb_key(op, field, m_index[active_index].unique_index_attrid_map[i], ptr))
1414 1415 1416 1417 1418
      ERR_RETURN(m_active_trans->getNdbError());
    key_ptr+= key_part->store_length;
  }
  DBUG_RETURN(0);
}
1419

1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
inline 
int ha_ndbcluster::define_read_attrs(byte* buf, NdbOperation* op)
{
  uint i;
  THD *thd= current_thd;

  DBUG_ENTER("define_read_attrs");  

  // Define attributes to read
  for (i= 0; i < table->s->fields; i++) 
  {
    Field *field= table->field[i];
    if ((thd->query_id == field->query_id) ||
1433 1434
        ((field->flags & PRI_KEY_FLAG)) || 
        m_retrieve_all_fields)
1435 1436
    {      
      if (get_ndb_value(op, field, i, buf))
1437
        ERR_RETURN(op->getNdbError());
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
    } 
    else 
    {
      m_value[i].ptr= NULL;
    }
  }
    
  if (table->s->primary_key == MAX_KEY) 
  {
    DBUG_PRINT("info", ("Getting hidden key"));
    // Scanning table with no primary key
    int hidden_no= table->s->fields;      
#ifndef DBUG_OFF
    const NDBTAB *tab= (const NDBTAB *) m_table;    
    if (!tab->getColumn(hidden_no))
      DBUG_RETURN(1);
#endif
    if (get_ndb_value(op, NULL, hidden_no, NULL))
      ERR_RETURN(op->getNdbError());
  }
  DBUG_RETURN(0);
} 

1461 1462 1463 1464
/*
  Read one record from NDB using primary key
*/

1465
int ha_ndbcluster::pk_read(const byte *key, uint key_len, byte *buf) 
1466
{
1467
  uint no_fields= table->s->fields;
1468 1469
  NdbConnection *trans= m_active_trans;
  NdbOperation *op;
1470

1471 1472 1473 1474
  int res;
  DBUG_ENTER("pk_read");
  DBUG_PRINT("enter", ("key_len: %u", key_len));
  DBUG_DUMP("key", (char*)key, key_len);
1475

1476 1477
  NdbOperation::LockMode lm=
    (NdbOperation::LockMode)get_ndb_lock_type(m_lock.type);
joreland@mysql.com's avatar
joreland@mysql.com committed
1478
  if (!(op= trans->getNdbOperation((const NDBTAB *) m_table)) || 
1479
      op->readTuple(lm) != 0)
1480
    ERR_RETURN(trans->getNdbError());
1481
  
1482
  if (table->s->primary_key == MAX_KEY) 
1483 1484 1485 1486 1487
  {
    // This table has no primary key, use "hidden" primary key
    DBUG_PRINT("info", ("Using hidden key"));
    DBUG_DUMP("key", (char*)key, 8);    
    if (set_hidden_key(op, no_fields, key))
1488
      ERR_RETURN(trans->getNdbError());
1489
    
1490
    // Read key at the same time, for future reference
1491
    if (get_ndb_value(op, NULL, no_fields, NULL))
1492
      ERR_RETURN(trans->getNdbError());
1493 1494 1495 1496 1497 1498 1499
  } 
  else 
  {
    if ((res= set_primary_key(op, key)))
      return res;
  }
  
1500
  if ((res= define_read_attrs(buf, op)))
1501
    DBUG_RETURN(res);
1502
  
1503
  if (execute_no_commit_ie(this,trans,false) != 0) 
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
  {
    table->status= STATUS_NOT_FOUND;
    DBUG_RETURN(ndb_err(trans));
  }

  // The value have now been fetched from NDB  
  unpack_record(buf);
  table->status= 0;     
  DBUG_RETURN(0);
}

1515 1516 1517 1518 1519 1520
/*
  Read one complementing record from NDB using primary key from old_data
*/

int ha_ndbcluster::complemented_pk_read(const byte *old_data, byte *new_data)
{
1521
  uint no_fields= table->s->fields, i;
1522
  NdbTransaction *trans= m_active_trans;
1523 1524 1525 1526
  NdbOperation *op;
  THD *thd= current_thd;
  DBUG_ENTER("complemented_pk_read");

1527
  if (m_retrieve_all_fields)
1528 1529 1530
    // We have allready retrieved all fields, nothing to complement
    DBUG_RETURN(0);

1531 1532
  NdbOperation::LockMode lm=
    (NdbOperation::LockMode)get_ndb_lock_type(m_lock.type);
joreland@mysql.com's avatar
joreland@mysql.com committed
1533
  if (!(op= trans->getNdbOperation((const NDBTAB *) m_table)) || 
1534
      op->readTuple(lm) != 0)
1535
    ERR_RETURN(trans->getNdbError());
1536
  int res;
mskold@mysql.com's avatar
mskold@mysql.com committed
1537
  if ((res= set_primary_key_from_record(op, old_data)))
1538
    ERR_RETURN(trans->getNdbError());
1539 1540 1541 1542
  // Read all unreferenced non-key field(s)
  for (i= 0; i < no_fields; i++) 
  {
    Field *field= table->field[i];
1543
    if (!((field->flags & PRI_KEY_FLAG) ||
1544
          (thd->query_id == field->query_id)))
1545
    {
1546
      if (get_ndb_value(op, field, i, new_data))
1547
        ERR_RETURN(trans->getNdbError());
1548 1549
    }
  }
1550
  if (execute_no_commit(this,trans,false) != 0) 
1551 1552 1553 1554 1555 1556 1557 1558
  {
    table->status= STATUS_NOT_FOUND;
    DBUG_RETURN(ndb_err(trans));
  }

  // The value have now been fetched from NDB  
  unpack_record(new_data);
  table->status= 0;     
1559 1560 1561 1562 1563 1564 1565 1566

  /**
   * restore m_value
   */
  for (i= 0; i < no_fields; i++) 
  {
    Field *field= table->field[i];
    if (!((field->flags & PRI_KEY_FLAG) ||
1567
          (thd->query_id == field->query_id)))
1568 1569 1570 1571 1572
    {
      m_value[i].ptr= NULL;
    }
  }
  
1573 1574 1575
  DBUG_RETURN(0);
}

1576
/*
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
 * Check that all operations between first and last all
 * have gotten the errcode
 * If checking for HA_ERR_KEY_NOT_FOUND then update m_dupkey
 * for all succeeding operations
 */
bool ha_ndbcluster::check_all_operations_for_error(NdbTransaction *trans,
                                                   const NdbOperation *first,
                                                   const NdbOperation *last,
                                                   uint errcode)
{
  const NdbOperation *op= first;
  DBUG_ENTER("ha_ndbcluster::check_all_operations_for_error");

  while(op)
  {
    NdbError err= op->getNdbError();
    if (err.status != NdbError::Success)
    {
      if (ndb_to_mysql_error(&err) != (int) errcode)
        DBUG_RETURN(false);
      if (op == last) break;
      op= trans->getNextCompletedOperation(op);
    }
    else
    {
      // We found a duplicate
      if (op->getType() == NdbOperation::UniqueIndexAccess)
      {
        if (errcode == HA_ERR_KEY_NOT_FOUND)
        {
          NdbIndexOperation *iop= (NdbIndexOperation *) op;
          const NDBINDEX *index= iop->getIndex();
          // Find the key_no of the index
          for(uint i= 0; i<table->s->keys; i++)
          {
            if (m_index[i].unique_index == index)
            {
              m_dupkey= i;
              break;
            }
          }
        }
      }
      else
      {
        // Must have been primary key access
        DBUG_ASSERT(op->getType() == NdbOperation::PrimaryKeyAccess);
        if (errcode == HA_ERR_KEY_NOT_FOUND)
          m_dupkey= table->s->primary_key;
      }
      DBUG_RETURN(false);      
    }
  }
  DBUG_RETURN(true);
}

1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660

/**
 * Check if record contains any null valued columns that are part of a key
 */
static
int
check_null_in_record(const KEY* key_info, const byte *record)
{
  KEY_PART_INFO *curr_part, *end_part;
  curr_part= key_info->key_part;
  end_part= curr_part + key_info->key_parts;

  while (curr_part != end_part)
  {
    if (curr_part->null_bit &&
        (record[curr_part->null_offset] & curr_part->null_bit))
      return 1;
    curr_part++;
  }
  return 0;
  /*
    We could instead pre-compute a bitmask in table_share with one bit for
    every null-bit in the key, and so check this just by OR'ing the bitmask
    with the null bitmap in the record.
    But not sure it's worth it.
  */
}

1661 1662 1663
/*
 * Peek to check if any rows already exist with conflicting
 * primary key or unique index values
1664 1665
*/

1666
int ha_ndbcluster::peek_indexed_rows(const byte *record, bool check_pk)
1667
{
1668
  NdbTransaction *trans= m_active_trans;
1669
  NdbOperation *op;
1670 1671 1672 1673
  const NdbOperation *first, *last;
  uint i;
  int res;
  DBUG_ENTER("peek_indexed_rows");
1674

jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
1675 1676 1677
  NdbOperation::LockMode lm=
    (NdbOperation::LockMode)get_ndb_lock_type(m_lock.type);
  
1678
  first= NULL;
1679
  if (check_pk && table->s->primary_key != MAX_KEY)
1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
  {
    /*
     * Fetch any row with colliding primary key
     */
    if (!(op= trans->getNdbOperation((const NDBTAB *) m_table)) ||
        op->readTuple(lm) != 0)
      ERR_RETURN(trans->getNdbError());
    
    first= op;
    if ((res= set_primary_key_from_record(op, record)))
      ERR_RETURN(trans->getNdbError());
  }
  /*
   * Fetch any rows with colliding unique indexes
   */
  KEY* key_info;
  KEY_PART_INFO *key_part, *end;
  for (i= 0, key_info= table->key_info; i < table->s->keys; i++, key_info++)
  {
    if (i != table->s->primary_key &&
        key_info->flags & HA_NOSAME)
    {
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712
      /*
        A unique index is defined on table.
        We cannot look up a NULL field value in a unique index. But since
        keys with NULLs are not indexed, such rows cannot conflict anyway, so
        we just skip the index in this case.
      */
      if (check_null_in_record(key_info, record))
      {
	DBUG_PRINT("info", ("skipping check for key with NULL"));
        continue;
      } 
1713 1714 1715 1716 1717 1718 1719 1720
      NdbIndexOperation *iop;
      NDBINDEX *unique_index = (NDBINDEX *) m_index[i].unique_index;
      key_part= key_info->key_part;
      end= key_part + key_info->key_parts;
      if (!(iop= trans->getNdbIndexOperation(unique_index,
                                             (const NDBTAB *) m_table)) ||
          iop->readTuple(lm) != 0)
        ERR_RETURN(trans->getNdbError());
1721

1722 1723 1724 1725 1726 1727 1728 1729
      if (!first)
        first= iop;
      if ((res= set_index_key_from_record(iop, record, i)))
        ERR_RETURN(trans->getNdbError());
    }
  }
  last= trans->getLastDefinedOperation();
  if (first)
1730
    res= execute_no_commit_ie(this,trans,false);
1731 1732 1733 1734 1735 1736 1737 1738
  else
  {
    // Table has no keys
    table->status= STATUS_NOT_FOUND;
    DBUG_RETURN(HA_ERR_KEY_NOT_FOUND);
  }
  if (check_all_operations_for_error(trans, first, last, 
                                     HA_ERR_KEY_NOT_FOUND))
1739 1740 1741 1742
  {
    table->status= STATUS_NOT_FOUND;
    DBUG_RETURN(ndb_err(trans));
  } 
1743 1744 1745 1746
  else
  {
    DBUG_PRINT("info", ("m_dupkey %d", m_dupkey));
  }
1747 1748
  DBUG_RETURN(0);
}
1749

1750 1751 1752 1753 1754
/*
  Read one record from NDB using unique secondary index
*/

int ha_ndbcluster::unique_index_read(const byte *key,
1755
                                     uint key_len, byte *buf)
1756
{
1757
  int res;
1758
  NdbTransaction *trans= m_active_trans;
1759
  NdbIndexOperation *op;
1760
  DBUG_ENTER("ha_ndbcluster::unique_index_read");
1761 1762 1763
  DBUG_PRINT("enter", ("key_len: %u, index: %u", key_len, active_index));
  DBUG_DUMP("key", (char*)key, key_len);
  
1764 1765
  NdbOperation::LockMode lm=
    (NdbOperation::LockMode)get_ndb_lock_type(m_lock.type);
1766
  if (!(op= trans->getNdbIndexOperation((NDBINDEX *) 
1767
                                        m_index[active_index].unique_index, 
joreland@mysql.com's avatar
joreland@mysql.com committed
1768
                                        (const NDBTAB *) m_table)) ||
1769
      op->readTuple(lm) != 0)
1770 1771 1772
    ERR_RETURN(trans->getNdbError());
  
  // Set secondary index key(s)
1773
  if ((res= set_index_key(op, table->key_info + active_index, key)))
1774 1775
    DBUG_RETURN(res);
  
1776
  if ((res= define_read_attrs(buf, op)))
1777
    DBUG_RETURN(res);
1778

1779
  if (execute_no_commit_ie(this,trans,false) != 0) 
1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
  {
    table->status= STATUS_NOT_FOUND;
    DBUG_RETURN(ndb_err(trans));
  }
  // The value have now been fetched from NDB
  unpack_record(buf);
  table->status= 0;
  DBUG_RETURN(0);
}

1790
inline int ha_ndbcluster::fetch_next(NdbScanOperation* cursor)
1791 1792
{
  DBUG_ENTER("fetch_next");
1793
  int local_check;
1794
  NdbTransaction *trans= m_active_trans;
1795
  
1796 1797 1798 1799 1800 1801 1802 1803
    if (m_lock_tuple)
  {
    /*
      Lock level m_lock.type either TL_WRITE_ALLOW_WRITE
      (SELECT FOR UPDATE) or TL_READ_WITH_SHARED_LOCKS (SELECT
      LOCK WITH SHARE MODE) and row was not explictly unlocked 
      with unlock_row() call
    */
1804
      NdbConnection *con_trans= m_active_trans;
1805 1806 1807 1808 1809 1810
      NdbOperation *op;
      // Lock row
      DBUG_PRINT("info", ("Keeping lock on scanned row"));
      
      if (!(op= m_active_cursor->lockCurrentTuple()))
      {
1811
        /* purecov: begin inspected */
1812
	m_lock_tuple= false;
1813 1814
	ERR_RETURN(con_trans->getNdbError());
        /* purecov: end */    
1815 1816 1817 1818 1819 1820 1821
      }
      m_ops_pending++;
  }
  m_lock_tuple= false;

  bool contact_ndb= m_lock.type < TL_WRITE_ALLOW_WRITE &&
                    m_lock.type != TL_READ_WITH_SHARED_LOCKS;
1822 1823
  do {
    DBUG_PRINT("info", ("Call nextResult, contact_ndb: %d", contact_ndb));
pekka@mysql.com's avatar
pekka@mysql.com committed
1824 1825 1826
    /*
      We can only handle one tuple with blobs at a time.
    */
1827
    if (m_ops_pending && m_blobs_pending)
pekka@mysql.com's avatar
pekka@mysql.com committed
1828
    {
1829
      if (execute_no_commit(this,trans,false) != 0)
1830
        DBUG_RETURN(ndb_err(trans));
1831 1832
      m_ops_pending= 0;
      m_blobs_pending= FALSE;
pekka@mysql.com's avatar
pekka@mysql.com committed
1833
    }
1834
    
1835
    if ((local_check= cursor->nextResult(contact_ndb, m_force_send)) == 0)
1836
    {
1837 1838 1839 1840 1841 1842 1843
      /*
	Explicitly lock tuple if "select for update" or
	"select lock in share mode"
      */
      m_lock_tuple= (m_lock.type == TL_WRITE_ALLOW_WRITE
		     || 
		     m_lock.type == TL_READ_WITH_SHARED_LOCKS);
1844 1845
      DBUG_RETURN(0);
    } 
1846
    else if (local_check == 1 || local_check == 2)
1847 1848 1849
    {
      // 1: No more records
      // 2: No more cached records
1850
      
1851
      /*
1852 1853 1854
        Before fetching more rows and releasing lock(s),
        all pending update or delete operations should 
        be sent to NDB
1855
      */
1856
      DBUG_PRINT("info", ("ops_pending: %ld", (long) m_ops_pending));    
1857
      if (m_ops_pending)
1858
      {
1859 1860
        if (m_transaction_on)
        {
1861
          if (execute_no_commit(this,trans,false) != 0)
1862 1863 1864 1865 1866 1867
            DBUG_RETURN(-1);
        }
        else
        {
          if  (execute_commit(this,trans) != 0)
            DBUG_RETURN(-1);
1868
          if (trans->restart() != 0)
1869 1870 1871 1872 1873 1874
          {
            DBUG_ASSERT(0);
            DBUG_RETURN(-1);
          }
        }
        m_ops_pending= 0;
1875
      }
1876
      contact_ndb= (local_check == 2);
1877
    }
1878 1879 1880 1881
    else
    {
      DBUG_RETURN(-1);
    }
1882
  } while (local_check == 2);
1883

1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894
  DBUG_RETURN(1);
}

/*
  Get the next record of a started scan. Try to fetch
  it locally from NdbApi cached records if possible, 
  otherwise ask NDB for more.

  NOTE
  If this is a update/delete make sure to not contact 
  NDB before any pending ops have been sent to NDB.
1895

1896 1897 1898 1899 1900 1901 1902
*/

inline int ha_ndbcluster::next_result(byte *buf)
{  
  int res;
  DBUG_ENTER("next_result");
    
1903 1904 1905
  if (!m_active_cursor)
    DBUG_RETURN(HA_ERR_END_OF_FILE);
  
1906
  if ((res= fetch_next(m_active_cursor)) == 0)
1907 1908 1909 1910 1911 1912 1913
  {
    DBUG_PRINT("info", ("One more record found"));    
    
    unpack_record(buf);
    table->status= 0;
    DBUG_RETURN(0);
  }
1914
  else if (res == 1)
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925
  {
    // No more records
    table->status= STATUS_NOT_FOUND;
    
    DBUG_PRINT("info", ("No more records"));
    DBUG_RETURN(HA_ERR_END_OF_FILE);
  }
  else
  {
    DBUG_RETURN(ndb_err(m_active_trans));
  }
1926 1927
}

1928
/*
1929
  Set bounds for ordered index scan.
1930 1931
*/

joreland@mysql.com's avatar
joreland@mysql.com committed
1932
int ha_ndbcluster::set_bounds(NdbIndexScanOperation *op,
1933 1934
                              const key_range *keys[2],
                              uint range_no)
1935
{
1936 1937 1938 1939
  const KEY *const key_info= table->key_info + active_index;
  const uint key_parts= key_info->key_parts;
  uint key_tot_len[2];
  uint tot_len;
1940
  uint i, j;
1941 1942

  DBUG_ENTER("set_bounds");
1943
  DBUG_PRINT("info", ("key_parts=%d", key_parts));
1944

1945
  for (j= 0; j <= 1; j++)
1946
  {
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959
    const key_range *key= keys[j];
    if (key != NULL)
    {
      // for key->flag see ha_rkey_function
      DBUG_PRINT("info", ("key %d length=%d flag=%d",
                          j, key->length, key->flag));
      key_tot_len[j]= key->length;
    }
    else
    {
      DBUG_PRINT("info", ("key %d not present", j));
      key_tot_len[j]= 0;
    }
1960 1961
  }
  tot_len= 0;
1962

1963 1964 1965 1966
  for (i= 0; i < key_parts; i++)
  {
    KEY_PART_INFO *key_part= &key_info->key_part[i];
    Field *field= key_part->field;
1967
#ifndef DBUG_OFF
1968
    uint part_len= key_part->length;
1969
#endif
1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
    uint part_store_len= key_part->store_length;
    // Info about each key part
    struct part_st {
      bool part_last;
      const key_range *key;
      const byte *part_ptr;
      bool part_null;
      int bound_type;
      const char* bound_ptr;
    };
    struct part_st part[2];

    for (j= 0; j <= 1; j++)
    {
1984
      struct part_st &p= part[j];
1985 1986 1987 1988 1989 1990 1991
      p.key= NULL;
      p.bound_type= -1;
      if (tot_len < key_tot_len[j])
      {
        p.part_last= (tot_len + part_store_len >= key_tot_len[j]);
        p.key= keys[j];
        p.part_ptr= &p.key->key[tot_len];
joreland@mysql.com's avatar
joreland@mysql.com committed
1992
        p.part_null= key_part->null_bit && *p.part_ptr;
1993
        p.bound_ptr= (const char *)
joreland@mysql.com's avatar
joreland@mysql.com committed
1994
          p.part_null ? 0 : key_part->null_bit ? p.part_ptr + 1 : p.part_ptr;
1995 1996 1997 1998 1999 2000 2001 2002

        if (j == 0)
        {
          switch (p.key->flag)
          {
            case HA_READ_KEY_EXACT:
              p.bound_type= NdbIndexScanOperation::BoundEQ;
              break;
2003
            // ascending
2004 2005 2006 2007 2008 2009 2010 2011 2012
            case HA_READ_KEY_OR_NEXT:
              p.bound_type= NdbIndexScanOperation::BoundLE;
              break;
            case HA_READ_AFTER_KEY:
              if (! p.part_last)
                p.bound_type= NdbIndexScanOperation::BoundLE;
              else
                p.bound_type= NdbIndexScanOperation::BoundLT;
              break;
2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025
            // descending
            case HA_READ_PREFIX_LAST:           // weird
              p.bound_type= NdbIndexScanOperation::BoundEQ;
              break;
            case HA_READ_PREFIX_LAST_OR_PREV:   // weird
              p.bound_type= NdbIndexScanOperation::BoundGE;
              break;
            case HA_READ_BEFORE_KEY:
              if (! p.part_last)
                p.bound_type= NdbIndexScanOperation::BoundGE;
              else
                p.bound_type= NdbIndexScanOperation::BoundGT;
              break;
2026 2027 2028 2029 2030 2031 2032
            default:
              break;
          }
        }
        if (j == 1) {
          switch (p.key->flag)
          {
2033
            // ascending
2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044
            case HA_READ_BEFORE_KEY:
              if (! p.part_last)
                p.bound_type= NdbIndexScanOperation::BoundGE;
              else
                p.bound_type= NdbIndexScanOperation::BoundGT;
              break;
            case HA_READ_AFTER_KEY:     // weird
              p.bound_type= NdbIndexScanOperation::BoundGE;
              break;
            default:
              break;
2045
            // descending strangely sets no end key
2046 2047
          }
        }
2048

2049 2050 2051
        if (p.bound_type == -1)
        {
          DBUG_PRINT("error", ("key %d unknown flag %d", j, p.key->flag));
2052
          DBUG_ASSERT(FALSE);
2053
          // Stop setting bounds but continue with what we have
2054
          DBUG_RETURN(op->end_of_bound(range_no));
2055 2056 2057
        }
      }
    }
2058

2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
    // Seen with e.g. b = 1 and c > 1
    if (part[0].bound_type == NdbIndexScanOperation::BoundLE &&
        part[1].bound_type == NdbIndexScanOperation::BoundGE &&
        memcmp(part[0].part_ptr, part[1].part_ptr, part_store_len) == 0)
    {
      DBUG_PRINT("info", ("replace LE/GE pair by EQ"));
      part[0].bound_type= NdbIndexScanOperation::BoundEQ;
      part[1].bound_type= -1;
    }
    // Not seen but was in previous version
    if (part[0].bound_type == NdbIndexScanOperation::BoundEQ &&
        part[1].bound_type == NdbIndexScanOperation::BoundGE &&
        memcmp(part[0].part_ptr, part[1].part_ptr, part_store_len) == 0)
    {
      DBUG_PRINT("info", ("remove GE from EQ/GE pair"));
      part[1].bound_type= -1;
    }
2076

2077 2078
    for (j= 0; j <= 1; j++)
    {
2079
      struct part_st &p= part[j];
2080 2081 2082 2083 2084 2085 2086 2087 2088
      // Set bound if not done with this key
      if (p.key != NULL)
      {
        DBUG_PRINT("info", ("key %d:%d offset=%d length=%d last=%d bound=%d",
                            j, i, tot_len, part_len, p.part_last, p.bound_type));
        DBUG_DUMP("info", (const char*)p.part_ptr, part_store_len);

        // Set bound if not cancelled via type -1
        if (p.bound_type != -1)
2089
        {
pekka@mysql.com's avatar
pekka@mysql.com committed
2090 2091 2092
          const char* ptr= p.bound_ptr;
          char buf[256];
          shrink_varchar(field, ptr, buf);
tomas@poseidon.ndb.mysql.com's avatar
Merge  
tomas@poseidon.ndb.mysql.com committed
2093
          if (op->setBound(i, p.bound_type, ptr))
2094
            ERR_RETURN(op->getNdbError());
2095
        }
2096 2097 2098 2099
      }
    }

    tot_len+= part_store_len;
2100
  }
2101
  DBUG_RETURN(op->end_of_bound(range_no));
2102 2103
}

2104
/*
2105
  Start ordered index scan in NDB
2106 2107
*/

2108
int ha_ndbcluster::ordered_index_scan(const key_range *start_key,
2109 2110
                                      const key_range *end_key,
                                      bool sorted, bool descending, byte* buf)
2111
{  
2112
  int res;
joreland@mysql.com's avatar
joreland@mysql.com committed
2113
  bool restart;
2114
  NdbTransaction *trans= m_active_trans;
joreland@mysql.com's avatar
joreland@mysql.com committed
2115
  NdbIndexScanOperation *op;
2116

2117 2118 2119
  DBUG_ENTER("ha_ndbcluster::ordered_index_scan");
  DBUG_PRINT("enter", ("index: %u, sorted: %d, descending: %d",
             active_index, sorted, descending));  
2120
  DBUG_PRINT("enter", ("Starting new ordered scan on %s", m_tabname));
pekka@mysql.com's avatar
pekka@mysql.com committed
2121

2122 2123
  // Check that sorted seems to be initialised
  DBUG_ASSERT(sorted == 0 || sorted == 1);
2124
  
2125
  if (m_active_cursor == 0)
joreland@mysql.com's avatar
joreland@mysql.com committed
2126
  {
2127
    restart= FALSE;
joreland@mysql.com's avatar
joreland@mysql.com committed
2128 2129
    NdbOperation::LockMode lm=
      (NdbOperation::LockMode)get_ndb_lock_type(m_lock.type);
2130
    bool need_pk = (lm == NdbOperation::LM_Read);
joreland@mysql.com's avatar
joreland@mysql.com committed
2131
    if (!(op= trans->getNdbIndexScanOperation((NDBINDEX *)
2132 2133
                                              m_index[active_index].index, 
                                              (const NDBTAB *) m_table)) ||
2134
        op->readTuples(lm, 0, parallelism, sorted, descending, false, need_pk))
joreland@mysql.com's avatar
joreland@mysql.com committed
2135
      ERR_RETURN(trans->getNdbError());
2136
    m_active_cursor= op;
joreland@mysql.com's avatar
joreland@mysql.com committed
2137
  } else {
2138
    restart= TRUE;
2139
    op= (NdbIndexScanOperation*)m_active_cursor;
joreland@mysql.com's avatar
joreland@mysql.com committed
2140 2141 2142
    
    DBUG_ASSERT(op->getSorted() == sorted);
    DBUG_ASSERT(op->getLockMode() == 
2143
                (NdbOperation::LockMode)get_ndb_lock_type(m_lock.type));
2144
    if (op->reset_bounds(m_force_send))
joreland@mysql.com's avatar
joreland@mysql.com committed
2145 2146
      DBUG_RETURN(ndb_err(m_active_trans));
  }
2147
  
2148
  {
2149
    const key_range *keys[2]= { start_key, end_key };
2150 2151 2152
    res= set_bounds(op, keys);
    if (res)
      DBUG_RETURN(res);
2153
  }
2154

2155
  if (!restart && m_cond && m_cond->generate_scan_filter(op))
2156
    DBUG_RETURN(ndb_err(trans));
2157
  
2158
  if (!restart && (res= define_read_attrs(buf, op)))
2159
  {
2160
    DBUG_RETURN(res);
joreland@mysql.com's avatar
joreland@mysql.com committed
2161
  }
2162

2163
  if (execute_no_commit(this,trans,false) != 0)
2164 2165 2166 2167
    DBUG_RETURN(ndb_err(trans));
  
  DBUG_RETURN(next_result(buf));
}
2168

2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193
/*
  Unique index scan in NDB (full table scan with scan filter)
 */

int ha_ndbcluster::unique_index_scan(const KEY* key_info, 
				     const byte *key, 
				     uint key_len,
				     byte *buf)
{
  int res;
  NdbScanOperation *op;
  NdbTransaction *trans= m_active_trans;

  DBUG_ENTER("unique_index_scan");  
  DBUG_PRINT("enter", ("Starting new scan on %s", m_tabname));

  NdbOperation::LockMode lm=
    (NdbOperation::LockMode)get_ndb_lock_type(m_lock.type);
  bool need_pk = (lm == NdbOperation::LM_Read);
  if (!(op=trans->getNdbScanOperation((const NDBTAB *) m_table)) ||
      op->readTuples(lm, 
		     (need_pk)?NdbScanOperation::SF_KeyInfo:0, 
		     parallelism))
    ERR_RETURN(trans->getNdbError());
  m_active_cursor= op;
2194 2195 2196 2197 2198 2199 2200 2201
  if (!m_cond)
    m_cond= new ha_ndbcluster_cond;
  if (!m_cond)
  {
    my_errno= HA_ERR_OUT_OF_MEM;
    DBUG_RETURN(my_errno);
  }       
  if (m_cond->generate_scan_filter_from_key(op, key_info, key, key_len, buf))
2202 2203 2204 2205 2206 2207 2208 2209 2210 2211
    DBUG_RETURN(ndb_err(trans));
  if ((res= define_read_attrs(buf, op)))
    DBUG_RETURN(res);

  if (execute_no_commit(this,trans,false) != 0)
    DBUG_RETURN(ndb_err(trans));
  DBUG_PRINT("exit", ("Scan started successfully"));
  DBUG_RETURN(next_result(buf));
}

2212
/*
2213
  Start full table scan in NDB
2214 2215 2216 2217
 */

int ha_ndbcluster::full_table_scan(byte *buf)
{
2218
  int res;
2219
  NdbScanOperation *op;
2220
  NdbTransaction *trans= m_active_trans;
2221 2222 2223 2224

  DBUG_ENTER("full_table_scan");  
  DBUG_PRINT("enter", ("Starting new scan on %s", m_tabname));

2225 2226
  NdbOperation::LockMode lm=
    (NdbOperation::LockMode)get_ndb_lock_type(m_lock.type);
2227
  bool need_pk = (lm == NdbOperation::LM_Read);
2228
  if (!(op=trans->getNdbScanOperation((const NDBTAB *) m_table)) ||
2229 2230 2231
      op->readTuples(lm, 
		     (need_pk)?NdbScanOperation::SF_KeyInfo:0, 
		     parallelism))
2232
    ERR_RETURN(trans->getNdbError());
2233
  m_active_cursor= op;
2234
  if (m_cond && m_cond->generate_scan_filter(op))
2235
    DBUG_RETURN(ndb_err(trans));
2236
  if ((res= define_read_attrs(buf, op)))
2237 2238
    DBUG_RETURN(res);

2239
  if (execute_no_commit(this,trans,false) != 0)
2240 2241 2242
    DBUG_RETURN(ndb_err(trans));
  DBUG_PRINT("exit", ("Scan started successfully"));
  DBUG_RETURN(next_result(buf));
2243 2244
}

2245 2246 2247 2248 2249
/*
  Insert one record into NDB
*/
int ha_ndbcluster::write_row(byte *record)
{
mskold@mysql.com's avatar
mskold@mysql.com committed
2250
  bool has_auto_increment;
2251
  uint i;
2252
  NdbTransaction *trans= m_active_trans;
2253 2254
  NdbOperation *op;
  int res;
2255
  THD *thd= table->in_use;
2256
  DBUG_ENTER("write_row");
2257

2258 2259
  has_auto_increment= (table->next_number_field && record == table->record[0]);
  if (table->s->primary_key != MAX_KEY)
2260
  {
2261 2262 2263 2264 2265
    /*
     * Increase any auto_incremented primary key
     */
    if (has_auto_increment) 
    {
2266
      int error;
2267 2268
      
      m_skip_auto_increment= FALSE;
2269 2270
      if ((error= update_auto_increment()))
        DBUG_RETURN(error);
2271 2272 2273 2274 2275 2276 2277 2278 2279
      /* Ensure that handler is always called for auto_increment values */
      thd->next_insert_id= 0;
      m_skip_auto_increment= !auto_increment_column_changed;
    }
  }
  
  /*
   * If IGNORE the ignore constraint violations on primary and unique keys
   */
2280
  if (!m_use_write && m_ignore_dup_key)
2281
  {
2282 2283 2284 2285 2286
    /*
      compare if expression with that in start_bulk_insert()
      start_bulk_insert will set parameters to ensure that each
      write_row is committed individually
    */
2287
    int peek_res= peek_indexed_rows(record, true);
2288 2289 2290 2291 2292 2293 2294
    
    if (!peek_res) 
    {
      DBUG_RETURN(HA_ERR_FOUND_DUPP_KEY);
    }
    if (peek_res != HA_ERR_KEY_NOT_FOUND)
      DBUG_RETURN(peek_res);
2295
  }
2296

2297
  statistic_increment(thd->status_var.ha_write_count, &LOCK_status);
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
2298 2299
  if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_INSERT)
    table->timestamp_field->set_time();
2300

joreland@mysql.com's avatar
joreland@mysql.com committed
2301
  if (!(op= trans->getNdbOperation((const NDBTAB *) m_table)))
2302 2303 2304 2305 2306 2307
    ERR_RETURN(trans->getNdbError());

  res= (m_use_write) ? op->writeTuple() :op->insertTuple(); 
  if (res != 0)
    ERR_RETURN(trans->getNdbError());  
 
2308
  if (table->s->primary_key == MAX_KEY) 
2309 2310
  {
    // Table has hidden primary key
2311
    Ndb *ndb= get_ndb();
2312
    Uint64 auto_value;
2313
    uint retries= NDB_AUTO_INCREMENT_RETRIES;
2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329
    int retry_sleep= 30; /* 30 milliseconds, transaction */
    for (;;)
    {
      if (ndb->getAutoIncrementValue((const NDBTAB *) m_table,
                                     auto_value, 1) == -1)
      {
        if (--retries &&
            ndb->getNdbError().status == NdbError::TemporaryError);
        {
          my_sleep(retry_sleep);
          continue;
        }
        ERR_RETURN(ndb->getNdbError());
      }
      break;
    }
2330
    if (set_hidden_key(op, table->s->fields, (const byte*)&auto_value))
2331 2332 2333 2334
      ERR_RETURN(op->getNdbError());
  } 
  else 
  {
2335
    if ((res= set_primary_key_from_record(op, record)))
2336
      return res;  
2337 2338 2339
  }

  // Set non-key attribute(s)
2340
  bool set_blob_value= FALSE;
2341
  for (i= 0; i < table->s->fields; i++) 
2342 2343 2344
  {
    Field *field= table->field[i];
    if (!(field->flags & PRI_KEY_FLAG) &&
2345
        set_ndb_value(op, field, i, &set_blob_value))
2346
    {
2347
      m_skip_auto_increment= TRUE;
2348
      ERR_RETURN(op->getNdbError());
2349
    }
2350 2351
  }

2352 2353
  m_rows_changed++;

2354 2355 2356 2357 2358 2359 2360
  /*
    Execute write operation
    NOTE When doing inserts with many values in 
    each INSERT statement it should not be necessary
    to NoCommit the transaction between each row.
    Find out how this is detected!
  */
2361
  m_rows_inserted++;
2362
  no_uncommitted_rows_update(1);
2363
  m_bulk_insert_not_flushed= TRUE;
2364
  if ((m_rows_to_insert == (ha_rows) 1) || 
2365
      ((m_rows_inserted % m_bulk_insert_rows) == 0) ||
2366
      m_primary_key_update ||
2367
      set_blob_value)
2368 2369 2370
  {
    // Send rows to NDB
    DBUG_PRINT("info", ("Sending inserts to NDB, "\
2371 2372
                        "rows_inserted:%d, bulk_insert_rows: %d", 
                        (int)m_rows_inserted, (int)m_bulk_insert_rows));
2373

2374
    m_bulk_insert_not_flushed= FALSE;
2375
    if (m_transaction_on)
2376
    {
2377
      if (execute_no_commit(this,trans,false) != 0)
2378
      {
2379 2380 2381
        m_skip_auto_increment= TRUE;
        no_uncommitted_rows_execute_failure();
        DBUG_RETURN(ndb_err(trans));
2382
      }
2383 2384
    }
    else
2385
    {
2386
      if (execute_commit(this,trans) != 0)
2387
      {
2388 2389 2390
        m_skip_auto_increment= TRUE;
        no_uncommitted_rows_execute_failure();
        DBUG_RETURN(ndb_err(trans));
2391
      }
2392
      if (trans->restart() != 0)
2393
      {
2394 2395
        DBUG_ASSERT(0);
        DBUG_RETURN(-1);
2396
      }
2397
    }
2398
  }
2399
  if ((has_auto_increment) && (m_skip_auto_increment))
mskold@mysql.com's avatar
mskold@mysql.com committed
2400
  {
2401
    Ndb *ndb= get_ndb();
2402
    Uint64 next_val= (Uint64) table->next_number_field->val_int() + 1;
2403
#ifndef DBUG_OFF
2404
    char buff[22];
mskold@mysql.com's avatar
mskold@mysql.com committed
2405
    DBUG_PRINT("info", 
2406 2407
               ("Trying to set next auto increment value to %s",
                llstr(next_val, buff)));
2408
#endif
2409
    if (ndb->setAutoIncrementValue((const NDBTAB *) m_table, next_val, TRUE)
2410
        == -1)
2411
      ERR_RETURN(ndb->getNdbError());
2412
  }
2413
  m_skip_auto_increment= TRUE;
2414

2415 2416 2417 2418 2419 2420 2421
  DBUG_RETURN(0);
}


/* Compare if a key in a row has changed */

int ha_ndbcluster::key_cmp(uint keynr, const byte * old_row,
2422
                           const byte * new_row)
2423 2424 2425 2426 2427 2428 2429 2430 2431
{
  KEY_PART_INFO *key_part=table->key_info[keynr].key_part;
  KEY_PART_INFO *end=key_part+table->key_info[keynr].key_parts;

  for (; key_part != end ; key_part++)
  {
    if (key_part->null_bit)
    {
      if ((old_row[key_part->null_offset] & key_part->null_bit) !=
2432 2433
          (new_row[key_part->null_offset] & key_part->null_bit))
        return 1;
2434
    }
2435
    if (key_part->key_part_flag & (HA_BLOB_PART | HA_VAR_LENGTH_PART))
2436 2437 2438
    {

      if (key_part->field->cmp_binary((char*) (old_row + key_part->offset),
2439 2440 2441
                                      (char*) (new_row + key_part->offset),
                                      (ulong) key_part->length))
        return 1;
2442 2443 2444 2445
    }
    else
    {
      if (memcmp(old_row+key_part->offset, new_row+key_part->offset,
2446 2447
                 key_part->length))
        return 1;
2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459
    }
  }
  return 0;
}

/*
  Update one record in NDB using primary key
*/

int ha_ndbcluster::update_row(const byte *old_data, byte *new_data)
{
  THD *thd= current_thd;
2460
  NdbTransaction *trans= m_active_trans;
2461
  NdbScanOperation* cursor= m_active_cursor;
2462 2463
  NdbOperation *op;
  uint i;
2464 2465
  bool pk_update= (table->s->primary_key != MAX_KEY &&
		   key_cmp(table->s->primary_key, old_data, new_data));
2466 2467
  DBUG_ENTER("update_row");
  
2468
  /*
2469 2470
   * If IGNORE the ignore constraint violations on primary and unique keys,
   * but check that it is not part of INSERT ... ON DUPLICATE KEY UPDATE
2471
   */
2472
  if (m_ignore_dup_key && thd->lex->sql_command == SQLCOM_UPDATE)
2473
  {
2474
    int peek_res= peek_indexed_rows(new_data, pk_update);
2475 2476 2477 2478 2479 2480 2481 2482 2483
    
    if (!peek_res) 
    {
      DBUG_RETURN(HA_ERR_FOUND_DUPP_KEY);
    }
    if (peek_res != HA_ERR_KEY_NOT_FOUND)
      DBUG_RETURN(peek_res);
  }

2484
  statistic_increment(thd->status_var.ha_update_count, &LOCK_status);
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
2485
  if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_UPDATE)
2486
  {
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
2487
    table->timestamp_field->set_time();
2488 2489 2490
    // Set query_id so that field is really updated
    table->timestamp_field->query_id= thd->query_id;
  }
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
2491

2492
  /* Check for update of primary key for special handling */  
2493
  if (pk_update)
2494
  {
2495
    int read_res, insert_res, delete_res, undo_res;
2496

2497
    DBUG_PRINT("info", ("primary key update, doing pk read+delete+insert"));
2498
    // Get all old fields, since we optimize away fields not in query
2499
    read_res= complemented_pk_read(old_data, new_data);
2500 2501 2502 2503 2504
    if (read_res)
    {
      DBUG_PRINT("info", ("pk read failed"));
      DBUG_RETURN(read_res);
    }
2505
    // Delete old row
2506
    m_primary_key_update= TRUE;
2507
    delete_res= delete_row(old_data);
2508
    m_primary_key_update= FALSE;
2509 2510 2511
    if (delete_res)
    {
      DBUG_PRINT("info", ("delete failed"));
2512
      DBUG_RETURN(delete_res);
2513
    }     
2514 2515
    // Insert new row
    DBUG_PRINT("info", ("delete succeded"));
2516
    m_primary_key_update= TRUE;
2517
    insert_res= write_row(new_data);
2518
    m_primary_key_update= FALSE;
2519 2520 2521 2522 2523
    if (insert_res)
    {
      DBUG_PRINT("info", ("insert failed"));
      if (trans->commitStatus() == NdbConnection::Started)
      {
2524
        // Undo delete_row(old_data)
2525
        m_primary_key_update= TRUE;
2526 2527 2528 2529 2530 2531
        undo_res= write_row((byte *)old_data);
        if (undo_res)
          push_warning(current_thd, 
                       MYSQL_ERROR::WARN_LEVEL_WARN, 
                       undo_res, 
                       "NDB failed undoing delete at primary key update");
2532 2533 2534 2535 2536
        m_primary_key_update= FALSE;
      }
      DBUG_RETURN(insert_res);
    }
    DBUG_PRINT("info", ("delete+insert succeeded"));
2537
    DBUG_RETURN(0);
2538
  }
2539

2540
  if (cursor)
2541
  {
2542 2543 2544 2545 2546 2547 2548 2549
    /*
      We are scanning records and want to update the record
      that was just found, call updateTuple on the cursor 
      to take over the lock to a new update operation
      And thus setting the primary key of the record from 
      the active record in cursor
    */
    DBUG_PRINT("info", ("Calling updateTuple on cursor"));
2550
    if (!(op= cursor->updateCurrentTuple()))
2551
      ERR_RETURN(trans->getNdbError());
2552
    m_lock_tuple= false;
2553
    m_ops_pending++;
2554
    if (uses_blob_value(FALSE))
2555
      m_blobs_pending= TRUE;
2556 2557 2558
  }
  else
  {  
joreland@mysql.com's avatar
joreland@mysql.com committed
2559
    if (!(op= trans->getNdbOperation((const NDBTAB *) m_table)) ||
2560
        op->updateTuple() != 0)
2561 2562
      ERR_RETURN(trans->getNdbError());  
    
2563
    if (table->s->primary_key == MAX_KEY) 
2564 2565 2566 2567 2568
    {
      // This table has no primary key, use "hidden" primary key
      DBUG_PRINT("info", ("Using hidden key"));
      
      // Require that the PK for this record has previously been 
2569 2570
      // read into m_ref
      DBUG_DUMP("key", m_ref, NDB_HIDDEN_PRIMARY_KEY_LENGTH);
2571
      
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
2572
      if (set_hidden_key(op, table->s->fields, m_ref))
2573
        ERR_RETURN(op->getNdbError());
2574 2575 2576 2577
    } 
    else 
    {
      int res;
2578
      if ((res= set_primary_key_from_record(op, old_data)))
2579
        DBUG_RETURN(res);
2580
    }
2581 2582
  }

2583 2584
  m_rows_changed++;

2585
  // Set non-key attribute(s)
2586
  for (i= 0; i < table->s->fields; i++) 
2587 2588
  {
    Field *field= table->field[i];
2589
    if (((thd->query_id == field->query_id) || m_retrieve_all_fields) &&
2590
        (!(field->flags & PRI_KEY_FLAG)) &&
2591
        set_ndb_value(op, field, i))
2592 2593
      ERR_RETURN(op->getNdbError());
  }
2594

2595 2596 2597 2598 2599 2600 2601
  /*
    Execute update operation if we are not doing a scan for update
    and there exist UPDATE AFTER triggers
  */

  if ((!cursor || m_update_cannot_batch) && 
      execute_no_commit(this,trans,false) != 0) {
2602
    no_uncommitted_rows_execute_failure();
2603
    DBUG_RETURN(ndb_err(trans));
2604
  }
2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615
  
  DBUG_RETURN(0);
}


/*
  Delete one record from NDB, using primary key 
*/

int ha_ndbcluster::delete_row(const byte *record)
{
2616
  THD *thd= current_thd;
2617
  NdbTransaction *trans= m_active_trans;
2618
  NdbScanOperation* cursor= m_active_cursor;
2619 2620 2621
  NdbOperation *op;
  DBUG_ENTER("delete_row");

2622
  statistic_increment(thd->status_var.ha_delete_count,&LOCK_status);
2623
  m_rows_changed++;
2624

2625
  if (cursor)
2626
  {
2627
    /*
2628
      We are scanning records and want to delete the record
2629
      that was just found, call deleteTuple on the cursor 
2630
      to take over the lock to a new delete operation
2631 2632 2633 2634
      And thus setting the primary key of the record from 
      the active record in cursor
    */
    DBUG_PRINT("info", ("Calling deleteTuple on cursor"));
2635
    if (cursor->deleteCurrentTuple() != 0)
2636
      ERR_RETURN(trans->getNdbError());     
2637
    m_lock_tuple= false;
2638
    m_ops_pending++;
2639

2640 2641
    no_uncommitted_rows_update(-1);

2642
    if (!(m_primary_key_update || m_delete_cannot_batch))
2643 2644
      // If deleting from cursor, NoCommit will be handled in next_result
      DBUG_RETURN(0);
2645 2646
  }
  else
2647
  {
2648
    
joreland@mysql.com's avatar
joreland@mysql.com committed
2649
    if (!(op=trans->getNdbOperation((const NDBTAB *) m_table)) || 
2650
        op->deleteTuple() != 0)
2651 2652
      ERR_RETURN(trans->getNdbError());
    
2653 2654
    no_uncommitted_rows_update(-1);
    
2655
    if (table->s->primary_key == MAX_KEY) 
2656 2657 2658 2659
    {
      // This table has no primary key, use "hidden" primary key
      DBUG_PRINT("info", ("Using hidden key"));
      
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
2660
      if (set_hidden_key(op, table->s->fields, m_ref))
2661
        ERR_RETURN(op->getNdbError());
2662 2663 2664 2665
    } 
    else 
    {
      int res;
2666 2667
      if ((res= set_primary_key_from_record(op, record)))
        return res;  
2668
    }
2669
  }
2670

2671
  // Execute delete operation
2672
  if (execute_no_commit(this,trans,false) != 0) {
2673
    no_uncommitted_rows_execute_failure();
2674
    DBUG_RETURN(ndb_err(trans));
2675
  }
2676 2677
  DBUG_RETURN(0);
}
2678
  
2679 2680 2681 2682 2683
/*
  Unpack a record read from NDB 

  SYNOPSIS
    unpack_record()
2684
    buf                 Buffer to store read row
2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696

  NOTE
    The data for each row is read directly into the
    destination buffer. This function is primarily 
    called in order to check if any fields should be 
    set to null.
*/

void ha_ndbcluster::unpack_record(byte* buf)
{
  uint row_offset= (uint) (buf - table->record[0]);
  Field **field, **end;
pekka@mysql.com's avatar
pekka@mysql.com committed
2697
  NdbValue *value= m_value;
2698
  DBUG_ENTER("unpack_record");
2699

joreland@mysql.com's avatar
merge  
joreland@mysql.com committed
2700
  end= table->field + table->s->fields;
2701 2702
  
  // Set null flag(s)
2703
  bzero(buf, table->s->null_bytes);
joreland@mysql.com's avatar
merge  
joreland@mysql.com committed
2704
  for (field= table->field;
2705 2706 2707
       field < end;
       field++, value++)
  {
pekka@mysql.com's avatar
pekka@mysql.com committed
2708 2709 2710 2711 2712 2713
    if ((*value).ptr)
    {
      if (! ((*field)->flags & BLOB_FLAG))
      {
        if ((*value).rec->isNULL())
         (*field)->set_null(row_offset);
2714 2715 2716 2717 2718 2719
        else if ((*field)->type() == MYSQL_TYPE_BIT)
        {
          uint pack_len= (*field)->pack_length();
          if (pack_len < 5)
          {
            DBUG_PRINT("info", ("bit field H'%.8X", 
2720
                                (*value).rec->u_32_value()));
2721
            ((Field_bit *) *field)->store((longlong) 
2722 2723
                                          (*value).rec->u_32_value(),
                                          FALSE);
2724 2725 2726 2727 2728 2729
          }
          else
          {
            DBUG_PRINT("info", ("bit field H'%.8X%.8X",
                                *(Uint32 *)(*value).rec->aRef(),
                                *((Uint32 *)(*value).rec->aRef()+1)));
2730 2731 2732
#ifdef WORDS_BIGENDIAN
            /* lsw is stored first */
            Uint32 *buf= (Uint32 *)(*value).rec->aRef();
2733
            ((Field_bit *) *field)->store((((longlong)*buf)
joerg@trift2's avatar
joerg@trift2 committed
2734
                                           & 0x000000000FFFFFFFFLL)
2735 2736
                                          |
                                          ((((longlong)*(buf+1)) << 32)
joerg@trift2's avatar
joerg@trift2 committed
2737
                                           & 0xFFFFFFFF00000000LL),
2738 2739
                                          TRUE);
#else
2740
            ((Field_bit *) *field)->store((longlong)
2741
                                          (*value).rec->u_64_value(), TRUE);
2742
#endif
2743
          }
2744
        }
pekka@mysql.com's avatar
pekka@mysql.com committed
2745 2746 2747 2748
      }
      else
      {
        NdbBlob* ndb_blob= (*value).blob;
2749
        bool isNull= TRUE;
2750 2751 2752
#ifndef DBUG_OFF
        int ret= 
#endif
2753
          ndb_blob->getNull(isNull);
pekka@mysql.com's avatar
pekka@mysql.com committed
2754 2755
        DBUG_ASSERT(ret == 0);
        if (isNull)
2756
          (*field)->set_null(row_offset);
pekka@mysql.com's avatar
pekka@mysql.com committed
2757 2758
      }
    }
2759
  }
2760
  
2761 2762
#ifndef DBUG_OFF
  // Read and print all values that was fetched
2763
  if (table->s->primary_key == MAX_KEY)
2764 2765
  {
    // Table with hidden primary key
2766
    int hidden_no= table->s->fields;
2767
    char buff[22];
joreland@mysql.com's avatar
joreland@mysql.com committed
2768
    const NDBTAB *tab= (const NDBTAB *) m_table;
2769
    const NDBCOL *hidden_col= tab->getColumn(hidden_no);
2770
    const NdbRecAttr* rec= m_value[hidden_no].rec;
2771
    DBUG_ASSERT(rec);
2772
    DBUG_PRINT("hidden", ("%d: %s \"%s\"", hidden_no, 
2773
			  hidden_col->getName(),
2774
                          llstr(rec->u_64_value(), buff)));
2775
  }
2776
  //print_results();
2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791
#endif
  DBUG_VOID_RETURN;
}

/*
  Utility function to print/dump the fetched field
 */

void ha_ndbcluster::print_results()
{
  DBUG_ENTER("print_results");

#ifndef DBUG_OFF
  if (!_db_on_)
    DBUG_VOID_RETURN;
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
2792

2793
  char buf_type[MAX_FIELD_WIDTH], buf_val[MAX_FIELD_WIDTH];
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
2794
  String type(buf_type, sizeof(buf_type), &my_charset_bin);
2795
  String val(buf_val, sizeof(buf_val), &my_charset_bin);
2796
  for (uint f= 0; f < table->s->fields; f++)
2797
  {
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
2798
    /* Use DBUG_PRINT since DBUG_FILE cannot be filtered out */
2799
    char buf[2000];
2800
    Field *field;
2801
    void* ptr;
pekka@mysql.com's avatar
pekka@mysql.com committed
2802
    NdbValue value;
2803

2804
    buf[0]= 0;
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
2805
    field= table->field[f];
pekka@mysql.com's avatar
pekka@mysql.com committed
2806
    if (!(value= m_value[f]).ptr)
2807
    {
2808
      strmov(buf, "not read");
2809
      goto print_value;
2810
    }
2811

2812
    ptr= field->ptr;
pekka@mysql.com's avatar
pekka@mysql.com committed
2813 2814

    if (! (field->flags & BLOB_FLAG))
2815
    {
pekka@mysql.com's avatar
pekka@mysql.com committed
2816 2817
      if (value.rec->isNULL())
      {
2818
        strmov(buf, "NULL");
2819
        goto print_value;
pekka@mysql.com's avatar
pekka@mysql.com committed
2820
      }
2821 2822 2823 2824 2825
      type.length(0);
      val.length(0);
      field->sql_type(type);
      field->val_str(&val);
      my_snprintf(buf, sizeof(buf), "%s %s", type.c_ptr(), val.c_ptr());
pekka@mysql.com's avatar
pekka@mysql.com committed
2826 2827 2828
    }
    else
    {
2829
      NdbBlob *ndb_blob= value.blob;
2830
      bool isNull= TRUE;
pekka@mysql.com's avatar
pekka@mysql.com committed
2831
      ndb_blob->getNull(isNull);
2832 2833
      if (isNull)
        strmov(buf, "NULL");
2834
    }
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
2835

2836
print_value:
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
2837
    DBUG_PRINT("value", ("%u,%s: %s", f, field->field_name, buf));
2838 2839 2840 2841 2842 2843 2844 2845
  }
#endif
  DBUG_VOID_RETURN;
}


int ha_ndbcluster::index_init(uint index)
{
2846
  DBUG_ENTER("ha_ndbcluster::index_init");
2847
  DBUG_PRINT("enter", ("index: %u", index));
2848 2849 2850 2851 2852 2853
 /*
    Locks are are explicitly released in scan
    unless m_lock.type == TL_READ_HIGH_PRIORITY
    and no sub-sequent call to unlock_row()
   */
  m_lock_tuple= false;
2854 2855 2856 2857 2858 2859
  DBUG_RETURN(handler::index_init(index));
}


int ha_ndbcluster::index_end()
{
2860
  DBUG_ENTER("ha_ndbcluster::index_end");
2861
  DBUG_RETURN(close_scan());
2862 2863
}

2864
/**
2865
 * Check if key contains nullable columns
2866 2867 2868 2869 2870 2871
 */
static
int
check_null_in_key(const KEY* key_info, const byte *key, uint key_len)
{
  KEY_PART_INFO *curr_part, *end_part;
2872
  const byte* end_ptr= key + key_len;
2873 2874 2875 2876 2877 2878
  curr_part= key_info->key_part;
  end_part= curr_part + key_info->key_parts;
  

  for (; curr_part != end_part && key < end_ptr; curr_part++)
  {
2879
    if (curr_part->null_bit && *key)
2880 2881 2882 2883 2884 2885
      return 1;

    key += curr_part->store_length;
  }
  return 0;
}
2886 2887

int ha_ndbcluster::index_read(byte *buf,
2888 2889
                              const byte *key, uint key_len, 
                              enum ha_rkey_function find_flag)
2890
{
2891
  DBUG_ENTER("ha_ndbcluster::index_read");
2892 2893 2894
  DBUG_PRINT("enter", ("active_index: %u, key_len: %u, find_flag: %d", 
                       active_index, key_len, find_flag));

joreland@mysql.com's avatar
joreland@mysql.com committed
2895
  int error;
2896 2897
  ndb_index_type type= get_index_type(active_index);
  const KEY* key_info= table->key_info+active_index;
joreland@mysql.com's avatar
joreland@mysql.com committed
2898 2899 2900 2901 2902
  switch (type){
  case PRIMARY_KEY_ORDERED_INDEX:
  case PRIMARY_KEY_INDEX:
    if (find_flag == HA_READ_KEY_EXACT && key_info->key_length == key_len)
    {
2903
      if (m_active_cursor && (error= close_scan()))
2904
        DBUG_RETURN(error);
joreland@mysql.com's avatar
joreland@mysql.com committed
2905 2906 2907 2908 2909 2910 2911 2912 2913
      DBUG_RETURN(pk_read(key, key_len, buf));
    }
    else if (type == PRIMARY_KEY_INDEX)
    {
      DBUG_RETURN(1);
    }
    break;
  case UNIQUE_ORDERED_INDEX:
  case UNIQUE_INDEX:
2914
    if (find_flag == HA_READ_KEY_EXACT && key_info->key_length == key_len &&
2915
        !check_null_in_key(key_info, key, key_len))
joreland@mysql.com's avatar
joreland@mysql.com committed
2916
    {
2917
      if (m_active_cursor && (error= close_scan()))
2918
        DBUG_RETURN(error);
joreland@mysql.com's avatar
joreland@mysql.com committed
2919 2920 2921 2922
      DBUG_RETURN(unique_index_read(key, key_len, buf));
    }
    else if (type == UNIQUE_INDEX)
    {
2923
      DBUG_RETURN(unique_index_scan(key_info, key, key_len, buf));
joreland@mysql.com's avatar
joreland@mysql.com committed
2924 2925 2926 2927 2928 2929
    }
    break;
  case ORDERED_INDEX:
    break;
  default:
  case UNDEFINED_INDEX:
2930
    DBUG_ASSERT(FALSE);
2931
    DBUG_RETURN(1);
joreland@mysql.com's avatar
joreland@mysql.com committed
2932 2933 2934
    break;
  }
  
2935
  key_range start_key;
2936 2937 2938
  start_key.key= key;
  start_key.length= key_len;
  start_key.flag= find_flag;
2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950
  bool descending= FALSE;
  switch (find_flag) {
  case HA_READ_KEY_OR_PREV:
  case HA_READ_BEFORE_KEY:
  case HA_READ_PREFIX_LAST:
  case HA_READ_PREFIX_LAST_OR_PREV:
    descending= TRUE;
    break;
  default:
    break;
  }
  error= ordered_index_scan(&start_key, 0, TRUE, descending, buf);  
joreland@mysql.com's avatar
joreland@mysql.com committed
2951
  DBUG_RETURN(error == HA_ERR_END_OF_FILE ? HA_ERR_KEY_NOT_FOUND : error);
2952 2953 2954 2955
}


int ha_ndbcluster::index_read_idx(byte *buf, uint index_no, 
2956 2957
                              const byte *key, uint key_len, 
                              enum ha_rkey_function find_flag)
2958
{
2959
  statistic_increment(current_thd->status_var.ha_read_key_count, &LOCK_status);
2960
  DBUG_ENTER("ha_ndbcluster::index_read_idx");
2961 2962 2963 2964 2965 2966 2967 2968
  DBUG_PRINT("enter", ("index_no: %u, key_len: %u", index_no, key_len));  
  index_init(index_no);  
  DBUG_RETURN(index_read(buf, key, key_len, find_flag));
}


int ha_ndbcluster::index_next(byte *buf)
{
2969
  DBUG_ENTER("ha_ndbcluster::index_next");
2970
  statistic_increment(current_thd->status_var.ha_read_next_count,
2971
                      &LOCK_status);
2972
  DBUG_RETURN(next_result(buf));
2973 2974 2975 2976 2977
}


int ha_ndbcluster::index_prev(byte *buf)
{
2978
  DBUG_ENTER("ha_ndbcluster::index_prev");
2979
  statistic_increment(current_thd->status_var.ha_read_prev_count,
2980
                      &LOCK_status);
2981
  DBUG_RETURN(next_result(buf));
2982 2983 2984 2985 2986
}


int ha_ndbcluster::index_first(byte *buf)
{
2987
  DBUG_ENTER("ha_ndbcluster::index_first");
2988
  statistic_increment(current_thd->status_var.ha_read_first_count,
2989
                      &LOCK_status);
2990 2991 2992
  // Start the ordered index scan and fetch the first row

  // Only HA_READ_ORDER indexes get called by index_first
2993
  DBUG_RETURN(ordered_index_scan(0, 0, TRUE, FALSE, buf));
2994 2995 2996 2997 2998
}


int ha_ndbcluster::index_last(byte *buf)
{
2999
  DBUG_ENTER("ha_ndbcluster::index_last");
3000
  statistic_increment(current_thd->status_var.ha_read_last_count,&LOCK_status);
3001
  DBUG_RETURN(ordered_index_scan(0, 0, TRUE, TRUE, buf));
3002 3003
}

3004 3005 3006 3007 3008
int ha_ndbcluster::index_read_last(byte * buf, const byte * key, uint key_len)
{
  DBUG_ENTER("ha_ndbcluster::index_read_last");
  DBUG_RETURN(index_read(buf, key, key_len, HA_READ_PREFIX_LAST));
}
3009

3010 3011
inline
int ha_ndbcluster::read_range_first_to_buf(const key_range *start_key,
3012 3013 3014
                                           const key_range *end_key,
                                           bool eq_r, bool sorted,
                                           byte* buf)
3015
{
3016 3017
   ndb_index_type type= get_index_type(active_index);
KEY* key_info;
3018 3019
  int error= 1; 
  DBUG_ENTER("ha_ndbcluster::read_range_first_to_buf");
3020
  DBUG_PRINT("info", ("eq_r: %d, sorted: %d", eq_r, sorted));
3021

3022
  switch (type){
3023
  case PRIMARY_KEY_ORDERED_INDEX:
3024
  case PRIMARY_KEY_INDEX:
3025 3026
    key_info= table->key_info + active_index;
    if (start_key && 
3027 3028
        start_key->length == key_info->key_length &&
        start_key->flag == HA_READ_KEY_EXACT)
3029
    {
3030
      if (m_active_cursor && (error= close_scan()))
3031
        DBUG_RETURN(error);
3032 3033 3034
      error= pk_read(start_key->key, start_key->length, buf);      
      DBUG_RETURN(error == HA_ERR_KEY_NOT_FOUND ? HA_ERR_END_OF_FILE : error);
    }
3035
    break;
3036
  case UNIQUE_ORDERED_INDEX:
3037
  case UNIQUE_INDEX:
3038
    key_info= table->key_info + active_index;
3039
    if (start_key && start_key->length == key_info->key_length &&
3040 3041
        start_key->flag == HA_READ_KEY_EXACT && 
        !check_null_in_key(key_info, start_key->key, start_key->length))
3042
    {
3043
      if (m_active_cursor && (error= close_scan()))
3044
        DBUG_RETURN(error);
3045 3046 3047
      error= unique_index_read(start_key->key, start_key->length, buf);
      DBUG_RETURN(error == HA_ERR_KEY_NOT_FOUND ? HA_ERR_END_OF_FILE : error);
    }
3048
    else if (type == UNIQUE_INDEX)
3049 3050 3051 3052
      DBUG_RETURN(unique_index_scan(key_info, 
				    start_key->key, 
				    start_key->length, 
				    buf));
3053 3054 3055 3056
    break;
  default:
    break;
  }
3057 3058

  // Start the ordered index scan and fetch the first row
3059
  error= ordered_index_scan(start_key, end_key, sorted, FALSE, buf);
3060 3061 3062
  DBUG_RETURN(error);
}

3063

joreland@mysql.com's avatar
joreland@mysql.com committed
3064
int ha_ndbcluster::read_range_first(const key_range *start_key,
3065 3066
                                    const key_range *end_key,
                                    bool eq_r, bool sorted)
joreland@mysql.com's avatar
joreland@mysql.com committed
3067 3068 3069 3070 3071
{
  byte* buf= table->record[0];
  DBUG_ENTER("ha_ndbcluster::read_range_first");
  
  DBUG_RETURN(read_range_first_to_buf(start_key,
3072 3073 3074 3075
                                      end_key,
                                      eq_r, 
                                      sorted,
                                      buf));
joreland@mysql.com's avatar
joreland@mysql.com committed
3076 3077
}

3078
int ha_ndbcluster::read_range_next()
3079 3080 3081 3082 3083 3084
{
  DBUG_ENTER("ha_ndbcluster::read_range_next");
  DBUG_RETURN(next_result(table->record[0]));
}


3085 3086
int ha_ndbcluster::rnd_init(bool scan)
{
3087
  NdbScanOperation *cursor= m_active_cursor;
3088 3089
  DBUG_ENTER("rnd_init");
  DBUG_PRINT("enter", ("scan: %d", scan));
3090
  // Check if scan is to be restarted
mskold@mysql.com's avatar
mskold@mysql.com committed
3091 3092 3093 3094
  if (cursor)
  {
    if (!scan)
      DBUG_RETURN(1);
3095
    if (cursor->restart(m_force_send) != 0)
3096 3097 3098 3099
    {
      DBUG_ASSERT(0);
      DBUG_RETURN(-1);
    }
mskold@mysql.com's avatar
mskold@mysql.com committed
3100
  }
3101
  index_init(table->s->primary_key);
3102 3103 3104
  DBUG_RETURN(0);
}

3105 3106
int ha_ndbcluster::close_scan()
{
3107
  NdbTransaction *trans= m_active_trans;
3108 3109
  DBUG_ENTER("close_scan");

3110 3111
  m_multi_cursor= 0;
  if (!m_active_cursor && !m_multi_cursor)
3112 3113
    DBUG_RETURN(1);

3114
  NdbScanOperation *cursor= m_active_cursor ? m_active_cursor : m_multi_cursor;
3115
  
3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127
  if (m_lock_tuple)
  {
    /*
      Lock level m_lock.type either TL_WRITE_ALLOW_WRITE
      (SELECT FOR UPDATE) or TL_READ_WITH_SHARED_LOCKS (SELECT
      LOCK WITH SHARE MODE) and row was not explictly unlocked 
      with unlock_row() call
    */
      NdbOperation *op;
      // Lock row
      DBUG_PRINT("info", ("Keeping lock on scanned row"));
      
3128
      if (!(op= cursor->lockCurrentTuple()))
3129 3130 3131 3132 3133 3134
      {
	m_lock_tuple= false;
	ERR_RETURN(trans->getNdbError());
      }
      m_ops_pending++;      
  }
3135
  m_lock_tuple= false;
3136
  if (m_ops_pending)
3137 3138 3139 3140 3141
  {
    /*
      Take over any pending transactions to the 
      deleteing/updating transaction before closing the scan    
    */
3142
    DBUG_PRINT("info", ("ops_pending: %ld", (long) m_ops_pending));    
3143
    if (execute_no_commit(this,trans,false) != 0) {
3144
      no_uncommitted_rows_execute_failure();
3145
      DBUG_RETURN(ndb_err(trans));
3146
    }
3147
    m_ops_pending= 0;
3148 3149
  }
  
3150
  cursor->close(m_force_send, TRUE);
3151
  m_active_cursor= m_multi_cursor= NULL;
mskold@mysql.com's avatar
mskold@mysql.com committed
3152
  DBUG_RETURN(0);
3153
}
3154 3155 3156 3157

int ha_ndbcluster::rnd_end()
{
  DBUG_ENTER("rnd_end");
3158
  DBUG_RETURN(close_scan());
3159 3160 3161 3162 3163 3164
}


int ha_ndbcluster::rnd_next(byte *buf)
{
  DBUG_ENTER("rnd_next");
3165
  statistic_increment(current_thd->status_var.ha_read_rnd_next_count,
3166
                      &LOCK_status);
3167

3168
  if (!m_active_cursor)
3169 3170
    DBUG_RETURN(full_table_scan(buf));
  DBUG_RETURN(next_result(buf));
3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183
}


/*
  An "interesting" record has been found and it's pk 
  retrieved by calling position
  Now it's time to read the record from db once 
  again
*/

int ha_ndbcluster::rnd_pos(byte *buf, byte *pos)
{
  DBUG_ENTER("rnd_pos");
3184
  statistic_increment(current_thd->status_var.ha_read_rnd_count,
3185
                      &LOCK_status);
3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205
  // The primary key for the record is stored in pos
  // Perform a pk_read using primary key "index"
  DBUG_RETURN(pk_read(pos, ref_length, buf));  
}


/*
  Store the primary key of this record in ref 
  variable, so that the row can be retrieved again later
  using "reference" in rnd_pos
*/

void ha_ndbcluster::position(const byte *record)
{
  KEY *key_info;
  KEY_PART_INFO *key_part;
  KEY_PART_INFO *end;
  byte *buff;
  DBUG_ENTER("position");

3206
  if (table->s->primary_key != MAX_KEY) 
3207
  {
3208
    key_info= table->key_info + table->s->primary_key;
3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223
    key_part= key_info->key_part;
    end= key_part + key_info->key_parts;
    buff= ref;
    
    for (; key_part != end; key_part++) 
    {
      if (key_part->null_bit) {
        /* Store 0 if the key part is a NULL part */      
        if (record[key_part->null_offset]
            & key_part->null_bit) {
          *buff++= 1;
          continue;
        }      
        *buff++= 0;
      }
3224 3225 3226 3227

      size_t len = key_part->length;
      const byte * ptr = record + key_part->offset;
      Field *field = key_part->field;
3228
      if (field->type() ==  MYSQL_TYPE_VARCHAR)
3229
      {
3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243
        if (((Field_varstring*)field)->length_bytes == 1)
        {
          /**
           * Keys always use 2 bytes length
           */
          buff[0] = ptr[0];
          buff[1] = 0;
          memcpy(buff+2, ptr + 1, len);
        }
        else
        {
          memcpy(buff, ptr, len + 2);
        }
        len += 2;
3244 3245 3246
      }
      else
      {
3247
        memcpy(buff, ptr, len);
3248 3249
      }
      buff += len;
3250 3251 3252 3253 3254 3255
    }
  } 
  else 
  {
    // No primary key, get hidden key
    DBUG_PRINT("info", ("Getting hidden key"));
3256
#ifndef DBUG_OFF
3257
    int hidden_no= table->s->fields;
joreland@mysql.com's avatar
joreland@mysql.com committed
3258
    const NDBTAB *tab= (const NDBTAB *) m_table;  
3259 3260 3261 3262
    const NDBCOL *hidden_col= tab->getColumn(hidden_no);
    DBUG_ASSERT(hidden_col->getPrimaryKey() && 
                hidden_col->getAutoIncrement() &&
                ref_length == NDB_HIDDEN_PRIMARY_KEY_LENGTH);
3263
#endif
3264
    memcpy(ref, m_ref, ref_length);
3265 3266 3267 3268 3269 3270 3271
  }
  
  DBUG_DUMP("ref", (char*)ref, ref_length);
  DBUG_VOID_RETURN;
}


3272
int ha_ndbcluster::info(uint flag)
3273
{
3274
  int result= 0;
3275 3276 3277 3278 3279 3280 3281 3282 3283 3284
  DBUG_ENTER("info");
  DBUG_PRINT("enter", ("flag: %d", flag));
  
  if (flag & HA_STATUS_POS)
    DBUG_PRINT("info", ("HA_STATUS_POS"));
  if (flag & HA_STATUS_NO_LOCK)
    DBUG_PRINT("info", ("HA_STATUS_NO_LOCK"));
  if (flag & HA_STATUS_TIME)
    DBUG_PRINT("info", ("HA_STATUS_TIME"));
  if (flag & HA_STATUS_VARIABLE)
3285
  {
3286
    DBUG_PRINT("info", ("HA_STATUS_VARIABLE"));
3287 3288
    if (m_table_info)
    {
3289
      if (m_ha_not_exact_count)
3290
        records= 100;
3291
      else
3292
	result= records_update();
3293 3294 3295
    }
    else
    {
3296
      if ((my_errno= check_ndb_connection()))
3297
        DBUG_RETURN(my_errno);
3298
      Ndb *ndb= get_ndb();
3299
      struct Ndb_statistics stat;
3300 3301 3302 3303
      if (ndb->setDatabaseName(m_dbname))
      {
        DBUG_RETURN(my_errno= HA_ERR_OUT_OF_MEM);
      }
3304
      if (current_thd->variables.ndb_use_exact_count &&
stewart@willster.(none)'s avatar
stewart@willster.(none) committed
3305 3306
          (result= ndb_get_table_statistics(this, true, ndb, m_tabname, &stat))
          == 0)
3307
      {
3308 3309 3310
        mean_rec_length= stat.row_size;
        data_file_length= stat.fragment_memory;
        records= stat.row_count;
3311 3312 3313
      }
      else
      {
3314 3315
        mean_rec_length= 0;
        records= 100;
3316
      }
3317
    }
3318
  }
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
3319 3320 3321 3322 3323
  if (flag & HA_STATUS_CONST)
  {
    DBUG_PRINT("info", ("HA_STATUS_CONST"));
    set_rec_per_key();
  }
3324
  if (flag & HA_STATUS_ERRKEY)
3325
  {
3326
    DBUG_PRINT("info", ("HA_STATUS_ERRKEY"));
3327
    errkey= m_dupkey;
3328
  }
3329
  if (flag & HA_STATUS_AUTO)
3330
  {
3331
    DBUG_PRINT("info", ("HA_STATUS_AUTO"));
3332
    if (m_table && table->found_next_number_field)
3333 3334 3335
    {
      Ndb *ndb= get_ndb();
      
3336
      Uint64 auto_increment_value64;
3337
      if (ndb->readAutoIncrementValue((const NDBTAB *) m_table,
3338
                                      auto_increment_value64) == -1)
3339 3340 3341 3342 3343 3344
      {
        const NdbError err= ndb->getNdbError();
        sql_print_error("Error %lu in readAutoIncrementValue(): %s",
                        (ulong) err.code, err.message);
        auto_increment_value= ~(Uint64)0;
      }
3345 3346
      else
        auto_increment_value= (ulonglong)auto_increment_value64;
3347 3348
    }
  }
3349 3350 3351 3352 3353

  if(result == -1)
    result= HA_ERR_NO_CONNECTION;

  DBUG_RETURN(result);
3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368
}


int ha_ndbcluster::extra(enum ha_extra_function operation)
{
  DBUG_ENTER("extra");
  switch (operation) {
  case HA_EXTRA_NORMAL:              /* Optimize for space (def) */
    DBUG_PRINT("info", ("HA_EXTRA_NORMAL"));
    break;
  case HA_EXTRA_QUICK:                 /* Optimize for speed */
    DBUG_PRINT("info", ("HA_EXTRA_QUICK"));
    break;
  case HA_EXTRA_RESET:                 /* Reset database to after open */
    DBUG_PRINT("info", ("HA_EXTRA_RESET"));
3369
    reset();
3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438
    break;
  case HA_EXTRA_CACHE:                 /* Cash record in HA_rrnd() */
    DBUG_PRINT("info", ("HA_EXTRA_CACHE"));
    break;
  case HA_EXTRA_NO_CACHE:              /* End cacheing of records (def) */
    DBUG_PRINT("info", ("HA_EXTRA_NO_CACHE"));
    break;
  case HA_EXTRA_NO_READCHECK:          /* No readcheck on update */
    DBUG_PRINT("info", ("HA_EXTRA_NO_READCHECK"));
    break;
  case HA_EXTRA_READCHECK:             /* Use readcheck (def) */
    DBUG_PRINT("info", ("HA_EXTRA_READCHECK"));
    break;
  case HA_EXTRA_KEYREAD:               /* Read only key to database */
    DBUG_PRINT("info", ("HA_EXTRA_KEYREAD"));
    break;
  case HA_EXTRA_NO_KEYREAD:            /* Normal read of records (def) */
    DBUG_PRINT("info", ("HA_EXTRA_NO_KEYREAD"));
    break;
  case HA_EXTRA_NO_USER_CHANGE:        /* No user is allowed to write */
    DBUG_PRINT("info", ("HA_EXTRA_NO_USER_CHANGE"));
    break;
  case HA_EXTRA_KEY_CACHE:
    DBUG_PRINT("info", ("HA_EXTRA_KEY_CACHE"));
    break;
  case HA_EXTRA_NO_KEY_CACHE:
    DBUG_PRINT("info", ("HA_EXTRA_NO_KEY_CACHE"));
    break;
  case HA_EXTRA_WAIT_LOCK:            /* Wait until file is avalably (def) */
    DBUG_PRINT("info", ("HA_EXTRA_WAIT_LOCK"));
    break;
  case HA_EXTRA_NO_WAIT_LOCK:         /* If file is locked, return quickly */
    DBUG_PRINT("info", ("HA_EXTRA_NO_WAIT_LOCK"));
    break;
  case HA_EXTRA_WRITE_CACHE:           /* Use write cache in ha_write() */
    DBUG_PRINT("info", ("HA_EXTRA_WRITE_CACHE"));
    break;
  case HA_EXTRA_FLUSH_CACHE:           /* flush write_record_cache */
    DBUG_PRINT("info", ("HA_EXTRA_FLUSH_CACHE"));
    break;
  case HA_EXTRA_NO_KEYS:               /* Remove all update of keys */
    DBUG_PRINT("info", ("HA_EXTRA_NO_KEYS"));
    break;
  case HA_EXTRA_KEYREAD_CHANGE_POS:         /* Keyread, but change pos */
    DBUG_PRINT("info", ("HA_EXTRA_KEYREAD_CHANGE_POS")); /* xxxxchk -r must be used */
    break;                                  
  case HA_EXTRA_REMEMBER_POS:          /* Remember pos for next/prev */
    DBUG_PRINT("info", ("HA_EXTRA_REMEMBER_POS"));
    break;
  case HA_EXTRA_RESTORE_POS:
    DBUG_PRINT("info", ("HA_EXTRA_RESTORE_POS"));
    break;
  case HA_EXTRA_REINIT_CACHE:          /* init cache from current record */
    DBUG_PRINT("info", ("HA_EXTRA_REINIT_CACHE"));
    break;
  case HA_EXTRA_FORCE_REOPEN:          /* Datafile have changed on disk */
    DBUG_PRINT("info", ("HA_EXTRA_FORCE_REOPEN"));
    break;
  case HA_EXTRA_FLUSH:                 /* Flush tables to disk */
    DBUG_PRINT("info", ("HA_EXTRA_FLUSH"));
    break;
  case HA_EXTRA_NO_ROWS:               /* Don't write rows */
    DBUG_PRINT("info", ("HA_EXTRA_NO_ROWS"));
    break;
  case HA_EXTRA_RESET_STATE:           /* Reset positions */
    DBUG_PRINT("info", ("HA_EXTRA_RESET_STATE"));
    break;
  case HA_EXTRA_IGNORE_DUP_KEY:       /* Dup keys don't rollback everything*/
    DBUG_PRINT("info", ("HA_EXTRA_IGNORE_DUP_KEY"));
3439 3440
    DBUG_PRINT("info", ("Ignoring duplicate key"));
    m_ignore_dup_key= TRUE;
3441 3442 3443
    break;
  case HA_EXTRA_NO_IGNORE_DUP_KEY:
    DBUG_PRINT("info", ("HA_EXTRA_NO_IGNORE_DUP_KEY"));
3444
    m_ignore_dup_key= FALSE;
3445 3446
    break;
  case HA_EXTRA_RETRIEVE_ALL_COLS:    /* Retrieve all columns, not just those
3447 3448
                                         where field->query_id is the same as
                                         the current query id */
3449
    DBUG_PRINT("info", ("HA_EXTRA_RETRIEVE_ALL_COLS"));
3450
    m_retrieve_all_fields= TRUE;
3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462
    break;
  case HA_EXTRA_PREPARE_FOR_DELETE:
    DBUG_PRINT("info", ("HA_EXTRA_PREPARE_FOR_DELETE"));
    break;
  case HA_EXTRA_PREPARE_FOR_UPDATE:     /* Remove read cache if problems */
    DBUG_PRINT("info", ("HA_EXTRA_PREPARE_FOR_UPDATE"));
    break;
  case HA_EXTRA_PRELOAD_BUFFER_SIZE: 
    DBUG_PRINT("info", ("HA_EXTRA_PRELOAD_BUFFER_SIZE"));
    break;
  case HA_EXTRA_RETRIEVE_PRIMARY_KEY: 
    DBUG_PRINT("info", ("HA_EXTRA_RETRIEVE_PRIMARY_KEY"));
3463
    m_retrieve_primary_key= TRUE;
3464 3465 3466 3467 3468 3469
    break;
  case HA_EXTRA_CHANGE_KEY_TO_UNIQUE: 
    DBUG_PRINT("info", ("HA_EXTRA_CHANGE_KEY_TO_UNIQUE"));
    break;
  case HA_EXTRA_CHANGE_KEY_TO_DUP: 
    DBUG_PRINT("info", ("HA_EXTRA_CHANGE_KEY_TO_DUP"));
3470 3471
  case HA_EXTRA_KEYREAD_PRESERVE_FIELDS:
    DBUG_PRINT("info", ("HA_EXTRA_KEYREAD_PRESERVE_FIELDS"));
3472
    break;
3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485
  case HA_EXTRA_WRITE_CAN_REPLACE:
    DBUG_PRINT("info", ("HA_EXTRA_WRITE_CAN_REPLACE"));
    if (!m_has_unique_index)
    {
      DBUG_PRINT("info", ("Turning ON use of write instead of insert"));
      m_use_write= TRUE;
    }
    break;
  case HA_EXTRA_WRITE_CANNOT_REPLACE:
    DBUG_PRINT("info", ("HA_EXTRA_WRITE_CANNOT_REPLACE"));
    DBUG_PRINT("info", ("Turning OFF use of write instead of insert"));
    m_use_write= FALSE;
    break;
3486 3487 3488 3489 3490 3491 3492 3493
  case HA_EXTRA_DELETE_CANNOT_BATCH:
    DBUG_PRINT("info", ("HA_EXTRA_DELETE_CANNOT_BATCH"));
    m_delete_cannot_batch= TRUE;
    break;
  case HA_EXTRA_UPDATE_CANNOT_BATCH:
    DBUG_PRINT("info", ("HA_EXTRA_UPDATE_CANNOT_BATCH"));
    m_update_cannot_batch= TRUE;
    break;
3494 3495
  default:
    break;
3496 3497 3498 3499 3500
  }
  
  DBUG_RETURN(0);
}

3501 3502 3503 3504

int ha_ndbcluster::reset()
{
  DBUG_ENTER("ha_ndbcluster::reset");
3505 3506 3507 3508
  if (m_cond)
  {
    m_cond->cond_clear();
  }
3509 3510 3511 3512 3513 3514

  /* reset flags set by extra calls */
  m_retrieve_all_fields= FALSE;
  m_retrieve_primary_key= FALSE;
  m_ignore_dup_key= FALSE;
  m_use_write= FALSE;
3515 3516
  m_delete_cannot_batch= FALSE;
  m_update_cannot_batch= FALSE;
3517 3518 3519 3520
  DBUG_RETURN(0);
}


3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533
/* 
   Start of an insert, remember number of rows to be inserted, it will
   be used in write_row and get_autoincrement to send an optimal number
   of rows in each roundtrip to the server

   SYNOPSIS
   rows     number of rows to insert, 0 if unknown

*/

void ha_ndbcluster::start_bulk_insert(ha_rows rows)
{
  int bytes, batch;
joreland@mysql.com's avatar
joreland@mysql.com committed
3534
  const NDBTAB *tab= (const NDBTAB *) m_table;    
3535 3536

  DBUG_ENTER("start_bulk_insert");
pekka@mysql.com's avatar
pekka@mysql.com committed
3537
  DBUG_PRINT("enter", ("rows: %d", (int)rows));
3538
  
3539
  m_rows_inserted= (ha_rows) 0;
3540
  if (!m_use_write && m_ignore_dup_key)
3541 3542 3543
  {
    /*
      compare if expression with that in write_row
3544
      we have a situation where peek_indexed_rows() will be called
3545 3546 3547 3548 3549 3550 3551 3552
      so we cannot batch
    */
    DBUG_PRINT("info", ("Batching turned off as duplicate key is "
                        "ignored by using peek_row"));
    m_rows_to_insert= 1;
    m_bulk_insert_rows= 1;
    DBUG_VOID_RETURN;
  }
3553
  if (rows == (ha_rows) 0)
3554
  {
3555 3556
    /* We don't know how many will be inserted, guess */
    m_rows_to_insert= m_autoincrement_prefetch;
3557
  }
3558 3559
  else
    m_rows_to_insert= rows; 
3560 3561 3562 3563 3564 3565 3566 3567

  /* 
    Calculate how many rows that should be inserted
    per roundtrip to NDB. This is done in order to minimize the 
    number of roundtrips as much as possible. However performance will 
    degrade if too many bytes are inserted, thus it's limited by this 
    calculation.   
  */
3568
  const int bytesperbatch= 8192;
3569
  bytes= 12 + tab->getRowSizeInBytes() + 4 * tab->getNoOfColumns();
3570
  batch= bytesperbatch/bytes;
3571 3572
  batch= batch == 0 ? 1 : batch;
  DBUG_PRINT("info", ("batch: %d, bytes: %d", batch, bytes));
3573
  m_bulk_insert_rows= batch;
3574 3575 3576 3577 3578 3579 3580 3581 3582

  DBUG_VOID_RETURN;
}

/*
  End of an insert
 */
int ha_ndbcluster::end_bulk_insert()
{
3583 3584
  int error= 0;

3585
  DBUG_ENTER("end_bulk_insert");
3586
  // Check if last inserts need to be flushed
3587
  if (m_bulk_insert_not_flushed)
3588
  {
3589
    NdbTransaction *trans= m_active_trans;
3590 3591 3592
    // Send rows to NDB
    DBUG_PRINT("info", ("Sending inserts to NDB, "\
                        "rows_inserted:%d, bulk_insert_rows: %d", 
3593
                        (int) m_rows_inserted, (int) m_bulk_insert_rows)); 
3594
    m_bulk_insert_not_flushed= FALSE;
3595 3596
    if (m_transaction_on)
    {
3597
      if (execute_no_commit(this, trans,false) != 0)
3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609
      {
        no_uncommitted_rows_execute_failure();
        my_errno= error= ndb_err(trans);
      }
    }
    else
    {
      if (execute_commit(this, trans) != 0)
      {
        no_uncommitted_rows_execute_failure();
        my_errno= error= ndb_err(trans);
      }
3610 3611
      else
      {
3612
        IF_DBUG(int res=) trans->restart();
3613 3614
        DBUG_ASSERT(res == 0);
      }
3615
    }
3616 3617
  }

3618 3619
  m_rows_inserted= (ha_rows) 0;
  m_rows_to_insert= (ha_rows) 1;
3620
  DBUG_RETURN(error);
3621 3622
}

3623 3624 3625 3626

int ha_ndbcluster::extra_opt(enum ha_extra_function operation, ulong cache_size)
{
  DBUG_ENTER("extra_opt");
pekka@mysql.com's avatar
pekka@mysql.com committed
3627
  DBUG_PRINT("enter", ("cache_size: %lu", cache_size));
3628 3629 3630
  DBUG_RETURN(extra(operation));
}

3631 3632 3633 3634
static const char *ha_ndbcluster_exts[] = {
 ha_ndb_ext,
 NullS
};
3635

3636
const char** ha_ndbcluster::bas_ext() const
3637 3638 3639
{
  return ha_ndbcluster_exts;
}
3640 3641 3642 3643 3644 3645 3646 3647 3648

/*
  How many seeks it will take to read through the table
  This is to be comparable to the number returned by records_in_range so
  that we can decide if we should scan the table or use keys.
*/

double ha_ndbcluster::scan_time()
{
3649 3650 3651
  DBUG_ENTER("ha_ndbcluster::scan_time()");
  double res= rows2double(records*1000);
  DBUG_PRINT("exit", ("table: %s value: %f", 
3652
                      m_tabname, res));
3653
  DBUG_RETURN(res);
3654 3655
}

3656 3657 3658 3659 3660 3661 3662
/*
  Convert MySQL table locks into locks supported by Ndb Cluster.
  Note that MySQL Cluster does currently not support distributed
  table locks, so to be safe one should set cluster in Single
  User Mode, before relying on table locks when updating tables
  from several MySQL servers
*/
3663 3664 3665 3666 3667 3668 3669 3670

THR_LOCK_DATA **ha_ndbcluster::store_lock(THD *thd,
                                          THR_LOCK_DATA **to,
                                          enum thr_lock_type lock_type)
{
  DBUG_ENTER("store_lock");
  if (lock_type != TL_IGNORE && m_lock.type == TL_UNLOCK) 
  {
3671

3672 3673 3674
    /* If we are not doing a LOCK TABLE, then allow multiple
       writers */
    
3675 3676 3677
    /* Since NDB does not currently have table locks
       this is treated as a ordinary lock */

3678
    if ((lock_type >= TL_WRITE_CONCURRENT_INSERT &&
3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693
         lock_type <= TL_WRITE) && !thd->in_lock_tables)      
      lock_type= TL_WRITE_ALLOW_WRITE;
    
    /* In queries of type INSERT INTO t1 SELECT ... FROM t2 ...
       MySQL would use the lock TL_READ_NO_INSERT on t2, and that
       would conflict with TL_WRITE_ALLOW_WRITE, blocking all inserts
       to t2. Convert the lock to a normal read lock to allow
       concurrent inserts to t2. */
    
    if (lock_type == TL_READ_NO_INSERT && !thd->in_lock_tables)
      lock_type= TL_READ;
    
    m_lock.type=lock_type;
  }
  *to++= &m_lock;
3694 3695

  DBUG_PRINT("exit", ("lock_type: %d", lock_type));
3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717
  
  DBUG_RETURN(to);
}

#ifndef DBUG_OFF
#define PRINT_OPTION_FLAGS(t) { \
      if (t->options & OPTION_NOT_AUTOCOMMIT) \
        DBUG_PRINT("thd->options", ("OPTION_NOT_AUTOCOMMIT")); \
      if (t->options & OPTION_BEGIN) \
        DBUG_PRINT("thd->options", ("OPTION_BEGIN")); \
      if (t->options & OPTION_TABLE_LOCK) \
        DBUG_PRINT("thd->options", ("OPTION_TABLE_LOCK")); \
}
#else
#define PRINT_OPTION_FLAGS(t)
#endif


/*
  As MySQL will execute an external lock for every new table it uses
  we can use this to start the transactions.
  If we are in auto_commit mode we just need to start a transaction
3718
  for the statement, this will be stored in thd_ndb.stmt.
3719
  If not, we have to start a master transaction if there doesn't exist
3720
  one from before, this will be stored in thd_ndb.all
3721 3722 3723
 
  When a table lock is held one transaction will be started which holds
  the table lock and for each statement a hupp transaction will be started  
3724
  If we are locking the table then:
3725
  - save the NdbDictionary::Table for easy access
3726 3727
  - save reference to table statistics
  - refresh list of the indexes for the table if needed (if altered)
3728 3729 3730 3731 3732
 */

int ha_ndbcluster::external_lock(THD *thd, int lock_type)
{
  int error=0;
3733
  NdbTransaction* trans= NULL;
3734 3735 3736 3737 3738 3739

  DBUG_ENTER("external_lock");
  /*
    Check that this handler instance has a connection
    set up to the Ndb object of thd
   */
3740
  if (check_ndb_connection(thd))
3741
    DBUG_RETURN(1);
3742

3743
  Thd_ndb *thd_ndb= get_thd_ndb(thd);
3744
  Ndb *ndb= thd_ndb->ndb;
3745

3746 3747
  DBUG_PRINT("enter", ("thd: 0x%lx  thd_ndb: 0x%lx  thd_ndb->lock_count: %d",
                       (long) thd, (long) thd_ndb, thd_ndb->lock_count));
3748

3749 3750
  if (lock_type != F_UNLCK)
  {
3751
    DBUG_PRINT("info", ("lock_type != F_UNLCK"));
3752 3753 3754 3755
    if (thd->lex->sql_command == SQLCOM_LOAD)
    {
      m_transaction_on= FALSE;
      /* Would be simpler if has_transactions() didn't always say "yes" */
3756
      thd->no_trans_update.all= thd->no_trans_update.stmt= TRUE;
3757 3758
    }
    else if (!thd->transaction.on)
3759 3760 3761
      m_transaction_on= FALSE;
    else
      m_transaction_on= thd->variables.ndb_use_transactions;
3762
    if (!thd_ndb->lock_count++)
3763 3764
    {
      PRINT_OPTION_FLAGS(thd);
3765
      if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN))) 
3766 3767
      {
        // Autocommit transaction
3768
        DBUG_ASSERT(!thd_ndb->stmt);
3769 3770
        DBUG_PRINT("trans",("Starting transaction stmt"));      

3771
        trans= ndb->startTransaction();
3772
        if (trans == NULL)
3773
          ERR_RETURN(ndb->getNdbError());
3774
        no_uncommitted_rows_reset(thd);
3775
        thd_ndb->stmt= trans;
3776
	thd_ndb->query_state&= NDB_QUERY_NORMAL;
3777
        trans_register_ha(thd, FALSE, &ndbcluster_hton);
3778 3779 3780
      } 
      else 
      { 
3781
        if (!thd_ndb->all)
3782
        {
3783 3784 3785 3786
          // Not autocommit transaction
          // A "master" transaction ha not been started yet
          DBUG_PRINT("trans",("starting transaction, all"));
          
3787
          trans= ndb->startTransaction();
3788
          if (trans == NULL)
3789
            ERR_RETURN(ndb->getNdbError());
3790
          no_uncommitted_rows_reset(thd);
3791
          thd_ndb->all= trans; 
3792
	  thd_ndb->query_state&= NDB_QUERY_NORMAL;
3793
          trans_register_ha(thd, TRUE, &ndbcluster_hton);
3794 3795 3796 3797 3798 3799 3800 3801

          /*
            If this is the start of a LOCK TABLE, a table look 
            should be taken on the table in NDB
           
            Check if it should be read or write lock
           */
          if (thd->options & (OPTION_TABLE_LOCK))
3802
          {
3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821
            //lockThisTable();
            DBUG_PRINT("info", ("Locking the table..." ));
          }

        }
      }
    }
    /*
      This is the place to make sure this handler instance
      has a started transaction.
     
      The transaction is started by the first handler on which 
      MySQL Server calls external lock
     
      Other handlers in the same stmt or transaction should use 
      the same NDB transaction. This is done by setting up the m_active_trans
      pointer to point to the NDB transaction. 
     */

3822 3823 3824
    // store thread specific data first to set the right context
    m_force_send=          thd->variables.ndb_force_send;
    m_ha_not_exact_count= !thd->variables.ndb_use_exact_count;
3825 3826
    m_autoincrement_prefetch= 
      (ha_rows) thd->variables.ndb_autoincrement_prefetch_sz;
3827

3828
    m_active_trans= thd_ndb->all ? thd_ndb->all : thd_ndb->stmt;
3829
    DBUG_ASSERT(m_active_trans);
3830
    // Start of transaction
3831
    m_rows_changed= 0;
3832
    m_retrieve_all_fields= FALSE;
3833
    m_retrieve_primary_key= FALSE;
3834
    m_ops_pending= 0;
3835
    {
3836
      NDBDICT *dict= ndb->getDictionary();
3837 3838 3839
      const NDBTAB *tab;
      void *tab_info;
      if (!(tab= dict->getTable(m_tabname, &tab_info)))
3840
        ERR_RETURN(dict->getNdbError());
3841 3842 3843
      DBUG_PRINT("info", ("Table schema version: %d", 
                          tab->getObjectVersion()));
      // Check if thread has stale local cache
3844 3845 3846 3847
      // New transaction must not use old tables... (trans != 0)
      // Running might...
      if ((trans && tab->getObjectStatus() != NdbDictionary::Object::Retrieved)
	  || tab->getObjectStatus() == NdbDictionary::Object::Invalid)
3848 3849
      {
        invalidate_dictionary_cache(FALSE);
3850
        if (!(tab= dict->getTable(m_tabname, &tab_info)))
3851 3852 3853 3854
          ERR_RETURN(dict->getNdbError());
        DBUG_PRINT("info", ("Table schema version: %d", 
                            tab->getObjectVersion()));
      }
3855
      if (m_table_version < tab->getObjectVersion())
3856 3857 3858 3859 3860 3861 3862
      {
        /*
          The table has been altered, caller has to retry
        */
        NdbError err= ndb->getNdbError(NDB_INVALID_SCHEMA_OBJECT);
        DBUG_RETURN(ndb_to_mysql_error(&err));
      }
3863 3864 3865 3866
      if (m_table != (void *)tab)
      {
        m_table= (void *)tab;
        m_table_version = tab->getObjectVersion();
3867
        if ((my_errno= build_index_list(ndb, table, ILBP_OPEN)))
3868
          DBUG_RETURN(my_errno);
3869

3870
        const void *data= NULL, *pack_data= NULL;
3871
        uint length, pack_length;
3872
        if (readfrm(table->s->path, &data, &length) ||
3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883
            packfrm(data, length, &pack_data, &pack_length) ||
            pack_length != tab->getFrmLength() ||
            memcmp(pack_data, tab->getFrmData(), pack_length))
        {
          my_free((char*)data, MYF(MY_ALLOW_ZERO_PTR));
          my_free((char*)pack_data, MYF(MY_ALLOW_ZERO_PTR));
          NdbError err= ndb->getNdbError(NDB_INVALID_SCHEMA_OBJECT);
          DBUG_RETURN(ndb_to_mysql_error(&err));
        }
        my_free((char*)data, MYF(MY_ALLOW_ZERO_PTR));
        my_free((char*)pack_data, MYF(MY_ALLOW_ZERO_PTR));
3884
      }
3885 3886
      m_table_info= tab_info;
    }
3887
    no_uncommitted_rows_init(thd);
3888 3889
  }
  else
3890
  {
3891
    DBUG_PRINT("info", ("lock_type == F_UNLCK"));
3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909

    if (ndb_cache_check_time && m_rows_changed)
    {
      DBUG_PRINT("info", ("Rows has changed and util thread is running"));
      if (thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN))
      {
        DBUG_PRINT("info", ("Add share to list of tables to be invalidated"));
        /* NOTE push_back allocates memory using transactions mem_root! */
        thd_ndb->changed_tables.push_back(m_share, &thd->transaction.mem_root);
      }

      pthread_mutex_lock(&m_share->mutex);
      DBUG_PRINT("info", ("Invalidating commit_count"));
      m_share->commit_count= 0;
      m_share->commit_count_lock++;
      pthread_mutex_unlock(&m_share->mutex);
    }

3910
    if (!--thd_ndb->lock_count)
3911 3912 3913 3914
    {
      DBUG_PRINT("trans", ("Last external_lock"));
      PRINT_OPTION_FLAGS(thd);

3915
      if (thd_ndb->stmt)
3916 3917 3918 3919 3920 3921 3922
      {
        /*
          Unlock is done without a transaction commit / rollback.
          This happens if the thread didn't update any rows
          We must in this case close the transaction to release resources
        */
        DBUG_PRINT("trans",("ending non-updating transaction"));
3923
        ndb->closeTransaction(m_active_trans);
3924
        thd_ndb->stmt= NULL;
3925 3926
      }
    }
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
3927
    m_table_info= NULL;
3928

3929 3930 3931 3932 3933 3934 3935 3936 3937
    /*
      This is the place to make sure this handler instance
      no longer are connected to the active transaction.

      And since the handler is no longer part of the transaction 
      it can't have open cursors, ops or blobs pending.
    */
    m_active_trans= NULL;    

3938 3939
    if (m_active_cursor)
      DBUG_PRINT("warning", ("m_active_cursor != NULL"));
3940 3941
    m_active_cursor= NULL;

3942 3943 3944 3945
    if (m_multi_cursor)
      DBUG_PRINT("warning", ("m_multi_cursor != NULL"));
    m_multi_cursor= NULL;
    
3946
    if (m_blobs_pending)
3947
      DBUG_PRINT("warning", ("blobs_pending != 0"));
3948
    m_blobs_pending= 0;
3949
    
3950
    if (m_ops_pending)
3951
      DBUG_PRINT("warning", ("ops_pending != 0L"));
3952
    m_ops_pending= 0;
3953 3954 3955 3956
  }
  DBUG_RETURN(error);
}

3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972
/*
  Unlock the last row read in an open scan.
  Rows are unlocked by default in ndb, but
  for SELECT FOR UPDATE and SELECT LOCK WIT SHARE MODE
  locks are kept if unlock_row() is not called.
*/

void ha_ndbcluster::unlock_row() 
{
  DBUG_ENTER("unlock_row");

  DBUG_PRINT("info", ("Unlocking row"));
  m_lock_tuple= false;
  DBUG_VOID_RETURN;
}

3973
/*
3974 3975 3976 3977 3978
  Start a transaction for running a statement if one is not
  already running in a transaction. This will be the case in
  a BEGIN; COMMIT; block
  When using LOCK TABLE's external_lock will start a transaction
  since ndb does not currently does not support table locking
3979 3980
*/

serg@serg.mylan's avatar
serg@serg.mylan committed
3981
int ha_ndbcluster::start_stmt(THD *thd, thr_lock_type lock_type)
3982 3983 3984 3985 3986
{
  int error=0;
  DBUG_ENTER("start_stmt");
  PRINT_OPTION_FLAGS(thd);

3987
  Thd_ndb *thd_ndb= get_thd_ndb(thd);
3988
  NdbTransaction *trans= (thd_ndb->stmt)?thd_ndb->stmt:thd_ndb->all;
3989
  if (!trans){
3990
    Ndb *ndb= thd_ndb->ndb;
3991
    DBUG_PRINT("trans",("Starting transaction stmt"));  
3992
    trans= ndb->startTransaction();
3993
    if (trans == NULL)
3994
      ERR_RETURN(ndb->getNdbError());
3995
    no_uncommitted_rows_reset(thd);
3996
    thd_ndb->stmt= trans;
3997
    thd_ndb->query_state&= NDB_QUERY_NORMAL;
3998
    trans_register_ha(thd, FALSE, &ndbcluster_hton);
3999 4000
  }
  m_active_trans= trans;
4001
  // Start of statement
4002
  m_retrieve_all_fields= FALSE;
4003
  m_retrieve_primary_key= FALSE;
4004
  m_ops_pending= 0;    
4005 4006 4007 4008 4009 4010
  
  DBUG_RETURN(error);
}


/*
4011
  Commit a transaction started in NDB
4012 4013
 */

4014
int ndbcluster_commit(THD *thd, bool all)
4015 4016
{
  int res= 0;
4017 4018 4019
  Thd_ndb *thd_ndb= get_thd_ndb(thd);
  Ndb *ndb= thd_ndb->ndb;
  NdbTransaction *trans= all ? thd_ndb->all : thd_ndb->stmt;
4020 4021 4022

  DBUG_ENTER("ndbcluster_commit");
  DBUG_PRINT("transaction",("%s",
4023
                            trans == thd_ndb->stmt ?
4024 4025 4026
                            "stmt" : "all"));
  DBUG_ASSERT(ndb && trans);

4027
  if (execute_commit(thd,trans) != 0)
4028 4029
  {
    const NdbError err= trans->getNdbError();
4030
    const NdbOperation *error_op= trans->getNdbErrorOperation();
4031
    ERR_PRINT(err);
4032
    res= ndb_to_mysql_error(&err);
4033
    if (res != -1)
4034
      ndbcluster_print_error(res, error_op);
4035
  }
4036
  ndb->closeTransaction(trans);
4037

4038
  if (all)
4039 4040 4041
    thd_ndb->all= NULL;
  else
    thd_ndb->stmt= NULL;
4042 4043 4044 4045 4046 4047 4048

  /* Clear commit_count for tables changed by transaction */
  NDB_SHARE* share;
  List_iterator_fast<NDB_SHARE> it(thd_ndb->changed_tables);
  while ((share= it++))
  {
    pthread_mutex_lock(&share->mutex);
4049 4050
    DBUG_PRINT("info", ("Invalidate commit_count for %s, share->commit_count: %lu",
                        share->table_name, (ulong) share->commit_count));
4051 4052 4053 4054 4055 4056
    share->commit_count= 0;
    share->commit_count_lock++;
    pthread_mutex_unlock(&share->mutex);
  }
  thd_ndb->changed_tables.empty();

4057 4058 4059 4060 4061 4062 4063 4064
  DBUG_RETURN(res);
}


/*
  Rollback a transaction started in NDB
 */

4065
int ndbcluster_rollback(THD *thd, bool all)
4066 4067
{
  int res= 0;
4068 4069 4070
  Thd_ndb *thd_ndb= get_thd_ndb(thd);
  Ndb *ndb= thd_ndb->ndb;
  NdbTransaction *trans= all ? thd_ndb->all : thd_ndb->stmt;
4071 4072 4073

  DBUG_ENTER("ndbcluster_rollback");
  DBUG_PRINT("transaction",("%s",
4074
                            trans == thd_ndb->stmt ? 
4075 4076 4077
                            "stmt" : "all"));
  DBUG_ASSERT(ndb && trans);

4078
  if (trans->execute(NdbTransaction::Rollback) != 0)
4079 4080
  {
    const NdbError err= trans->getNdbError();
4081
    const NdbOperation *error_op= trans->getNdbErrorOperation();
4082 4083
    ERR_PRINT(err);     
    res= ndb_to_mysql_error(&err);
4084 4085
    if (res != -1) 
      ndbcluster_print_error(res, error_op);
4086 4087
  }
  ndb->closeTransaction(trans);
4088

4089
  if (all)
4090 4091 4092 4093
    thd_ndb->all= NULL;
  else
    thd_ndb->stmt= NULL;

4094 4095 4096
  /* Clear list of tables changed by transaction */
  thd_ndb->changed_tables.empty();

4097
  DBUG_RETURN(res);
4098 4099 4100 4101
}


/*
pekka@mysql.com's avatar
pekka@mysql.com committed
4102 4103 4104
  Define NDB column based on Field.
  Returns 0 or mysql error code.
  Not member of ha_ndbcluster because NDBCOL cannot be declared.
pekka@mysql.com's avatar
pekka@mysql.com committed
4105 4106 4107

  MySQL text types with character set "binary" are mapped to true
  NDB binary types without a character set.  This may change.
4108 4109
 */

pekka@mysql.com's avatar
pekka@mysql.com committed
4110 4111 4112
static int create_ndb_column(NDBCOL &col,
                             Field *field,
                             HA_CREATE_INFO *info)
4113
{
pekka@mysql.com's avatar
pekka@mysql.com committed
4114
  // Set name
4115 4116 4117 4118
  if (col.setName(field->field_name))
  {
    return (my_errno= errno);
  }
pekka@mysql.com's avatar
pekka@mysql.com committed
4119 4120
  // Get char set
  CHARSET_INFO *cs= field->charset();
pekka@mysql.com's avatar
pekka@mysql.com committed
4121 4122 4123 4124
  // Set type and sizes
  const enum enum_field_types mysql_type= field->real_type();
  switch (mysql_type) {
  // Numeric types
4125
  case MYSQL_TYPE_TINY:        
pekka@mysql.com's avatar
pekka@mysql.com committed
4126 4127 4128 4129 4130 4131
    if (field->flags & UNSIGNED_FLAG)
      col.setType(NDBCOL::Tinyunsigned);
    else
      col.setType(NDBCOL::Tinyint);
    col.setLength(1);
    break;
4132
  case MYSQL_TYPE_SHORT:
pekka@mysql.com's avatar
pekka@mysql.com committed
4133 4134 4135 4136 4137 4138
    if (field->flags & UNSIGNED_FLAG)
      col.setType(NDBCOL::Smallunsigned);
    else
      col.setType(NDBCOL::Smallint);
    col.setLength(1);
    break;
4139
  case MYSQL_TYPE_LONG:
pekka@mysql.com's avatar
pekka@mysql.com committed
4140 4141 4142 4143 4144 4145
    if (field->flags & UNSIGNED_FLAG)
      col.setType(NDBCOL::Unsigned);
    else
      col.setType(NDBCOL::Int);
    col.setLength(1);
    break;
4146
  case MYSQL_TYPE_INT24:       
pekka@mysql.com's avatar
pekka@mysql.com committed
4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158
    if (field->flags & UNSIGNED_FLAG)
      col.setType(NDBCOL::Mediumunsigned);
    else
      col.setType(NDBCOL::Mediumint);
    col.setLength(1);
    break;
  case MYSQL_TYPE_LONGLONG:
    if (field->flags & UNSIGNED_FLAG)
      col.setType(NDBCOL::Bigunsigned);
    else
      col.setType(NDBCOL::Bigint);
    col.setLength(1);
4159 4160
    break;
  case MYSQL_TYPE_FLOAT:
pekka@mysql.com's avatar
pekka@mysql.com committed
4161 4162 4163
    col.setType(NDBCOL::Float);
    col.setLength(1);
    break;
4164
  case MYSQL_TYPE_DOUBLE:
pekka@mysql.com's avatar
pekka@mysql.com committed
4165 4166 4167
    col.setType(NDBCOL::Double);
    col.setLength(1);
    break;
4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187
  case MYSQL_TYPE_DECIMAL:    
    {
      Field_decimal *f= (Field_decimal*)field;
      uint precision= f->pack_length();
      uint scale= f->decimals();
      if (field->flags & UNSIGNED_FLAG)
      {
        col.setType(NDBCOL::Olddecimalunsigned);
        precision-= (scale > 0);
      }
      else
      {
        col.setType(NDBCOL::Olddecimal);
        precision-= 1 + (scale > 0);
      }
      col.setPrecision(precision);
      col.setScale(scale);
      col.setLength(1);
    }
    break;
4188 4189 4190
  case MYSQL_TYPE_NEWDECIMAL:    
    {
      Field_new_decimal *f= (Field_new_decimal*)field;
4191
      uint precision= f->precision;
4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205
      uint scale= f->decimals();
      if (field->flags & UNSIGNED_FLAG)
      {
        col.setType(NDBCOL::Decimalunsigned);
      }
      else
      {
        col.setType(NDBCOL::Decimal);
      }
      col.setPrecision(precision);
      col.setScale(scale);
      col.setLength(1);
    }
    break;
pekka@mysql.com's avatar
pekka@mysql.com committed
4206 4207 4208 4209 4210
  // Date types
  case MYSQL_TYPE_DATETIME:    
    col.setType(NDBCOL::Datetime);
    col.setLength(1);
    break;
4211 4212 4213 4214
  case MYSQL_TYPE_DATE: // ?
    col.setType(NDBCOL::Char);
    col.setLength(field->pack_length());
    break;
pekka@mysql.com's avatar
pekka@mysql.com committed
4215
  case MYSQL_TYPE_NEWDATE:
4216 4217 4218
    col.setType(NDBCOL::Date);
    col.setLength(1);
    break;
pekka@mysql.com's avatar
pekka@mysql.com committed
4219
  case MYSQL_TYPE_TIME:        
4220 4221 4222
    col.setType(NDBCOL::Time);
    col.setLength(1);
    break;
4223 4224 4225 4226 4227 4228 4229
  case MYSQL_TYPE_YEAR:
    col.setType(NDBCOL::Year);
    col.setLength(1);
    break;
  case MYSQL_TYPE_TIMESTAMP:
    col.setType(NDBCOL::Timestamp);
    col.setLength(1);
pekka@mysql.com's avatar
pekka@mysql.com committed
4230 4231 4232
    break;
  // Char types
  case MYSQL_TYPE_STRING:      
4233
    if (field->pack_length() == 0)
4234 4235 4236 4237
    {
      col.setType(NDBCOL::Bit);
      col.setLength(1);
    }
pekka@mysql.com's avatar
pekka@mysql.com committed
4238
    else if ((field->flags & BINARY_FLAG) && cs == &my_charset_bin)
4239
    {
pekka@mysql.com's avatar
pekka@mysql.com committed
4240
      col.setType(NDBCOL::Binary);
4241
      col.setLength(field->pack_length());
pekka@mysql.com's avatar
pekka@mysql.com committed
4242
    }
4243
    else
4244 4245 4246
    {
      col.setType(NDBCOL::Char);
      col.setCharset(cs);
4247
      col.setLength(field->pack_length());
4248
    }
pekka@mysql.com's avatar
pekka@mysql.com committed
4249
    break;
pekka@mysql.com's avatar
pekka@mysql.com committed
4250 4251 4252 4253 4254 4255
  case MYSQL_TYPE_VAR_STRING: // ?
  case MYSQL_TYPE_VARCHAR:
    {
      Field_varstring* f= (Field_varstring*)field;
      if (f->length_bytes == 1)
      {
pekka@mysql.com's avatar
pekka@mysql.com committed
4256
        if ((field->flags & BINARY_FLAG) && cs == &my_charset_bin)
pekka@mysql.com's avatar
pekka@mysql.com committed
4257 4258 4259 4260 4261 4262 4263 4264
          col.setType(NDBCOL::Varbinary);
        else {
          col.setType(NDBCOL::Varchar);
          col.setCharset(cs);
        }
      }
      else if (f->length_bytes == 2)
      {
pekka@mysql.com's avatar
pekka@mysql.com committed
4265
        if ((field->flags & BINARY_FLAG) && cs == &my_charset_bin)
pekka@mysql.com's avatar
pekka@mysql.com committed
4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276
          col.setType(NDBCOL::Longvarbinary);
        else {
          col.setType(NDBCOL::Longvarchar);
          col.setCharset(cs);
        }
      }
      else
      {
        return HA_ERR_UNSUPPORTED;
      }
      col.setLength(field->field_length);
pekka@mysql.com's avatar
pekka@mysql.com committed
4277
    }
pekka@mysql.com's avatar
pekka@mysql.com committed
4278 4279 4280 4281
    break;
  // Blob types (all come in as MYSQL_TYPE_BLOB)
  mysql_type_tiny_blob:
  case MYSQL_TYPE_TINY_BLOB:
pekka@mysql.com's avatar
pekka@mysql.com committed
4282
    if ((field->flags & BINARY_FLAG) && cs == &my_charset_bin)
pekka@mysql.com's avatar
pekka@mysql.com committed
4283
      col.setType(NDBCOL::Blob);
pekka@mysql.com's avatar
pekka@mysql.com committed
4284
    else {
pekka@mysql.com's avatar
pekka@mysql.com committed
4285
      col.setType(NDBCOL::Text);
pekka@mysql.com's avatar
pekka@mysql.com committed
4286 4287
      col.setCharset(cs);
    }
pekka@mysql.com's avatar
pekka@mysql.com committed
4288 4289 4290 4291 4292
    col.setInlineSize(256);
    // No parts
    col.setPartSize(0);
    col.setStripeSize(0);
    break;
4293
  //mysql_type_blob:
4294
  case MYSQL_TYPE_GEOMETRY:
pekka@mysql.com's avatar
pekka@mysql.com committed
4295
  case MYSQL_TYPE_BLOB:    
pekka@mysql.com's avatar
pekka@mysql.com committed
4296
    if ((field->flags & BINARY_FLAG) && cs == &my_charset_bin)
pekka@mysql.com's avatar
pekka@mysql.com committed
4297
      col.setType(NDBCOL::Blob);
pekka@mysql.com's avatar
pekka@mysql.com committed
4298
    else {
pekka@mysql.com's avatar
pekka@mysql.com committed
4299
      col.setType(NDBCOL::Text);
pekka@mysql.com's avatar
pekka@mysql.com committed
4300 4301
      col.setCharset(cs);
    }
pekka@mysql.com's avatar
pekka@mysql.com committed
4302
    {
4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323
      Field_blob *field_blob= (Field_blob *)field;
      /*
       * max_data_length is 2^8-1, 2^16-1, 2^24-1 for tiny, blob, medium.
       * Tinyblob gets no blob parts.  The other cases are just a crude
       * way to control part size and striping.
       *
       * In mysql blob(256) is promoted to blob(65535) so it does not
       * in fact fit "inline" in NDB.
       */
      if (field_blob->max_data_length() < (1 << 8))
        goto mysql_type_tiny_blob;
      else if (field_blob->max_data_length() < (1 << 16))
      {
        col.setInlineSize(256);
        col.setPartSize(2000);
        col.setStripeSize(16);
      }
      else if (field_blob->max_data_length() < (1 << 24))
        goto mysql_type_medium_blob;
      else
        goto mysql_type_long_blob;
pekka@mysql.com's avatar
pekka@mysql.com committed
4324 4325 4326 4327
    }
    break;
  mysql_type_medium_blob:
  case MYSQL_TYPE_MEDIUM_BLOB:   
pekka@mysql.com's avatar
pekka@mysql.com committed
4328
    if ((field->flags & BINARY_FLAG) && cs == &my_charset_bin)
pekka@mysql.com's avatar
pekka@mysql.com committed
4329
      col.setType(NDBCOL::Blob);
pekka@mysql.com's avatar
pekka@mysql.com committed
4330
    else {
pekka@mysql.com's avatar
pekka@mysql.com committed
4331
      col.setType(NDBCOL::Text);
pekka@mysql.com's avatar
pekka@mysql.com committed
4332 4333
      col.setCharset(cs);
    }
pekka@mysql.com's avatar
pekka@mysql.com committed
4334 4335 4336 4337 4338 4339
    col.setInlineSize(256);
    col.setPartSize(4000);
    col.setStripeSize(8);
    break;
  mysql_type_long_blob:
  case MYSQL_TYPE_LONG_BLOB:  
pekka@mysql.com's avatar
pekka@mysql.com committed
4340
    if ((field->flags & BINARY_FLAG) && cs == &my_charset_bin)
pekka@mysql.com's avatar
pekka@mysql.com committed
4341
      col.setType(NDBCOL::Blob);
pekka@mysql.com's avatar
pekka@mysql.com committed
4342
    else {
pekka@mysql.com's avatar
pekka@mysql.com committed
4343
      col.setType(NDBCOL::Text);
pekka@mysql.com's avatar
pekka@mysql.com committed
4344 4345
      col.setCharset(cs);
    }
pekka@mysql.com's avatar
pekka@mysql.com committed
4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358
    col.setInlineSize(256);
    col.setPartSize(8000);
    col.setStripeSize(4);
    break;
  // Other types
  case MYSQL_TYPE_ENUM:
    col.setType(NDBCOL::Char);
    col.setLength(field->pack_length());
    break;
  case MYSQL_TYPE_SET:         
    col.setType(NDBCOL::Char);
    col.setLength(field->pack_length());
    break;
4359 4360
  case MYSQL_TYPE_BIT:
  {
4361
    int no_of_bits= field->field_length;
4362 4363 4364 4365 4366 4367 4368
    col.setType(NDBCOL::Bit);
    if (!no_of_bits)
      col.setLength(1);
      else
        col.setLength(no_of_bits);
    break;
  }
pekka@mysql.com's avatar
pekka@mysql.com committed
4369 4370 4371 4372 4373
  case MYSQL_TYPE_NULL:        
    goto mysql_type_unsupported;
  mysql_type_unsupported:
  default:
    return HA_ERR_UNSUPPORTED;
4374
  }
pekka@mysql.com's avatar
pekka@mysql.com committed
4375 4376 4377 4378 4379 4380
  // Set nullable and pk
  col.setNullable(field->maybe_null());
  col.setPrimaryKey(field->flags & PRI_KEY_FLAG);
  // Set autoincrement
  if (field->flags & AUTO_INCREMENT_FLAG) 
  {
4381
#ifndef DBUG_OFF
4382
    char buff[22];
4383
#endif
pekka@mysql.com's avatar
pekka@mysql.com committed
4384 4385
    col.setAutoIncrement(TRUE);
    ulonglong value= info->auto_increment_value ?
4386
      info->auto_increment_value : (ulonglong) 1;
4387
    DBUG_PRINT("info", ("Autoincrement key, initial: %s", llstr(value, buff)));
pekka@mysql.com's avatar
pekka@mysql.com committed
4388
    col.setAutoIncrementInitialValue(value);
4389
  }
pekka@mysql.com's avatar
pekka@mysql.com committed
4390
  else
4391
    col.setAutoIncrement(FALSE);
pekka@mysql.com's avatar
pekka@mysql.com committed
4392
  return 0;
4393 4394 4395 4396 4397 4398
}

/*
  Create a table in NDB Cluster
 */

mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
4399 4400
static void ndb_set_fragmentation(NDBTAB &tab, TABLE *form, uint pk_length)
{
4401 4402 4403 4404 4405
  ha_rows max_rows= form->s->max_rows;
  ha_rows min_rows= form->s->min_rows;
  if (max_rows < min_rows)
    max_rows= min_rows;
  if (max_rows == (ha_rows)0) /* default setting, don't set fragmentation */
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422
    return;
  /**
   * get the number of fragments right
   */
  uint no_fragments;
  {
#if MYSQL_VERSION_ID >= 50000
    uint acc_row_size= 25 + /*safety margin*/ 2;
#else
    uint acc_row_size= pk_length*4;
    /* add acc overhead */
    if (pk_length <= 8)  /* main page will set the limit */
      acc_row_size+= 25 + /*safety margin*/ 2;
    else                /* overflow page will set the limit */
      acc_row_size+= 4 + /*safety margin*/ 4;
#endif
    ulonglong acc_fragment_size= 512*1024*1024;
4423 4424 4425 4426 4427
    /*
     * if not --with-big-tables then max_rows is ulong
     * the warning in this case is misleading though
     */
    ulonglong big_max_rows = (ulonglong)max_rows;
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
4428
#if MYSQL_VERSION_ID >= 50100
4429
    no_fragments= (big_max_rows*acc_row_size)/acc_fragment_size+1;
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
4430
#else
4431
    no_fragments= ((big_max_rows*acc_row_size)/acc_fragment_size+1
4432
                   +1/*correct rounding*/)/2;
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
4433 4434 4435 4436 4437 4438 4439 4440 4441
#endif
  }
  {
    uint no_nodes= g_ndb_cluster_connection->no_db_nodes();
    NDBTAB::FragmentType ftype;
    if (no_fragments > 2*no_nodes)
    {
      ftype= NDBTAB::FragAllLarge;
      if (no_fragments > 4*no_nodes)
4442 4443
        push_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
                     "Ndb might have problems storing the max amount of rows specified");
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
4444 4445 4446 4447 4448 4449 4450
    }
    else if (no_fragments > no_nodes)
      ftype= NDBTAB::FragAllMedium;
    else
      ftype= NDBTAB::FragAllSmall;
    tab.setFragmentType(ftype);
  }
4451 4452
  tab.setMaxRows(max_rows);
  tab.setMinRows(min_rows);
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
4453 4454
}

4455
int ha_ndbcluster::create(const char *name, 
4456
                          TABLE *form, 
4457
                          HA_CREATE_INFO *create_info)
4458 4459 4460
{
  NDBTAB tab;
  NDBCOL col;
joreland@mysql.com's avatar
joreland@mysql.com committed
4461
  uint pack_length, length, i, pk_length= 0;
4462
  const void *data= NULL, *pack_data= NULL;
4463
  char name2[FN_HEADLEN];
4464
  bool create_from_engine= (create_info->table_options & HA_OPTION_CREATE_FROM_ENGINE);
4465

pekka@mysql.com's avatar
pekka@mysql.com committed
4466
  DBUG_ENTER("ha_ndbcluster::create");
4467 4468 4469
  DBUG_PRINT("enter", ("name: %s", name));
  fn_format(name2, name, "", "",2);       // Remove the .frm extension
  set_dbname(name2);
4470 4471
  set_tabname(name2);    

4472 4473 4474 4475 4476 4477
  if (current_thd->lex->sql_command == SQLCOM_TRUNCATE)
  {
    DBUG_PRINT("info", ("Dropping and re-creating table for TRUNCATE"));
    if ((my_errno= delete_table(name)))
      DBUG_RETURN(my_errno);
  }
4478 4479 4480 4481 4482 4483 4484 4485 4486 4487
  if (create_from_engine)
  {
    /*
      Table alreay exists in NDB and frm file has been created by 
      caller.
      Do Ndb specific stuff, such as create a .ndb file
    */
    my_errno= write_ndb_file();
    DBUG_RETURN(my_errno);
  }
4488 4489

  DBUG_PRINT("table", ("name: %s", m_tabname));  
4490 4491 4492 4493
  if (tab.setName(m_tabname))
  {
    DBUG_RETURN(my_errno= errno);
  }
4494
  tab.setLogging(!(create_info->options & HA_LEX_CREATE_TMP_TABLE));    
4495 4496 4497 4498 4499
   
  // Save frm data for this table
  if (readfrm(name, &data, &length))
    DBUG_RETURN(1);
  if (packfrm(data, length, &pack_data, &pack_length))
4500 4501
  {
    my_free((char*)data, MYF(0));
4502
    DBUG_RETURN(2);
4503 4504
  }

4505
  DBUG_PRINT("info", ("setFrm data: 0x%lx  len: %d", (long) pack_data, pack_length));
4506 4507 4508 4509
  tab.setFrm(pack_data, pack_length);      
  my_free((char*)data, MYF(0));
  my_free((char*)pack_data, MYF(0));
  
4510
  for (i= 0; i < form->s->fields; i++) 
4511 4512
  {
    Field *field= form->field[i];
4513
    DBUG_PRINT("info", ("name: %s  type: %u  pack_length: %d", 
4514
                        field->field_name, field->real_type(),
4515
                        field->pack_length()));
4516
    if ((my_errno= create_ndb_column(col, field, create_info)))
pekka@mysql.com's avatar
pekka@mysql.com committed
4517
      DBUG_RETURN(my_errno);
4518 4519 4520 4521
    if (tab.addColumn(col))
    {
      DBUG_RETURN(my_errno= errno);
    }
4522
    if (col.getPrimaryKey())
joreland@mysql.com's avatar
joreland@mysql.com committed
4523
      pk_length += (field->pack_length() + 3) / 4;
4524 4525 4526
  }
  
  // No primary key, create shadow key as 64 bit, auto increment  
4527
  if (form->s->primary_key == MAX_KEY) 
4528 4529
  {
    DBUG_PRINT("info", ("Generating shadow key"));
4530 4531 4532 4533
    if (col.setName("$PK"))
    {
      DBUG_RETURN(my_errno= errno);
    }
4534 4535
    col.setType(NdbDictionary::Column::Bigunsigned);
    col.setLength(1);
4536
    col.setNullable(FALSE);
4537 4538
    col.setPrimaryKey(TRUE);
    col.setAutoIncrement(TRUE);
4539 4540 4541 4542
    if (tab.addColumn(col))
    {
      DBUG_RETURN(my_errno= errno);
    }
joreland@mysql.com's avatar
joreland@mysql.com committed
4543 4544 4545 4546
    pk_length += 2;
  }
  
  // Make sure that blob tables don't have to big part size
4547
  for (i= 0; i < form->s->fields; i++) 
joreland@mysql.com's avatar
joreland@mysql.com committed
4548 4549 4550 4551 4552 4553 4554
  {
    /**
     * The extra +7 concists
     * 2 - words from pk in blob table
     * 5 - from extra words added by tup/dict??
     */
    switch (form->field[i]->real_type()) {
4555
    case MYSQL_TYPE_GEOMETRY:
joreland@mysql.com's avatar
joreland@mysql.com committed
4556 4557 4558 4559
    case MYSQL_TYPE_BLOB:    
    case MYSQL_TYPE_MEDIUM_BLOB:   
    case MYSQL_TYPE_LONG_BLOB: 
    {
4560 4561
      NdbDictionary::Column * column= tab.getColumn(i);
      int size= pk_length + (column->getPartSize()+3)/4 + 7;
4562
      if (size > NDB_MAX_TUPLE_SIZE_IN_WORDS && 
4563
         (pk_length+7) < NDB_MAX_TUPLE_SIZE_IN_WORDS)
joreland@mysql.com's avatar
joreland@mysql.com committed
4564
      {
4565
        size= NDB_MAX_TUPLE_SIZE_IN_WORDS - pk_length - 7;
4566
        column->setPartSize(4*size);
joreland@mysql.com's avatar
joreland@mysql.com committed
4567 4568 4569 4570 4571 4572 4573 4574 4575 4576
      }
      /**
       * If size > NDB_MAX and pk_length+7 >= NDB_MAX
       *   then the table can't be created anyway, so skip
       *   changing part size, and have error later
       */ 
    }
    default:
      break;
    }
4577
  }
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
4578 4579 4580

  ndb_set_fragmentation(tab, form, pk_length);

4581
  if ((my_errno= check_ndb_connection()))
4582 4583 4584
    DBUG_RETURN(my_errno);
  
  // Create the table in NDB     
4585 4586
  Ndb *ndb= get_ndb();
  NDBDICT *dict= ndb->getDictionary();
4587
  if (dict->createTable(tab) != 0) 
4588 4589 4590 4591 4592 4593 4594 4595
  {
    const NdbError err= dict->getNdbError();
    ERR_PRINT(err);
    my_errno= ndb_to_mysql_error(&err);
    DBUG_RETURN(my_errno);
  }
  DBUG_PRINT("info", ("Table %s/%s created successfully", 
                      m_dbname, m_tabname));
4596

4597
  // Create secondary indexes
4598
  my_errno= build_index_list(ndb, form, ILBP_CREATE);
4599

4600 4601 4602
  if (!my_errno)
    my_errno= write_ndb_file();

4603 4604 4605 4606
  DBUG_RETURN(my_errno);
}


4607
int ha_ndbcluster::create_ordered_index(const char *name, 
4608
                                        KEY *key_info)
4609
{
4610
  DBUG_ENTER("ha_ndbcluster::create_ordered_index");
4611
  DBUG_RETURN(create_index(name, key_info, FALSE));
4612 4613 4614
}

int ha_ndbcluster::create_unique_index(const char *name, 
4615
                                       KEY *key_info)
4616 4617
{

4618
  DBUG_ENTER("ha_ndbcluster::create_unique_index");
4619
  DBUG_RETURN(create_index(name, key_info, TRUE));
4620 4621 4622
}


4623 4624 4625 4626 4627
/*
  Create an index in NDB Cluster
 */

int ha_ndbcluster::create_index(const char *name, 
4628 4629
                                KEY *key_info,
                                bool unique)
4630
{
4631 4632
  Ndb *ndb= get_ndb();
  NdbDictionary::Dictionary *dict= ndb->getDictionary();
4633 4634 4635
  KEY_PART_INFO *key_part= key_info->key_part;
  KEY_PART_INFO *end= key_part + key_info->key_parts;
  
4636
  DBUG_ENTER("ha_ndbcluster::create_index");
4637
  DBUG_PRINT("enter", ("name: %s ", name));
4638

4639
  NdbDictionary::Index ndb_index(name);
4640
  if (unique)
4641 4642 4643 4644 4645
    ndb_index.setType(NdbDictionary::Index::UniqueHashIndex);
  else 
  {
    ndb_index.setType(NdbDictionary::Index::OrderedIndex);
    // TODO Only temporary ordered indexes supported
4646
    ndb_index.setLogging(FALSE); 
4647
  }
4648 4649 4650 4651
  if (ndb_index.setTable(m_tabname))
  {
    DBUG_RETURN(my_errno= errno);
  }
4652 4653 4654 4655 4656

  for (; key_part != end; key_part++) 
  {
    Field *field= key_part->field;
    DBUG_PRINT("info", ("attr: %s", field->field_name));
4657 4658 4659 4660
    if (ndb_index.addColumnName(field->field_name))
    {
      DBUG_RETURN(my_errno= errno);
    }
4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676
  }
  
  if (dict->createIndex(ndb_index))
    ERR_RETURN(dict->getNdbError());

  // Success
  DBUG_PRINT("info", ("Created index %s", name));
  DBUG_RETURN(0);  
}

/*
  Rename a table in NDB Cluster
*/

int ha_ndbcluster::rename_table(const char *from, const char *to)
{
4677
  NDBDICT *dict;
4678
  char new_tabname[FN_HEADLEN];
4679
  char new_dbname[FN_HEADLEN];
4680 4681
  const NDBTAB *orig_tab;
  int result;
4682 4683
  bool recreate_indexes= FALSE;
  NDBDICT::List index_list;
4684 4685

  DBUG_ENTER("ha_ndbcluster::rename_table");
4686
  DBUG_PRINT("info", ("Renaming %s to %s", from, to));
4687
  set_dbname(from);
4688
  set_dbname(to, new_dbname);
4689 4690 4691
  set_tabname(from);
  set_tabname(to, new_tabname);

4692 4693 4694
  if (check_ndb_connection())
    DBUG_RETURN(my_errno= HA_ERR_NO_CONNECTION);

mskold@mysql.com's avatar
mskold@mysql.com committed
4695 4696
  Ndb *ndb= get_ndb();
  dict= ndb->getDictionary();
4697 4698
  if (!(orig_tab= dict->getTable(m_tabname)))
    ERR_RETURN(dict->getNdbError());
4699 4700 4701 4702 4703 4704 4705
  // Check if thread has stale local cache
  if (orig_tab->getObjectStatus() == NdbDictionary::Object::Invalid)
  {
    dict->removeCachedTable(m_tabname);
    if (!(orig_tab= dict->getTable(m_tabname)))
      ERR_RETURN(dict->getNdbError());
  }
4706 4707 4708 4709 4710 4711
  if (my_strcasecmp(system_charset_info, new_dbname, m_dbname))
  {
    dict->listIndexes(index_list, m_tabname);
    recreate_indexes= TRUE;
  }

4712 4713 4714
  m_table= (void *)orig_tab;
  // Change current database to that of target table
  set_dbname(to);
4715 4716 4717 4718
  if (ndb->setDatabaseName(m_dbname))
  {
    ERR_RETURN(ndb->getNdbError());
  }
4719
  if (!(result= alter_table_name(new_tabname)))
4720
  {
4721 4722
    // Rename .ndb file
    result= handler::rename_table(from, to);
4723
  }
4724

4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736
  // If we are moving tables between databases, we need to recreate
  // indexes
  if (recreate_indexes)
  {
    const NDBTAB *new_tab;
    set_tabname(to);
    if (!(new_tab= dict->getTable(m_tabname)))
      ERR_RETURN(dict->getNdbError());

    for (unsigned i = 0; i < index_list.count; i++) {
        NDBDICT::List::Element& index_el = index_list.elements[i];
	set_dbname(from);
4737 4738 4739 4740
	if (ndb->setDatabaseName(m_dbname))
        {
          ERR_RETURN(ndb->getNdbError());
        }
4741 4742
	const NDBINDEX * index= dict->getIndex(index_el.name,  *new_tab);
	set_dbname(to);
4743 4744 4745 4746
	if (ndb->setDatabaseName(m_dbname))
        {
          ERR_RETURN(ndb->getNdbError());
        }
4747 4748 4749 4750 4751 4752 4753
	DBUG_PRINT("info", ("Creating index %s/%s", 
			    m_dbname, index->getName()));
	dict->createIndex(*index);
        DBUG_PRINT("info", ("Dropping index %s/%s", 
			    m_dbname, index->getName()));
	
	set_dbname(from);
4754 4755 4756 4757
        if (ndb->setDatabaseName(m_dbname))
        {
          ERR_RETURN(ndb->getNdbError());
        }
4758 4759 4760 4761
	dict->dropIndex(*index);
    }
  }

4762 4763 4764 4765 4766 4767 4768 4769
  DBUG_RETURN(result);
}


/*
  Rename a table in NDB Cluster using alter table
 */

4770
int ha_ndbcluster::alter_table_name(const char *to)
4771
{
4772 4773
  Ndb *ndb= get_ndb();
  NDBDICT *dict= ndb->getDictionary();
4774
  const NDBTAB *orig_tab= (const NDBTAB *) m_table;
4775 4776
  DBUG_ENTER("alter_table_name_table");

4777
  NdbDictionary::Table new_tab= *orig_tab;
4778 4779 4780 4781
  if (new_tab.setName(to))
  {
    DBUG_RETURN(my_errno= errno);
  }
4782
  if (dict->alterTable(new_tab) != 0)
4783 4784 4785
    ERR_RETURN(dict->getNdbError());

  m_table= NULL;
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
4786
  m_table_info= NULL;
4787 4788 4789 4790 4791 4792
                                                                             
  DBUG_RETURN(0);
}


/*
4793 4794
  Delete table from NDB Cluster

4795 4796 4797 4798
 */

int ha_ndbcluster::delete_table(const char *name)
{
4799
  DBUG_ENTER("ha_ndbcluster::delete_table");
4800 4801 4802
  DBUG_PRINT("enter", ("name: %s", name));
  set_dbname(name);
  set_tabname(name);
4803

4804 4805
  if (check_ndb_connection())
    DBUG_RETURN(HA_ERR_NO_CONNECTION);
4806 4807

  /* Call ancestor function to delete .ndb file */
4808
  handler::delete_table(name);
4809 4810
  
  /* Drop the table from NDB */
4811 4812 4813 4814 4815
  DBUG_RETURN(drop_table());
}


/*
4816
  Drop table in NDB Cluster
4817 4818 4819 4820
 */

int ha_ndbcluster::drop_table()
{
4821
  THD *thd= current_thd;
4822 4823
  Ndb *ndb= get_ndb();
  NdbDictionary::Dictionary *dict= ndb->getDictionary();
4824

4825 4826 4827 4828
  DBUG_ENTER("drop_table");
  DBUG_PRINT("enter", ("Deleting %s", m_tabname));
  
  release_metadata();
4829
  while (dict->dropTable(m_tabname)) 
4830 4831
  {
    const NdbError err= dict->getNdbError();
4832 4833 4834 4835 4836 4837 4838 4839 4840
    switch (err.status)
    {
      case NdbError::TemporaryError:
        if (!thd->killed)
          continue; // retry indefinitly
        break;
      default:
        break;
    }
4841
    ERR_RETURN(dict->getNdbError());
4842 4843
  }

4844 4845 4846 4847
  DBUG_RETURN(0);
}


4848
ulonglong ha_ndbcluster::get_auto_increment()
4849
{  
4850 4851
  int cache_size;
  Uint64 auto_value;
4852 4853
  DBUG_ENTER("get_auto_increment");
  DBUG_PRINT("enter", ("m_tabname: %s", m_tabname));
4854
  Ndb *ndb= get_ndb();
4855
   
4856
  if (m_rows_inserted > m_rows_to_insert)
4857
  {
4858 4859
    /* We guessed too low */
    m_rows_to_insert+= m_autoincrement_prefetch;
4860
  }
serg@serg.mylan's avatar
serg@serg.mylan committed
4861
  cache_size= 
4862 4863 4864 4865
    (int) ((m_rows_to_insert - m_rows_inserted < m_autoincrement_prefetch) ?
           m_rows_to_insert - m_rows_inserted :
           ((m_rows_to_insert > m_autoincrement_prefetch) ?
            m_rows_to_insert : m_autoincrement_prefetch));
4866
  uint retries= NDB_AUTO_INCREMENT_RETRIES;
4867 4868
  int retry_sleep= 30; /* 30 milliseconds, transaction */
  for (;;)
4869
  {
4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886
    if (m_skip_auto_increment &&
        ndb->readAutoIncrementValue((const NDBTAB *) m_table, auto_value) ||
        ndb->getAutoIncrementValue((const NDBTAB *) m_table,
                                   auto_value, cache_size))
    {
      if (--retries &&
          ndb->getNdbError().status == NdbError::TemporaryError);
      {
        my_sleep(retry_sleep);
        continue;
      }
      const NdbError err= ndb->getNdbError();
      sql_print_error("Error %lu in ::get_auto_increment(): %s",
                      (ulong) err.code, err.message);
      DBUG_RETURN(~(ulonglong) 0);
    }
    break;
4887
  }
4888
  DBUG_RETURN((longlong)auto_value);
4889 4890 4891 4892 4893 4894 4895 4896
}


/*
  Constructor for the NDB Cluster table handler 
 */

ha_ndbcluster::ha_ndbcluster(TABLE *table_arg):
4897
  handler(&ndbcluster_hton, table_arg),
4898 4899 4900
  m_active_trans(NULL),
  m_active_cursor(NULL),
  m_table(NULL),
4901
  m_table_version(-1),
4902
  m_table_info(NULL),
4903
  m_table_flags(HA_REC_NOT_IN_SEQ |
4904 4905 4906 4907
                HA_NULL_IN_KEY |
                HA_AUTO_PART_KEY |
                HA_NO_PREFIX_CHAR_KEYS |
                HA_NEED_READ_RANGE_BUFFER |
4908
                HA_CAN_GEOMETRY |
4909 4910
                HA_CAN_BIT_FIELD |
                HA_PARTIAL_COLUMN_READ),
4911
  m_share(0),
4912
  m_use_write(FALSE),
4913
  m_ignore_dup_key(FALSE),
4914
  m_has_unique_index(FALSE),
4915 4916
  m_primary_key_update(FALSE),
  m_retrieve_all_fields(FALSE),
4917
  m_retrieve_primary_key(FALSE),
4918 4919 4920
  m_rows_to_insert((ha_rows) 1),
  m_rows_inserted((ha_rows) 0),
  m_bulk_insert_rows((ha_rows) 1024),
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
4921
  m_rows_changed((ha_rows) 0),
4922
  m_bulk_insert_not_flushed(FALSE),
4923 4924
  m_delete_cannot_batch(FALSE),
  m_update_cannot_batch(FALSE),
4925 4926 4927
  m_ops_pending(0),
  m_skip_auto_increment(TRUE),
  m_blobs_pending(0),
4928
  m_blobs_offset(0),
4929 4930
  m_blobs_buffer(0),
  m_blobs_buffer_size(0),
4931 4932 4933
  m_dupkey((uint) -1),
  m_ha_not_exact_count(FALSE),
  m_force_send(TRUE),
4934
  m_autoincrement_prefetch((ha_rows) 32),
4935
  m_transaction_on(TRUE),
4936
  m_cond(NULL),
mskold@mysql.com's avatar
mskold@mysql.com committed
4937
  m_multi_cursor(NULL)
4938
{
4939
  int i;
4940
 
4941 4942 4943 4944 4945
  DBUG_ENTER("ha_ndbcluster");

  m_tabname[0]= '\0';
  m_dbname[0]= '\0';

4946
  records= ~(ha_rows)0; // uninitialized
4947 4948
  block_size= 1024;

4949 4950
  for (i= 0; i < MAX_KEY; i++)
  {
4951 4952 4953 4954
    m_index[i].type= UNDEFINED_INDEX;
    m_index[i].unique_index= NULL;
    m_index[i].index= NULL;
    m_index[i].unique_index_attrid_map= NULL;
4955 4956
  }

4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968
  DBUG_VOID_RETURN;
}


/*
  Destructor for NDB Cluster table handler
 */

ha_ndbcluster::~ha_ndbcluster() 
{
  DBUG_ENTER("~ha_ndbcluster");

4969 4970
  if (m_share)
    free_share(m_share);
4971
  release_metadata();
4972 4973
  my_free(m_blobs_buffer, MYF(MY_ALLOW_ZERO_PTR));
  m_blobs_buffer= 0;
4974 4975

  // Check for open cursor/transaction
4976 4977
  if (m_active_cursor) {
  }
4978
  DBUG_ASSERT(m_active_cursor == NULL);
4979 4980
  if (m_active_trans) {
  }
4981 4982
  DBUG_ASSERT(m_active_trans == NULL);

4983 4984 4985 4986 4987 4988 4989
  // Discard any generated condition
  DBUG_PRINT("info", ("Deleting generated condition"));
  if (m_cond)
  {
    delete m_cond;
    m_cond= NULL;
  }
4990

4991 4992 4993 4994
  DBUG_VOID_RETURN;
}


mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
4995

4996 4997 4998 4999 5000 5001 5002 5003
/*
  Open a table for further use
  - fetch metadata for this table from NDB
  - check that table exists
*/

int ha_ndbcluster::open(const char *name, int mode, uint test_if_locked)
{
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
5004
  int res;
5005 5006 5007 5008 5009 5010 5011 5012
  KEY *key;
  DBUG_ENTER("open");
  DBUG_PRINT("enter", ("name: %s mode: %d test_if_locked: %d",
                       name, mode, test_if_locked));
  
  // Setup ref_length to make room for the whole 
  // primary key to be written in the ref variable
  
5013
  if (table->s->primary_key != MAX_KEY) 
5014
  {
5015
    key= table->key_info+table->s->primary_key;
5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026
    ref_length= key->key_length;
    DBUG_PRINT("info", (" ref_length: %d", ref_length));
  }
  // Init table lock structure 
  if (!(m_share=get_share(name)))
    DBUG_RETURN(1);
  thr_lock_data_init(&m_share->lock,&m_lock,(void*) 0);
  
  set_dbname(name);
  set_tabname(name);
  
5027 5028 5029 5030 5031 5032
  if ((res= check_ndb_connection()) || 
      (res= get_metadata(name)))
  {
    free_share(m_share);
    m_share= 0;
    DBUG_RETURN(res);
5033
  }
5034
  while (1)
5035 5036
  {
    Ndb *ndb= get_ndb();
5037 5038
    if (ndb->setDatabaseName(m_dbname))
    {
5039 5040
      res= ndb_to_mysql_error(&ndb->getNdbError());
      break;
5041
    }
stewart@willster.(none)'s avatar
stewart@willster.(none) committed
5042 5043 5044
    struct Ndb_statistics stat;
    res= ndb_get_table_statistics(NULL, false, ndb, m_tabname, &stat);
    records= stat.row_count;
5045 5046
    if(!res)
      res= info(HA_STATUS_CONST);
5047
    break;
5048
  }
5049 5050 5051 5052 5053 5054 5055 5056
  if (res)
  {
    free_share(m_share);
    m_share= 0;
    release_metadata();
    DBUG_RETURN(res);
  }
  DBUG_RETURN(0);
5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067
}


/*
  Close the table
  - release resources setup by open()
 */

int ha_ndbcluster::close(void)
{
  DBUG_ENTER("close");  
5068
  free_share(m_share); m_share= 0;
5069 5070 5071 5072 5073
  release_metadata();
  DBUG_RETURN(0);
}


5074
Thd_ndb* ha_ndbcluster::seize_thd_ndb()
5075
{
5076 5077
  Thd_ndb *thd_ndb;
  DBUG_ENTER("seize_thd_ndb");
5078

5079
  thd_ndb= new Thd_ndb();
5080 5081 5082 5083 5084
  if (thd_ndb == NULL)
  {
    my_errno= HA_ERR_OUT_OF_MEM;
    return NULL;
  }
5085 5086 5087
  thd_ndb->ndb->getDictionary()->set_local_table_data_size(
    sizeof(Ndb_local_table_statistics)
    );
5088
  if (thd_ndb->ndb->init(max_transactions) != 0)
5089
  {
5090
    ERR_PRINT(thd_ndb->ndb->getNdbError());
5091 5092 5093 5094 5095 5096
    /*
      TODO 
      Alt.1 If init fails because to many allocated Ndb 
      wait on condition for a Ndb object to be released.
      Alt.2 Seize/release from pool, wait until next release 
    */
5097 5098
    delete thd_ndb;
    thd_ndb= NULL;
5099
  }
5100
  DBUG_RETURN(thd_ndb);
5101 5102 5103
}


5104
void ha_ndbcluster::release_thd_ndb(Thd_ndb* thd_ndb)
5105
{
5106 5107
  DBUG_ENTER("release_thd_ndb");
  delete thd_ndb;
5108 5109 5110 5111 5112
  DBUG_VOID_RETURN;
}


/*
magnus@neptunus.(none)'s avatar
magnus@neptunus.(none) committed
5113
  If this thread already has a Thd_ndb object allocated
5114
  in current THD, reuse it. Otherwise
magnus@neptunus.(none)'s avatar
magnus@neptunus.(none) committed
5115
  seize a Thd_ndb object, assign it to current THD and use it.
5116 5117 5118
 
*/

5119
Ndb* check_ndb_in_thd(THD* thd)
5120
{
5121
  Thd_ndb *thd_ndb= get_thd_ndb(thd);
5122
  if (!thd_ndb)
5123
  {
magnus@neptunus.(none)'s avatar
magnus@neptunus.(none) committed
5124
    if (!(thd_ndb= ha_ndbcluster::seize_thd_ndb()))
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5125
      return NULL;
5126
    set_thd_ndb(thd, thd_ndb);
5127
  }
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5128
  return thd_ndb->ndb;
5129 5130
}

magnus@neptunus.(none)'s avatar
magnus@neptunus.(none) committed
5131

5132

5133
int ha_ndbcluster::check_ndb_connection(THD* thd)
5134
{
5135
  Ndb *ndb;
5136 5137
  DBUG_ENTER("check_ndb_connection");
  
5138
  if (!(ndb= check_ndb_in_thd(thd)))
5139
    DBUG_RETURN(HA_ERR_NO_CONNECTION);
5140 5141 5142 5143
  if (ndb->setDatabaseName(m_dbname))
  {
    ERR_RETURN(ndb->getNdbError());
  }
5144 5145 5146
  DBUG_RETURN(0);
}

magnus@neptunus.(none)'s avatar
magnus@neptunus.(none) committed
5147

5148
int ndbcluster_close_connection(THD *thd)
5149
{
5150
  Thd_ndb *thd_ndb= get_thd_ndb(thd);
5151
  DBUG_ENTER("ndbcluster_close_connection");
5152 5153
  if (thd_ndb)
  {
5154
    ha_ndbcluster::release_thd_ndb(thd_ndb);
5155
    set_thd_ndb(thd, NULL); // not strictly required but does not hurt either
5156
  }
5157
  DBUG_RETURN(0);
5158 5159 5160 5161 5162 5163 5164
}


/*
  Try to discover one table from NDB
 */

5165
int ndbcluster_discover(THD* thd, const char *db, const char *name,
5166
                        const void** frmblob, uint* frmlen)
5167 5168 5169 5170
{
  uint len;
  const void* data;
  const NDBTAB* tab;
5171
  Ndb* ndb;
5172
  DBUG_ENTER("ndbcluster_discover");
5173
  DBUG_PRINT("enter", ("db: %s, name: %s", db, name)); 
5174

5175 5176
  if (!(ndb= check_ndb_in_thd(thd)))
    DBUG_RETURN(HA_ERR_NO_CONNECTION);  
5177 5178 5179 5180
  if (ndb->setDatabaseName(db))
  {
    ERR_RETURN(ndb->getNdbError());
  }
5181
  NDBDICT* dict= ndb->getDictionary();
5182
  dict->set_local_table_data_size(sizeof(Ndb_local_table_statistics));
5183 5184 5185 5186 5187
  dict->invalidateTable(name);
  if (!(tab= dict->getTable(name)))
  {    
    const NdbError err= dict->getNdbError();
    if (err.code == 709)
5188
      DBUG_RETURN(-1);
5189
    ERR_RETURN(err);
5190 5191 5192 5193 5194 5195
  }
  DBUG_PRINT("info", ("Found table %s", tab->getName()));
  
  len= tab->getFrmLength();  
  if (len == 0 || tab->getFrmData() == NULL)
  {
5196 5197
    DBUG_PRINT("error", ("No frm data found."));
    DBUG_RETURN(1);
5198 5199 5200
  }
  
  if (unpackfrm(&data, &len, tab->getFrmData()))
5201 5202 5203 5204
  {
    DBUG_PRINT("error", ("Could not unpack table"));
    DBUG_RETURN(1);
  }
5205 5206 5207 5208 5209 5210 5211 5212

  *frmlen= len;
  *frmblob= data;
  
  DBUG_RETURN(0);
}

/*
5213
  Check if a table exists in NDB
5214

5215
 */
5216

5217
int ndbcluster_table_exists_in_engine(THD* thd, const char *db, const char *name)
5218 5219 5220
{
  const NDBTAB* tab;
  Ndb* ndb;
5221
  DBUG_ENTER("ndbcluster_table_exists_in_engine");
5222
  DBUG_PRINT("enter", ("db: %s, name: %s", db, name));
5223 5224

  if (!(ndb= check_ndb_in_thd(thd)))
5225
    DBUG_RETURN(HA_ERR_NO_CONNECTION);
5226 5227 5228 5229
  if (ndb->setDatabaseName(db))
  {
    ERR_RETURN(ndb->getNdbError());
  }
5230
  NDBDICT* dict= ndb->getDictionary();
5231
  dict->set_local_table_data_size(sizeof(Ndb_local_table_statistics));
5232 5233
  dict->invalidateTable(name);
  if (!(tab= dict->getTable(name)))
5234
  {
5235
    ERR_RETURN(dict->getNdbError());
5236
  }
5237

5238
  DBUG_PRINT("info", ("Found table %s", tab->getName()));
5239
  DBUG_RETURN(HA_ERR_TABLE_EXIST);
5240 5241
}

5242 5243


5244
extern "C" byte* tables_get_key(const char *entry, uint *length,
5245
                                my_bool not_used __attribute__((unused)))
5246 5247 5248 5249 5250 5251
{
  *length= strlen(entry);
  return (byte*) entry;
}


5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265
/*
  Drop a database in NDB Cluster
 */

int ndbcluster_drop_database(const char *path)
{
  DBUG_ENTER("ndbcluster_drop_database");
  THD *thd= current_thd;
  char dbname[FN_HEADLEN];
  Ndb* ndb;
  NdbDictionary::Dictionary::List list;
  uint i;
  char *tabname;
  List<char> drop_list;
5266
  int ret= 0;
5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289
  ha_ndbcluster::set_dbname(path, (char *)&dbname);
  DBUG_PRINT("enter", ("db: %s", dbname));
  
  if (!(ndb= check_ndb_in_thd(thd)))
    DBUG_RETURN(HA_ERR_NO_CONNECTION);
  
  // List tables in NDB
  NDBDICT *dict= ndb->getDictionary();
  if (dict->listObjects(list, 
                        NdbDictionary::Object::UserTable) != 0)
    ERR_RETURN(dict->getNdbError());
  for (i= 0 ; i < list.count ; i++)
  {
    NdbDictionary::Dictionary::List::Element& t= list.elements[i];
    DBUG_PRINT("info", ("Found %s/%s in NDB", t.database, t.name));     
    
    // Add only tables that belongs to db
    if (my_strcasecmp(system_charset_info, t.database, dbname))
      continue;
    DBUG_PRINT("info", ("%s must be dropped", t.name));     
    drop_list.push_back(thd->strdup(t.name));
  }
  // Drop any tables belonging to database
5290 5291 5292 5293
  if (ndb->setDatabaseName(dbname))
  {
    ERR_RETURN(ndb->getNdbError());
  }
5294 5295
  List_iterator_fast<char> it(drop_list);
  while ((tabname=it++))
5296
  {
5297
    while (dict->dropTable(tabname))
5298 5299
    {
      const NdbError err= dict->getNdbError();
5300 5301 5302 5303 5304 5305 5306 5307 5308 5309
      switch (err.status)
      {
        case NdbError::TemporaryError:
          if (!thd->killed)
            continue; // retry indefinitly
          break;
        default:
          break;
      }
      if (err.code != 709) // 709: No such table existed
5310 5311
      {
        ERR_PRINT(err);
5312
        ret= ndb_to_mysql_error(&err);
5313
      }
5314
      break;
5315 5316 5317
    }
  }
  DBUG_RETURN(ret);      
5318 5319 5320
}


5321
int ndbcluster_find_files(THD *thd,const char *db,const char *path,
5322
                          const char *wild, bool dir, List<char> *files)
5323
{
5324 5325 5326
  DBUG_ENTER("ndbcluster_find_files");
  DBUG_PRINT("enter", ("db: %s", db));
  { // extra bracket to avoid gcc 2.95.3 warning
5327
  uint i;
5328
  Ndb* ndb;
5329
  char name[FN_REFLEN];
5330
  HASH ndb_tables, ok_tables;
5331
  NdbDictionary::Dictionary::List list;
5332 5333 5334 5335

  if (!(ndb= check_ndb_in_thd(thd)))
    DBUG_RETURN(HA_ERR_NO_CONNECTION);

5336
  if (dir)
5337
    DBUG_RETURN(0); // Discover of databases not yet supported
5338

5339
  // List tables in NDB
5340
  NDBDICT *dict= ndb->getDictionary();
5341
  if (dict->listObjects(list, 
5342
                        NdbDictionary::Object::UserTable) != 0)
5343
    ERR_RETURN(dict->getNdbError());
5344

5345
  if (hash_init(&ndb_tables, system_charset_info,list.count,0,0,
5346
                (hash_get_key)tables_get_key,0,0))
5347 5348 5349 5350 5351 5352
  {
    DBUG_PRINT("error", ("Failed to init HASH ndb_tables"));
    DBUG_RETURN(-1);
  }

  if (hash_init(&ok_tables, system_charset_info,32,0,0,
5353
                (hash_get_key)tables_get_key,0,0))
5354 5355 5356 5357 5358 5359
  {
    DBUG_PRINT("error", ("Failed to init HASH ok_tables"));
    hash_free(&ndb_tables);
    DBUG_RETURN(-1);
  }  

5360 5361 5362
  for (i= 0 ; i < list.count ; i++)
  {
    NdbDictionary::Dictionary::List::Element& t= list.elements[i];
5363
    DBUG_PRINT("info", ("Found %s/%s in NDB", t.database, t.name));     
5364

5365 5366 5367
    // Add only tables that belongs to db
    if (my_strcasecmp(system_charset_info, t.database, db))
      continue;
5368

5369
    // Apply wildcard to list of tables in NDB
5370
    if (wild)
5371
    {
5372 5373
      if (lower_case_table_names)
      {
5374 5375
        if (wild_case_compare(files_charset_info, t.name, wild))
          continue;
5376 5377
      }
      else if (wild_compare(t.name,wild,0))
5378
        continue;
5379
    }
5380 5381
    DBUG_PRINT("info", ("Inserting %s into ndb_tables hash", t.name));     
    my_hash_insert(&ndb_tables, (byte*)thd->strdup(t.name));
5382 5383
  }

5384 5385 5386 5387 5388
  char *file_name;
  List_iterator<char> it(*files);
  List<char> delete_list;
  while ((file_name=it++))
  {
5389
    bool file_on_disk= false;
5390 5391 5392 5393
    DBUG_PRINT("info", ("%s", file_name));     
    if (hash_search(&ndb_tables, file_name, strlen(file_name)))
    {
      DBUG_PRINT("info", ("%s existed in NDB _and_ on disk ", file_name));
5394
      file_on_disk= true;
5395 5396
    }
    
5397
    // Check for .ndb file with this name
5398
    (void)strxnmov(name, FN_REFLEN, 
5399
                   mysql_data_home,"/",db,"/",file_name,ha_ndb_ext,NullS);
5400
    DBUG_PRINT("info", ("Check access for %s", name));
5401
    if (access(name, F_OK))
5402 5403 5404
    {
      DBUG_PRINT("info", ("%s did not exist on disk", name));     
      // .ndb file did not exist on disk, another table type
5405
      if (file_on_disk)
5406 5407 5408 5409 5410
      {
	// Ignore this ndb table
	gptr record=  hash_search(&ndb_tables, file_name, strlen(file_name));
	DBUG_ASSERT(record);
	hash_delete(&ndb_tables, record);
5411 5412 5413 5414
	push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
			    ER_TABLE_EXISTS_ERROR,
			    "Local table %s.%s shadows ndb table",
			    db, file_name);
5415
      }
5416 5417 5418 5419
      continue;
    }
    if (file_on_disk) 
    {
5420
      // File existed in NDB and as frm file, put in ok_tables list
5421
      my_hash_insert(&ok_tables, (byte*)file_name);
5422
      continue;
5423
    }
5424 5425 5426
    DBUG_PRINT("info", ("%s existed on disk", name));     
    // The .ndb file exists on disk, but it's not in list of tables in ndb
    // Verify that handler agrees table is gone.
5427
    if (ndbcluster_table_exists_in_engine(thd, db, file_name) == HA_ERR_NO_SUCH_TABLE)
5428 5429 5430 5431 5432 5433 5434
    {
      DBUG_PRINT("info", ("NDB says %s does not exists", file_name));     
      it.remove();
      // Put in list of tables to remove from disk
      delete_list.push_back(thd->strdup(file_name));
    }
  }
5435

5436 5437 5438 5439
  // Check for new files to discover
  DBUG_PRINT("info", ("Checking for new files to discover"));       
  List<char> create_list;
  for (i= 0 ; i < ndb_tables.records ; i++)
5440
  {
5441 5442
    file_name= hash_element(&ndb_tables, i);
    if (!hash_search(&ok_tables, file_name, strlen(file_name)))
5443
    {
5444 5445 5446 5447 5448 5449
      DBUG_PRINT("info", ("%s must be discovered", file_name));       
      // File is in list of ndb tables and not in ok_tables
      // This table need to be created
      create_list.push_back(thd->strdup(file_name));
    }
  }
5450

5451 5452
  // Lock mutex before deleting and creating frm files
  pthread_mutex_lock(&LOCK_open);
5453

5454 5455 5456 5457 5458
  if (!global_read_lock)
  {
    // Delete old files
    List_iterator_fast<char> it3(delete_list);
    while ((file_name=it3++))
5459 5460
    {
      DBUG_PRINT("info", ("Remove table %s/%s", db, file_name));
5461 5462 5463 5464
      // Delete the table and all related files
      TABLE_LIST table_list;
      bzero((char*) &table_list,sizeof(table_list));
      table_list.db= (char*) db;
5465
      table_list.alias= table_list.table_name= (char*)file_name;
5466
      (void)mysql_rm_table_part2(thd, &table_list,
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5467 5468 5469 5470
                                                                 /* if_exists */ FALSE,
                                                                 /* drop_temporary */ FALSE,
                                                                 /* drop_view */ FALSE,
                                                                 /* dont_log_query*/ TRUE);
5471 5472
      /* Clear error message that is returned when table is deleted */
      thd->clear_error();
5473 5474 5475
    }
  }

5476 5477 5478 5479
  // Create new files
  List_iterator_fast<char> it2(create_list);
  while ((file_name=it2++))
  {  
5480
    DBUG_PRINT("info", ("Table %s need discovery", file_name));
5481
    if (ha_create_table_from_engine(thd, db, file_name) == 0)
5482
      files->push_back(thd->strdup(file_name)); 
5483 5484 5485 5486 5487
  }

  pthread_mutex_unlock(&LOCK_open);      
  
  hash_free(&ok_tables);
5488
  hash_free(&ndb_tables);
5489
  } // extra bracket to avoid gcc 2.95.3 warning
5490
  DBUG_RETURN(0);    
5491 5492 5493 5494 5495 5496 5497 5498
}


/*
  Initialise all gloal variables before creating 
  a NDB Cluster table handler
 */

5499 5500 5501 5502 5503 5504 5505
/* Call back after cluster connect */
static int connect_callback()
{
  update_status_variables(g_ndb_cluster_connection);
  return 0;
}

5506
bool ndbcluster_init()
5507
{
5508
  int res;
5509
  DBUG_ENTER("ndbcluster_init");
5510 5511 5512 5513

  if (have_ndbcluster != SHOW_OPTION_YES)
    goto ndbcluster_init_error;

5514
  // Set connectstring if specified
5515 5516
  if (opt_ndbcluster_connectstring != 0)
    DBUG_PRINT("connectstring", ("%s", opt_ndbcluster_connectstring));     
5517
  if ((g_ndb_cluster_connection=
5518
       new Ndb_cluster_connection(opt_ndbcluster_connectstring)) == 0)
5519
  {
5520
    DBUG_PRINT("error",("Ndb_cluster_connection(%s)",
5521
                        opt_ndbcluster_connectstring));
5522
    my_errno= HA_ERR_OUT_OF_MEM;
5523
    goto ndbcluster_init_error;
5524
  }
tomas@poseidon.ndb.mysql.com's avatar
ndb:  
tomas@poseidon.ndb.mysql.com committed
5525 5526
  {
    char buf[128];
5527
    my_snprintf(buf, sizeof(buf), "mysqld --server-id=%lu", server_id);
tomas@poseidon.ndb.mysql.com's avatar
ndb:  
tomas@poseidon.ndb.mysql.com committed
5528 5529
    g_ndb_cluster_connection->set_name(buf);
  }
5530 5531 5532
  g_ndb_cluster_connection->set_optimized_node_selection
    (opt_ndb_optimized_node_selection);

5533
  // Create a Ndb object to open the connection  to NDB
5534 5535 5536
  if ( (g_ndb= new Ndb(g_ndb_cluster_connection, "sys")) == 0 )
  {
    DBUG_PRINT("error", ("failed to create global ndb object"));
5537
    my_errno= HA_ERR_OUT_OF_MEM;
5538 5539
    goto ndbcluster_init_error;
  }
5540
  g_ndb->getDictionary()->set_local_table_data_size(sizeof(Ndb_local_table_statistics));
5541 5542 5543
  if (g_ndb->init() != 0)
  {
    ERR_PRINT (g_ndb->getNdbError());
5544
    goto ndbcluster_init_error;
5545
  }
5546

5547
  if ((res= g_ndb_cluster_connection->connect(0,0,0)) == 0)
5548
  {
5549
    connect_callback();
5550
    DBUG_PRINT("info",("NDBCLUSTER storage engine at %s on port %d",
5551 5552
                       g_ndb_cluster_connection->get_connected_host(),
                       g_ndb_cluster_connection->get_connected_port()));
5553
    g_ndb_cluster_connection->wait_until_ready(10,3);
5554
  } 
5555
  else if (res == 1)
5556
  {
5557
    if (g_ndb_cluster_connection->start_connect_thread(connect_callback)) 
5558
    {
5559
      DBUG_PRINT("error", ("g_ndb_cluster_connection->start_connect_thread()"));
5560 5561
      goto ndbcluster_init_error;
    }
5562
#ifndef DBUG_OFF
5563 5564
    {
      char buf[1024];
5565
      DBUG_PRINT("info",
5566 5567 5568 5569
                 ("NDBCLUSTER storage engine not started, "
                  "will connect using %s",
                  g_ndb_cluster_connection->
                  get_connectstring(buf,sizeof(buf))));
5570
    }
5571
#endif
5572
  }
5573
  else
5574 5575 5576
  {
    DBUG_ASSERT(res == -1);
    DBUG_PRINT("error", ("permanent error"));
5577
    goto ndbcluster_init_error;
5578
  }
5579
  
5580 5581 5582
  (void) hash_init(&ndbcluster_open_tables,system_charset_info,32,0,0,
                   (hash_get_key) ndbcluster_get_key,0,0);
  pthread_mutex_init(&ndbcluster_mutex,MY_MUTEX_INIT_FAST);
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5583 5584
  pthread_mutex_init(&LOCK_ndb_util_thread, MY_MUTEX_INIT_FAST);
  pthread_cond_init(&COND_ndb_util_thread, NULL);
5585

mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5586

jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
5587
  ndb_cache_check_time = opt_ndb_cache_check_time;
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5588 5589 5590 5591 5592
  // Create utility thread
  pthread_t tmp;
  if (pthread_create(&tmp, &connection_attrib, ndb_util_thread_func, 0))
  {
    DBUG_PRINT("error", ("Could not create ndb utility thread"));
5593 5594 5595 5596
    hash_free(&ndbcluster_open_tables);
    pthread_mutex_destroy(&ndbcluster_mutex);
    pthread_mutex_destroy(&LOCK_ndb_util_thread);
    pthread_cond_destroy(&COND_ndb_util_thread);
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5597 5598 5599
    goto ndbcluster_init_error;
  }
  
5600
  ndbcluster_inited= 1;
5601
  DBUG_RETURN(FALSE);
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5602

5603
ndbcluster_init_error:
5604
  if (g_ndb)
5605 5606 5607 5608 5609
    delete g_ndb;
  g_ndb= NULL;
  if (g_ndb_cluster_connection)
    delete g_ndb_cluster_connection;
  g_ndb_cluster_connection= NULL;
5610 5611
  have_ndbcluster= SHOW_OPTION_DISABLED;	// If we couldn't use handler
  DBUG_RETURN(TRUE);
5612 5613 5614 5615 5616 5617
}


/*
  End use of the NDB Cluster table handler
  - free all global variables allocated by 
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5618
    ndbcluster_init()
5619 5620 5621 5622 5623
*/

bool ndbcluster_end()
{
  DBUG_ENTER("ndbcluster_end");
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5624

5625 5626 5627
  if (!ndbcluster_inited)
    DBUG_RETURN(0);

mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5628 5629 5630 5631 5632 5633
  // Kill ndb utility thread
  (void) pthread_mutex_lock(&LOCK_ndb_util_thread);  
  DBUG_PRINT("exit",("killing ndb util thread: %lx", ndb_util_thread));
  (void) pthread_cond_signal(&COND_ndb_util_thread);
  (void) pthread_mutex_unlock(&LOCK_ndb_util_thread);

5634
  if (g_ndb)
5635 5636
  {
#ifndef DBUG_OFF
5637 5638
    Ndb::Free_list_usage tmp;
    tmp.m_name= 0;
5639 5640 5641 5642 5643 5644 5645 5646 5647 5648
    while (g_ndb->get_free_list_usage(&tmp))
    {
      uint leaked= (uint) tmp.m_created - tmp.m_free;
      if (leaked)
        fprintf(stderr, "NDB: Found %u %s%s that %s not been released\n",
                leaked, tmp.m_name,
                (leaked == 1)?"":"'s",
                (leaked == 1)?"has":"have");
    }
#endif
5649
    delete g_ndb;
5650
    g_ndb= NULL;
5651
  }
5652
  delete g_ndb_cluster_connection;
5653
  g_ndb_cluster_connection= NULL;
5654

5655 5656
  hash_free(&ndbcluster_open_tables);
  pthread_mutex_destroy(&ndbcluster_mutex);
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5657 5658
  pthread_mutex_destroy(&LOCK_ndb_util_thread);
  pthread_cond_destroy(&COND_ndb_util_thread);
5659 5660 5661 5662
  ndbcluster_inited= 0;
  DBUG_RETURN(0);
}

5663 5664 5665 5666 5667
/*
  Static error print function called from
  static handler method ndbcluster_commit
  and ndbcluster_rollback
*/
5668 5669

void ndbcluster_print_error(int error, const NdbOperation *error_op)
5670
{
5671 5672
  DBUG_ENTER("ndbcluster_print_error");
  TABLE tab;
5673
  const char *tab_name= (error_op) ? error_op->getTableName() : "";
5674
  tab.alias= (char *) tab_name;
5675
  ha_ndbcluster error_handler(&tab);
5676
  tab.file= &error_handler;
5677
  error_handler.print_error(error, MYF(0));
ndbdev@ndbmaster.mysql.com's avatar
ndbdev@ndbmaster.mysql.com committed
5678
  DBUG_VOID_RETURN;
5679
}
5680

5681 5682 5683
/**
 * Set a given location from full pathname to database name
 *
5684
 */
5685
void ha_ndbcluster::set_dbname(const char *path_name, char *dbname)
5686 5687 5688 5689
{
  char *end, *ptr;
  
  /* Scan name from the end */
5690 5691 5692 5693 5694 5695
  ptr= strend(path_name)-1;
  while (ptr >= path_name && *ptr != '\\' && *ptr != '/') {
    ptr--;
  }
  ptr--;
  end= ptr;
5696 5697 5698 5699
  while (ptr >= path_name && *ptr != '\\' && *ptr != '/') {
    ptr--;
  }
  uint name_len= end - ptr;
5700 5701
  memcpy(dbname, ptr + 1, name_len);
  dbname[name_len]= '\0';
5702 5703
#ifdef __WIN__
  /* Put to lower case */
5704 5705
  
  ptr= dbname;
5706 5707
  
  while (*ptr != '\0') {
5708
    *ptr= tolower(*ptr);
5709 5710 5711 5712 5713
    ptr++;
  }
#endif
}

5714 5715 5716 5717 5718 5719 5720 5721 5722
/*
  Set m_dbname from full pathname to table file
 */

void ha_ndbcluster::set_dbname(const char *path_name)
{
  set_dbname(path_name, m_dbname);
}

5723 5724 5725 5726 5727 5728 5729 5730 5731 5732
/**
 * Set a given location from full pathname to table file
 *
 */
void
ha_ndbcluster::set_tabname(const char *path_name, char * tabname)
{
  char *end, *ptr;
  
  /* Scan name from the end */
5733 5734
  end= strend(path_name)-1;
  ptr= end;
5735 5736 5737
  while (ptr >= path_name && *ptr != '\\' && *ptr != '/') {
    ptr--;
  }
5738
  uint name_len= end - ptr;
5739
  memcpy(tabname, ptr + 1, end - ptr);
5740
  tabname[name_len]= '\0';
5741 5742
#ifdef __WIN__
  /* Put to lower case */
5743
  ptr= tabname;
5744 5745 5746 5747 5748 5749 5750 5751 5752
  
  while (*ptr != '\0') {
    *ptr= tolower(*ptr);
    ptr++;
  }
#endif
}

/*
5753
  Set m_tabname from full pathname to table file 
5754 5755
 */

5756
void ha_ndbcluster::set_tabname(const char *path_name)
5757
{
5758
  set_tabname(path_name, m_tabname);
5759 5760 5761 5762
}


ha_rows 
5763 5764 5765 5766
ha_ndbcluster::records_in_range(uint inx, key_range *min_key,
                                key_range *max_key)
{
  KEY *key_info= table->key_info + inx;
5767
  uint key_length= key_info->key_length;
5768
  NDB_INDEX_TYPE idx_type= get_index_type(inx);  
5769 5770

  DBUG_ENTER("records_in_range");
5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784
  // Prevent partial read of hash indexes by returning HA_POS_ERROR
  if ((idx_type == UNIQUE_INDEX || idx_type == PRIMARY_KEY_INDEX) &&
      ((min_key && min_key->length < key_length) ||
       (max_key && max_key->length < key_length)))
    DBUG_RETURN(HA_POS_ERROR);
  
  // Read from hash index with full key
  // This is a "const" table which returns only one record!      
  if ((idx_type != ORDERED_INDEX) &&
      ((min_key && min_key->length == key_length) || 
       (max_key && max_key->length == key_length)))
    DBUG_RETURN(1);
  
  DBUG_RETURN(10); /* Good guess when you don't know anything */
5785 5786
}

5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813
ulong ha_ndbcluster::table_flags(void) const
{
  if (m_ha_not_exact_count)
    return m_table_flags | HA_NOT_EXACT_COUNT;
  else
    return m_table_flags;
}
const char * ha_ndbcluster::table_type() const 
{
  return("ndbcluster");
}
uint ha_ndbcluster::max_supported_record_length() const
{ 
  return NDB_MAX_TUPLE_SIZE;
}
uint ha_ndbcluster::max_supported_keys() const
{
  return MAX_KEY;
}
uint ha_ndbcluster::max_supported_key_parts() const 
{
  return NDB_MAX_NO_OF_ATTRIBUTES_IN_KEY;
}
uint ha_ndbcluster::max_supported_key_length() const
{
  return NDB_MAX_KEY_SIZE;
}
pekka@mysql.com's avatar
pekka@mysql.com committed
5814 5815 5816 5817
uint ha_ndbcluster::max_supported_key_part_length() const
{
  return NDB_MAX_KEY_SIZE;
}
5818 5819 5820 5821 5822 5823 5824 5825 5826 5827
bool ha_ndbcluster::low_byte_first() const
{ 
#ifdef WORDS_BIGENDIAN
  return FALSE;
#else
  return TRUE;
#endif
}
bool ha_ndbcluster::has_transactions()
{
5828
  return TRUE;
5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842
}
const char* ha_ndbcluster::index_type(uint key_number)
{
  switch (get_index_type(key_number)) {
  case ORDERED_INDEX:
  case UNIQUE_ORDERED_INDEX:
  case PRIMARY_KEY_ORDERED_INDEX:
    return "BTREE";
  case UNIQUE_INDEX:
  case PRIMARY_KEY_INDEX:
  default:
    return "HASH";
  }
}
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5843

5844 5845
uint8 ha_ndbcluster::table_cache_type()
{
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5846 5847 5848 5849 5850 5851
  DBUG_ENTER("ha_ndbcluster::table_cache_type=HA_CACHE_TBL_ASKTRANSACT");
  DBUG_RETURN(HA_CACHE_TBL_ASKTRANSACT);
}


uint ndb_get_commitcount(THD *thd, char *dbname, char *tabname,
5852
                         Uint64 *commit_count)
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5853 5854 5855
{
  DBUG_ENTER("ndb_get_commitcount");

5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873
  char name[FN_REFLEN];
  NDB_SHARE *share;
  (void)strxnmov(name, FN_REFLEN, "./",dbname,"/",tabname,NullS);
  DBUG_PRINT("enter", ("name: %s", name));
  pthread_mutex_lock(&ndbcluster_mutex);
  if (!(share=(NDB_SHARE*) hash_search(&ndbcluster_open_tables,
                                       (byte*) name,
                                       strlen(name))))
  {
    pthread_mutex_unlock(&ndbcluster_mutex);
    DBUG_PRINT("info", ("Table %s not found in ndbcluster_open_tables",
                        name));
    DBUG_RETURN(1);
  }
  share->use_count++;
  pthread_mutex_unlock(&ndbcluster_mutex);

  pthread_mutex_lock(&share->mutex);
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5874 5875
  if (ndb_cache_check_time > 0)
  {
5876
    if (share->commit_count != 0)
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5877
    {
5878
      *commit_count= share->commit_count;
5879
#ifndef DBUG_OFF
5880
      char buff[22];
5881
#endif
5882 5883
      DBUG_PRINT("info", ("Getting commit_count: %s from share",
                          llstr(share->commit_count, buff)));
5884 5885 5886
      pthread_mutex_unlock(&share->mutex);
      free_share(share);
      DBUG_RETURN(0);
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5887 5888
    }
  }
5889
  DBUG_PRINT("info", ("Get commit_count from NDB"));
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5890 5891 5892
  Ndb *ndb;
  if (!(ndb= check_ndb_in_thd(thd)))
    DBUG_RETURN(1);
5893 5894 5895 5896
  if (ndb->setDatabaseName(dbname))
  {
    ERR_RETURN(ndb->getNdbError());
  }
5897 5898
  uint lock= share->commit_count_lock;
  pthread_mutex_unlock(&share->mutex);
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5899 5900

  struct Ndb_statistics stat;
5901
  if (ndb_get_table_statistics(NULL, false, ndb, tabname, &stat))
5902 5903
  {
    free_share(share);
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5904
    DBUG_RETURN(1);
5905 5906 5907
  }

  pthread_mutex_lock(&share->mutex);
5908
  if (share->commit_count_lock == lock)
5909
  {
5910
#ifndef DBUG_OFF
5911
    char buff[22];
5912
#endif
5913 5914
    DBUG_PRINT("info", ("Setting commit_count to %s",
                        llstr(stat.commit_count, buff)));
5915 5916 5917 5918 5919 5920 5921 5922 5923 5924
    share->commit_count= stat.commit_count;
    *commit_count= stat.commit_count;
  }
  else
  {
    DBUG_PRINT("info", ("Discarding commit_count, comit_count_lock changed"));
    *commit_count= 0;
  }
  pthread_mutex_unlock(&share->mutex);
  free_share(share);
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960
  DBUG_RETURN(0);
}


/*
  Check if a cached query can be used.
  This is done by comparing the supplied engine_data to commit_count of
  the table.
  The commit_count is either retrieved from the share for the table, where
  it has been cached by the util thread. If the util thread is not started,
  NDB has to be contacetd to retrieve the commit_count, this will introduce
  a small delay while waiting for NDB to answer.


  SYNOPSIS
  ndbcluster_cache_retrieval_allowed
    thd            thread handle
    full_name      concatenation of database name,
                   the null character '\0', and the table
                   name
    full_name_len  length of the full name,
                   i.e. len(dbname) + len(tablename) + 1

    engine_data    parameter retrieved when query was first inserted into
                   the cache. If the value of engine_data is changed,
                   all queries for this table should be invalidated.

  RETURN VALUE
    TRUE  Yes, use the query from cache
    FALSE No, don't use the cached query, and if engine_data
          has changed, all queries for this table should be invalidated

*/

static my_bool
ndbcluster_cache_retrieval_allowed(THD *thd,
5961 5962
                                   char *full_name, uint full_name_len,
                                   ulonglong *engine_data)
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5963 5964 5965 5966 5967
{
  Uint64 commit_count;
  bool is_autocommit= !(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN));
  char *dbname= full_name;
  char *tabname= dbname+strlen(dbname)+1;
5968
#ifndef DBUG_OFF
5969
  char buff[22], buff2[22];
5970
#endif
5971
  DBUG_ENTER("ndbcluster_cache_retrieval_allowed");
5972 5973
  DBUG_PRINT("enter", ("dbname: %s, tabname: %s, is_autocommit: %d",
                       dbname, tabname, is_autocommit));
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5974 5975

  if (!is_autocommit)
5976 5977
  {
    DBUG_PRINT("exit", ("No, don't use cache in transaction"));
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5978
    DBUG_RETURN(FALSE);
5979
  }
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5980 5981 5982

  if (ndb_get_commitcount(thd, dbname, tabname, &commit_count))
  {
5983 5984
    *engine_data= 0; /* invalidate */
    DBUG_PRINT("exit", ("No, could not retrieve commit_count"));
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5985 5986
    DBUG_RETURN(FALSE);
  }
5987 5988
  DBUG_PRINT("info", ("*engine_data: %s, commit_count: %s",
                      llstr(*engine_data, buff), llstr(commit_count, buff2)));
5989
  if (commit_count == 0)
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5990
  {
5991 5992
    *engine_data= 0; /* invalidate */
    DBUG_PRINT("exit", ("No, local commit has been performed"));
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
5993 5994
    DBUG_RETURN(FALSE);
  }
5995 5996 5997 5998 5999 6000
  else if (*engine_data != commit_count)
  {
    *engine_data= commit_count; /* invalidate */
     DBUG_PRINT("exit", ("No, commit_count has changed"));
     DBUG_RETURN(FALSE);
   }
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6001

6002 6003
  DBUG_PRINT("exit", ("OK to use cache, engine_data: %s",
                      llstr(*engine_data, buff)));
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031
  DBUG_RETURN(TRUE);
}


/**
   Register a table for use in the query cache. Fetch the commit_count
   for the table and return it in engine_data, this will later be used
   to check if the table has changed, before the cached query is reused.

   SYNOPSIS
   ha_ndbcluster::can_query_cache_table
    thd            thread handle
    full_name      concatenation of database name,
                   the null character '\0', and the table
                   name
    full_name_len  length of the full name,
                   i.e. len(dbname) + len(tablename) + 1
    qc_engine_callback  function to be called before using cache on this table
    engine_data    out, commit_count for this table

  RETURN VALUE
    TRUE  Yes, it's ok to cahce this query
    FALSE No, don't cach the query

*/

my_bool
ha_ndbcluster::register_query_cache_table(THD *thd,
6032 6033 6034
                                          char *full_name, uint full_name_len,
                                          qc_engine_callback *engine_callback,
                                          ulonglong *engine_data)
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6035
{
6036
  Uint64 commit_count;
6037
#ifndef DBUG_OFF
6038
  char buff[22];
6039
#endif
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6040
  bool is_autocommit= !(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN));
6041
  DBUG_ENTER("ha_ndbcluster::register_query_cache_table");
6042 6043 6044
  DBUG_PRINT("enter",("dbname: %s, tabname: %s, is_autocommit: %d",
		      m_dbname, m_tabname, is_autocommit));

mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6045
  if (!is_autocommit)
6046 6047
  {
    DBUG_PRINT("exit", ("Can't register table during transaction"))
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6048
    DBUG_RETURN(FALSE);
6049
  }
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6050 6051 6052 6053

  if (ndb_get_commitcount(thd, m_dbname, m_tabname, &commit_count))
  {
    *engine_data= 0;
6054
    DBUG_PRINT("exit", ("Error, could not get commitcount"))
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6055 6056 6057 6058
    DBUG_RETURN(FALSE);
  }
  *engine_data= commit_count;
  *engine_callback= ndbcluster_cache_retrieval_allowed;
6059
  DBUG_PRINT("exit", ("commit_count: %s", llstr(commit_count, buff)));
6060
  DBUG_RETURN(commit_count > 0);
6061
}
6062

mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6063

6064
/*
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6065
  Handling the shared NDB_SHARE structure that is needed to
6066 6067 6068 6069 6070 6071 6072
  provide table locking.
  It's also used for sharing data with other NDB handlers
  in the same MySQL Server. There is currently not much
  data we want to or can share.
 */

static byte* ndbcluster_get_key(NDB_SHARE *share,uint *length,
6073
                                my_bool not_used __attribute__((unused)))
6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101
{
  *length=share->table_name_length;
  return (byte*) share->table_name;
}

static NDB_SHARE* get_share(const char *table_name)
{
  NDB_SHARE *share;
  pthread_mutex_lock(&ndbcluster_mutex);
  uint length=(uint) strlen(table_name);
  if (!(share=(NDB_SHARE*) hash_search(&ndbcluster_open_tables,
                                       (byte*) table_name,
                                       length)))
  {
    if ((share=(NDB_SHARE *) my_malloc(sizeof(*share)+length+1,
                                       MYF(MY_WME | MY_ZEROFILL))))
    {
      share->table_name_length=length;
      share->table_name=(char*) (share+1);
      strmov(share->table_name,table_name);
      if (my_hash_insert(&ndbcluster_open_tables, (byte*) share))
      {
        pthread_mutex_unlock(&ndbcluster_mutex);
        my_free((gptr) share,0);
        return 0;
      }
      thr_lock_init(&share->lock);
      pthread_mutex_init(&share->mutex,MY_MUTEX_INIT_FAST);
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6102
      share->commit_count= 0;
6103 6104 6105 6106 6107 6108
      share->commit_count_lock= 0;
    }
    else
    {
      DBUG_PRINT("error", ("Failed to alloc share"));
      pthread_mutex_unlock(&ndbcluster_mutex);
6109 6110
      sql_print_error("get_share: my_malloc(%u) failed",
                      (unsigned int)(sizeof(*share)+length+1));
6111
      return 0;
6112 6113 6114
    }
  }
  share->use_count++;
6115 6116

  DBUG_PRINT("share",
6117
	     ("table_name: %s  length: %d  use_count: %d  commit_count: %lu",
6118
	      share->table_name, share->table_name_length, share->use_count,
6119
	      (ulong) share->commit_count));
6120 6121 6122 6123 6124 6125 6126 6127 6128 6129
  pthread_mutex_unlock(&ndbcluster_mutex);
  return share;
}


static void free_share(NDB_SHARE *share)
{
  pthread_mutex_lock(&ndbcluster_mutex);
  if (!--share->use_count)
  {
6130
     hash_delete(&ndbcluster_open_tables, (byte*) share);
6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158
    thr_lock_delete(&share->lock);
    pthread_mutex_destroy(&share->mutex);
    my_free((gptr) share, MYF(0));
  }
  pthread_mutex_unlock(&ndbcluster_mutex);
}



/*
  Internal representation of the frm blob
   
*/

struct frm_blob_struct 
{
  struct frm_blob_header 
  {
    uint ver;      // Version of header
    uint orglen;   // Original length of compressed data
    uint complen;  // Compressed length of data, 0=uncompressed
  } head;
  char data[1];  
};



static int packfrm(const void *data, uint len, 
6159
                   const void **pack_data, uint *pack_len)
6160 6161 6162 6163 6164 6165
{
  int error;
  ulong org_len, comp_len;
  uint blob_len;
  frm_blob_struct* blob;
  DBUG_ENTER("packfrm");
6166
  DBUG_PRINT("enter", ("data: 0x%lx  len: %d", (long) data, len));
6167 6168
  
  error= 1;
6169
  org_len= len;
6170
  if (my_compress((byte*)data, &org_len, &comp_len))
6171 6172 6173
  {
    sql_print_error("packfrm: my_compress(org_len: %u)",
                    (unsigned int)org_len);
6174
    goto err;
6175 6176
  }

6177
  DBUG_PRINT("info", ("org_len: %lu  comp_len: %lu", org_len, comp_len));
6178 6179 6180 6181 6182
  DBUG_DUMP("compressed", (char*)data, org_len);
  
  error= 2;
  blob_len= sizeof(frm_blob_struct::frm_blob_header)+org_len;
  if (!(blob= (frm_blob_struct*) my_malloc(blob_len,MYF(MY_WME))))
6183 6184
  {
    sql_print_error("packfrm: my_malloc(%u)", blob_len);
6185
    goto err;
6186
  }
6187 6188 6189 6190 6191 6192 6193 6194
  // Store compressed blob in machine independent format
  int4store((char*)(&blob->head.ver), 1);
  int4store((char*)(&blob->head.orglen), comp_len);
  int4store((char*)(&blob->head.complen), org_len);
  
  // Copy frm data into blob, already in machine independent format
  memcpy(blob->data, data, org_len);  
  
6195 6196 6197
  *pack_data= blob;
  *pack_len= blob_len;
  error= 0;
6198
  
6199
  DBUG_PRINT("exit", ("pack_data: 0x%lx  pack_len: %d", (long) *pack_data,
6200
                      *pack_len));
6201 6202 6203 6204 6205 6206 6207
err:
  DBUG_RETURN(error);
  
}


static int unpackfrm(const void **unpack_data, uint *unpack_len,
6208
                    const void *pack_data)
6209
{
6210
   const frm_blob_struct *blob= (frm_blob_struct*)pack_data;
6211 6212 6213
   byte *data;
   ulong complen, orglen, ver;
   DBUG_ENTER("unpackfrm");
6214
   DBUG_PRINT("enter", ("pack_data: 0x%lx", (long) pack_data));
6215

6216 6217 6218
   complen=     uint4korr((char*)&blob->head.complen);
   orglen=      uint4korr((char*)&blob->head.orglen);
   ver=         uint4korr((char*)&blob->head.ver);
6219
 
6220
   DBUG_PRINT("blob",("ver: %lu  complen: %lu  orglen: %lu",
6221
                     ver,complen,orglen));
6222 6223 6224
   DBUG_DUMP("blob->data", (char*) blob->data, complen);
 
   if (ver != 1)
6225 6226
   {
     sql_print_error("unpackfrm: ver != 1");
6227
     DBUG_RETURN(1);
6228
   }
6229
   if (!(data= my_malloc(max(orglen, complen), MYF(MY_WME))))
6230 6231 6232 6233 6234
   {
     sql_print_error("unpackfrm: my_malloc(%u)",
                     (unsigned int)max(orglen, complen));
     DBUG_RETURN(HA_ERR_OUT_OF_MEM);
   }
6235 6236 6237 6238 6239
   memcpy(data, blob->data, complen);
 
   if (my_uncompress(data, &complen, &orglen))
   {
     my_free((char*)data, MYF(0));
6240 6241
     sql_print_error("unpackfrm: my_uncompress(complen: %u, orglen: %u)",
                     (unsigned int)complen, (unsigned int)orglen);
6242 6243 6244
     DBUG_RETURN(3);
   }

6245 6246
   *unpack_data= data;
   *unpack_len= complen;
6247

6248
   DBUG_PRINT("exit", ("frmdata: 0x%lx  len: %d", (long) *unpack_data,
6249
                       *unpack_len));
6250 6251 6252

   DBUG_RETURN(0);
}
6253 6254 6255

static 
int
6256
ndb_get_table_statistics(ha_ndbcluster* file, bool report_error, Ndb* ndb,
stewart@willster.(none)'s avatar
stewart@willster.(none) committed
6257
                         const char* table,
6258
                         struct Ndb_statistics * ndbstat)
6259
{
6260
  NdbTransaction* pTrans;
6261
  NdbError error;
6262
  int retries= 10;
6263
  int reterr= 0;
6264
  int retry_sleep= 30; /* 30 milliseconds, transaction */
6265
#ifndef DBUG_OFF
6266
  char buff[22], buff2[22], buff3[22], buff4[22];
6267
#endif
6268 6269
  DBUG_ENTER("ndb_get_table_statistics");
  DBUG_PRINT("enter", ("table: %s", table));
6270 6271

  do
6272
  {
6273 6274
    Uint64 rows, commits, mem;
    Uint32 size;
6275
    Uint32 count= 0;
6276 6277
    Uint64 sum_rows= 0;
    Uint64 sum_commits= 0;
6278 6279
    Uint64 sum_row_size= 0;
    Uint64 sum_mem= 0;
6280 6281 6282 6283
    NdbScanOperation*pOp;
    int check;

    if ((pTrans= ndb->startTransaction()) == NULL)
6284
    {
6285 6286 6287
      error= ndb->getNdbError();
      goto retry;
    }
6288
      
6289 6290 6291 6292
    if ((pOp= pTrans->getNdbScanOperation(table)) == NULL)
    {
      error= pTrans->getNdbError();
      goto retry;
6293
    }
6294
    
6295
    if (pOp->readTuples(NdbOperation::LM_CommittedRead))
6296 6297 6298 6299
    {
      error= pOp->getNdbError();
      goto retry;
    }
6300
    
6301 6302 6303 6304 6305
    if (pOp->interpret_exit_last_row() == -1)
    {
      error= pOp->getNdbError();
      goto retry;
    }
6306 6307 6308
    
    pOp->getValue(NdbDictionary::Column::ROW_COUNT, (char*)&rows);
    pOp->getValue(NdbDictionary::Column::COMMIT_COUNT, (char*)&commits);
6309 6310
    pOp->getValue(NdbDictionary::Column::ROW_SIZE, (char*)&size);
    pOp->getValue(NdbDictionary::Column::FRAGMENT_MEMORY, (char*)&mem);
6311
    
6312 6313 6314
    if (pTrans->execute(NdbTransaction::NoCommit,
                        NdbTransaction::AbortOnError,
                        TRUE) == -1)
6315
    {
6316 6317
      error= pTrans->getNdbError();
      goto retry;
6318
    }
6319
    
monty@mishka.local's avatar
monty@mishka.local committed
6320
    while ((check= pOp->nextResult(TRUE, TRUE)) == 0)
6321 6322 6323
    {
      sum_rows+= rows;
      sum_commits+= commits;
6324
      if (sum_row_size < size)
6325
        sum_row_size= size;
6326
      sum_mem+= mem;
6327
      count++;
6328 6329 6330
    }
    
    if (check == -1)
6331 6332 6333 6334
    {
      error= pOp->getNdbError();
      goto retry;
    }
6335

6336
    pOp->close(TRUE);
6337

6338
    ndb->closeTransaction(pTrans);
6339 6340 6341 6342 6343 6344

    ndbstat->row_count= sum_rows;
    ndbstat->commit_count= sum_commits;
    ndbstat->row_size= sum_row_size;
    ndbstat->fragment_memory= sum_mem;

6345 6346 6347 6348 6349 6350 6351
    DBUG_PRINT("exit", ("records: %s  commits: %s "
                        "row_size: %s  mem: %s count: %u",
			llstr(sum_rows, buff),
                        llstr(sum_commits, buff2),
                        llstr(sum_row_size, buff3),
                        llstr(sum_mem, buff4),
                        count));
6352

6353
    DBUG_RETURN(0);
6354
retry:
6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369
    if(report_error)
    {
      if (file && pTrans)
      {
        reterr= file->ndb_err(pTrans);
      }
      else
      {
        const NdbError& tmp= error;
        ERR_PRINT(tmp);
        reterr= ndb_to_mysql_error(&tmp);
      }
    }
    else
      reterr= error.code;
6370

6371
    if (pTrans)
6372 6373 6374 6375 6376 6377 6378 6379 6380 6381
    {
      ndb->closeTransaction(pTrans);
      pTrans= NULL;
    }
    if (error.status == NdbError::TemporaryError && retries--)
    {
      my_sleep(retry_sleep);
      continue;
    }
    break;
6382
  } while(1);
6383 6384 6385
  DBUG_PRINT("exit", ("failed, reterr: %u, NdbError %u(%s)", reterr,
                      error.code, error.message));
  DBUG_RETURN(reterr);
6386 6387
}

6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402
/*
  Create a .ndb file to serve as a placeholder indicating 
  that the table with this name is a ndb table
*/

int ha_ndbcluster::write_ndb_file()
{
  File file;
  bool error=1;
  char path[FN_REFLEN];
  
  DBUG_ENTER("write_ndb_file");
  DBUG_PRINT("enter", ("db: %s, name: %s", m_dbname, m_tabname));

  (void)strxnmov(path, FN_REFLEN, 
6403
                 mysql_data_home,"/",m_dbname,"/",m_tabname,ha_ndb_ext,NullS);
6404 6405 6406 6407 6408 6409 6410 6411 6412 6413

  if ((file=my_create(path, CREATE_MODE,O_RDWR | O_TRUNC,MYF(MY_WME))) >= 0)
  {
    // It's an empty file
    error=0;
    my_close(file,MYF(0));
  }
  DBUG_RETURN(error);
}

6414
void 
6415 6416
ha_ndbcluster::release_completed_operations(NdbTransaction *trans,
					    bool force_release)
6417 6418 6419 6420 6421 6422 6423 6424
{
  if (trans->hasBlobOperation())
  {
    /* We are reading/writing BLOB fields, 
       releasing operation records is unsafe
    */
    return;
  }
6425 6426 6427 6428 6429 6430 6431 6432 6433 6434
  if (!force_release)
  {
    if (get_thd_ndb(current_thd)->query_state & NDB_QUERY_MULTI_READ_RANGE)
    {
      /* We are batching reads and have not consumed all fetched
	 rows yet, releasing operation records is unsafe 
      */
      return;
    }
  }
6435
  trans->releaseCompletedOperations();
6436 6437
}

6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461
bool 
ha_ndbcluster::null_value_index_search(KEY_MULTI_RANGE *ranges,
				       KEY_MULTI_RANGE *end_range,
				       HANDLER_BUFFER *buffer)
{
  DBUG_ENTER("null_value_index_search");
  KEY* key_info= table->key_info + active_index;
  KEY_MULTI_RANGE *range= ranges;
  ulong reclength= table->s->reclength;
  byte *curr= (byte*)buffer->buffer;
  byte *end_of_buffer= (byte*)buffer->buffer_end;
  
  for (; range<end_range && curr+reclength <= end_of_buffer; 
       range++)
  {
    const byte *key= range->start_key.key;
    uint key_len= range->start_key.length;
    if (check_null_in_key(key_info, key, key_len))
      DBUG_RETURN(true);
    curr += reclength;
  }
  DBUG_RETURN(false);
}

6462
int
6463
ha_ndbcluster::read_multi_range_first(KEY_MULTI_RANGE **found_range_p,
6464 6465 6466 6467
                                      KEY_MULTI_RANGE *ranges, 
                                      uint range_count,
                                      bool sorted, 
                                      HANDLER_BUFFER *buffer)
6468 6469 6470
{
  int res;
  KEY* key_info= table->key_info + active_index;
6471
  NDB_INDEX_TYPE cur_index_type= get_index_type(active_index);
joreland@mysql.com's avatar
merge  
joreland@mysql.com committed
6472
  ulong reclength= table->s->reclength;
6473
  NdbOperation* op;
6474
  Thd_ndb *thd_ndb= get_thd_ndb(current_thd);
6475
  DBUG_ENTER("ha_ndbcluster::read_multi_range_first");
6476

6477 6478 6479 6480
  /**
   * blobs and unique hash index with NULL can't be batched currently
   */
  if (uses_blob_value(m_retrieve_all_fields) ||
6481
      (cur_index_type == UNIQUE_INDEX &&
6482 6483
       has_null_in_unique_index(active_index) &&
       null_value_index_search(ranges, ranges+range_count, buffer)))
6484
  {
6485
    m_disable_multi_read= TRUE;
6486
    DBUG_RETURN(handler::read_multi_range_first(found_range_p, 
6487 6488 6489 6490
                                                ranges, 
                                                range_count,
                                                sorted, 
                                                buffer));
6491
  }
6492
  thd_ndb->query_state|= NDB_QUERY_MULTI_READ_RANGE;
6493
  m_disable_multi_read= FALSE;
6494 6495 6496 6497

  /**
   * Copy arguments into member variables
   */
6498 6499 6500
  m_multi_ranges= ranges;
  multi_range_curr= ranges;
  multi_range_end= ranges+range_count;
6501 6502 6503
  multi_range_sorted= sorted;
  multi_range_buffer= buffer;

6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514
  /**
   * read multi range will read ranges as follows (if not ordered)
   *
   * input    read order
   * ======   ==========
   * pk-op 1  pk-op 1
   * pk-op 2  pk-op 2
   * range 3  range (3,5) NOTE result rows will be intermixed
   * pk-op 4  pk-op 4
   * range 5
   * pk-op 6  pk-ok 6
6515 6516
   */   

mskold@mysql.com's avatar
mskold@mysql.com committed
6517
  /**
6518 6519
   * Variables for loop
   */
6520 6521
  byte *curr= (byte*)buffer->buffer;
  byte *end_of_buffer= (byte*)buffer->buffer_end;
6522 6523
  NdbOperation::LockMode lm= 
    (NdbOperation::LockMode)get_ndb_lock_type(m_lock.type);
6524
  bool need_pk = (lm == NdbOperation::LM_Read);
6525 6526
  const NDBTAB *tab= (const NDBTAB *) m_table;
  const NDBINDEX *unique_idx= (NDBINDEX *) m_index[active_index].unique_index;
6527
  const NDBINDEX *idx= (NDBINDEX *) m_index[active_index].index; 
6528 6529
  const NdbOperation* lastOp= m_active_trans->getLastDefinedOperation();
  NdbIndexScanOperation* scanOp= 0;
6530 6531
  for (; multi_range_curr<multi_range_end && curr+reclength <= end_of_buffer; 
       multi_range_curr++)
6532
  {
6533
    switch (cur_index_type) {
6534 6535 6536 6537 6538
    case PRIMARY_KEY_ORDERED_INDEX:
      if (!(multi_range_curr->start_key.length == key_info->key_length &&
            multi_range_curr->start_key.flag == HA_READ_KEY_EXACT))
      goto range;
      /* fall through */
6539
    case PRIMARY_KEY_INDEX:
6540
      multi_range_curr->range_flag |= UNIQUE_RANGE;
6541
      if ((op= m_active_trans->getNdbOperation(tab)) && 
6542 6543 6544
          !op->readTuple(lm) && 
          !set_primary_key(op, multi_range_curr->start_key.key) &&
          !define_read_attrs(curr, op) &&
6545
          (op->setAbortOption(AO_IgnoreError), TRUE))
6546
        curr += reclength;
6547
      else
6548
        ERR_RETURN(op ? op->getNdbError() : m_active_trans->getNdbError());
6549
      break;
6550 6551 6552 6553 6554 6555 6556
    case UNIQUE_ORDERED_INDEX:
      if (!(multi_range_curr->start_key.length == key_info->key_length &&
            multi_range_curr->start_key.flag == HA_READ_KEY_EXACT &&
            !check_null_in_key(key_info, multi_range_curr->start_key.key,
                               multi_range_curr->start_key.length)))
      goto range;
      /* fall through */
6557
    case UNIQUE_INDEX:
6558
      multi_range_curr->range_flag |= UNIQUE_RANGE;
6559
      if ((op= m_active_trans->getNdbIndexOperation(unique_idx, tab)) && 
6560 6561 6562 6563 6564
	  !op->readTuple(lm) && 
	  !set_index_key(op, key_info, multi_range_curr->start_key.key) &&
	  !define_read_attrs(curr, op) &&
	  (op->setAbortOption(AO_IgnoreError), TRUE))
	curr += reclength;
6565
      else
6566
	ERR_RETURN(op ? op->getNdbError() : m_active_trans->getNdbError());
6567
      break;
6568 6569
    case ORDERED_INDEX:
    {
6570
  range:
6571
      multi_range_curr->range_flag &= ~(uint)UNIQUE_RANGE;
6572 6573
      if (scanOp == 0)
      {
6574 6575 6576 6577 6578 6579
        if (m_multi_cursor)
        {
          scanOp= m_multi_cursor;
          DBUG_ASSERT(scanOp->getSorted() == sorted);
          DBUG_ASSERT(scanOp->getLockMode() == 
                      (NdbOperation::LockMode)get_ndb_lock_type(m_lock.type));
6580
          if (scanOp->reset_bounds(m_force_send))
6581 6582 6583 6584 6585
            DBUG_RETURN(ndb_err(m_active_trans));
          
          end_of_buffer -= reclength;
        }
        else if ((scanOp= m_active_trans->getNdbIndexScanOperation(idx, tab)) 
6586
                 &&!scanOp->readTuples(lm, 0, parallelism, sorted, 
6587
				       FALSE, TRUE, need_pk, TRUE)
6588
                 &&!(m_cond && m_cond->generate_scan_filter(scanOp))
6589 6590 6591 6592 6593 6594 6595 6596 6597 6598
                 &&!define_read_attrs(end_of_buffer-reclength, scanOp))
        {
          m_multi_cursor= scanOp;
          m_multi_range_cursor_result_ptr= end_of_buffer-reclength;
        }
        else
        {
          ERR_RETURN(scanOp ? scanOp->getNdbError() : 
                     m_active_trans->getNdbError());
        }
6599
      }
6600

6601
      const key_range *keys[2]= { &multi_range_curr->start_key, 
6602
                                  &multi_range_curr->end_key };
6603
      if ((res= set_bounds(scanOp, keys, multi_range_curr-ranges)))
6604
        DBUG_RETURN(res);
6605
      break;
6606
    }
6607
    case UNDEFINED_INDEX:
mskold@mysql.com's avatar
mskold@mysql.com committed
6608 6609 6610 6611
      DBUG_ASSERT(FALSE);
      DBUG_RETURN(1);
      break;
    }
6612 6613
  }
  
6614
  if (multi_range_curr != multi_range_end)
6615
  {
6616 6617 6618 6619 6620 6621
    /**
     * Mark that we're using entire buffer (even if might not) as
     *   we haven't read all ranges for some reason
     * This as we don't want mysqld to reuse the buffer when we read
     *   the remaining ranges
     */
6622
    buffer->end_of_used_area= (byte*)buffer->buffer_end;
6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633
  }
  else
  {
    buffer->end_of_used_area= curr;
  }
  
  /**
   * Set first operation in multi range
   */
  m_current_multi_operation= 
    lastOp ? lastOp->next() : m_active_trans->getFirstDefinedOperation();
6634
  if (!(res= execute_no_commit_ie(this, m_active_trans, true)))
6635
  {
6636 6637
    m_multi_range_defined= multi_range_curr;
    multi_range_curr= ranges;
6638 6639
    m_multi_range_result_ptr= (byte*)buffer->buffer;
    DBUG_RETURN(read_multi_range_next(found_range_p));
6640 6641 6642 6643
  }
  ERR_RETURN(m_active_trans->getNdbError());
}

6644 6645 6646 6647 6648 6649
#if 0
#define DBUG_MULTI_RANGE(x) printf("read_multi_range_next: case %d\n", x);
#else
#define DBUG_MULTI_RANGE(x)
#endif

6650
int
6651
ha_ndbcluster::read_multi_range_next(KEY_MULTI_RANGE ** multi_range_found_p)
6652 6653
{
  DBUG_ENTER("ha_ndbcluster::read_multi_range_next");
6654
  if (m_disable_multi_read)
6655
  {
6656
    DBUG_RETURN(handler::read_multi_range_next(multi_range_found_p));
6657
  }
6658
  
6659
  int res;
6660
  int range_no;
joreland@mysql.com's avatar
merge  
joreland@mysql.com committed
6661
  ulong reclength= table->s->reclength;
6662
  const NdbOperation* op= m_current_multi_operation;
6663
  for (;multi_range_curr < m_multi_range_defined; multi_range_curr++)
6664
  {
6665
    if (multi_range_curr->range_flag & UNIQUE_RANGE)
6666
    {
6667
      if (op->getNdbError().code == 0)
6668
        goto found_next;
6669 6670 6671
      
      op= m_active_trans->getNextCompletedOperation(op);
      m_multi_range_result_ptr += reclength;
6672
      continue;
6673
    } 
6674
    else if (m_multi_cursor && !multi_range_sorted)
6675
    {
6676 6677
      DBUG_MULTI_RANGE(1);
      if ((res= fetch_next(m_multi_cursor)) == 0)
6678
      {
6679 6680 6681
        DBUG_MULTI_RANGE(2);
        range_no= m_multi_cursor->get_range_no();
        goto found;
6682 6683 6684
      } 
      else
      {
6685
        goto close_scan;
6686 6687
      }
    }
6688
    else if (m_multi_cursor && multi_range_sorted)
6689
    {
6690 6691
      if (m_active_cursor && (res= fetch_next(m_multi_cursor)))
      {
6692 6693
        DBUG_MULTI_RANGE(3);
        goto close_scan;
6694
      }
6695
      
6696
      range_no= m_multi_cursor->get_range_no();
6697
      uint current_range_no= multi_range_curr - m_multi_ranges;
mskold@mysql.com's avatar
mskold@mysql.com committed
6698
      if ((uint) range_no == current_range_no)
6699
      {
6700
        DBUG_MULTI_RANGE(4);
6701
        // return current row
6702
        goto found;
6703
      }
6704
      else if (range_no > (int)current_range_no)
6705
      {
6706 6707 6708 6709
        DBUG_MULTI_RANGE(5);
        // wait with current row
        m_active_cursor= 0;
        continue;
6710 6711 6712
      }
      else 
      {
6713 6714 6715
        DBUG_MULTI_RANGE(6);
        // First fetch from cursor
        DBUG_ASSERT(range_no == -1);
6716
        if ((res= m_multi_cursor->nextResult(true)))
6717 6718 6719 6720 6721
        {
          goto close_scan;
        }
        multi_range_curr--; // Will be increased in for-loop
        continue;
6722
      }
6723
    }
6724
    else /** m_multi_cursor == 0 */
6725
    {
6726
      DBUG_MULTI_RANGE(7);
6727 6728 6729 6730
      /**
       * Corresponds to range 5 in example in read_multi_range_first
       */
      (void)1;
6731
      continue;
6732
    }
6733
    
6734
    DBUG_ASSERT(FALSE); // Should only get here via goto's
6735 6736 6737
close_scan:
    if (res == 1)
    {
6738
      m_multi_cursor->close(FALSE, TRUE);
6739
      m_active_cursor= m_multi_cursor= 0;
6740
      DBUG_MULTI_RANGE(8);
6741 6742 6743 6744 6745 6746 6747
      continue;
    } 
    else 
    {
      DBUG_RETURN(ndb_err(m_active_trans));
    }
  }
6748
  
6749
  if (multi_range_curr == multi_range_end)
6750 6751 6752
  {
    Thd_ndb *thd_ndb= get_thd_ndb(current_thd);
    thd_ndb->query_state&= NDB_QUERY_NORMAL;
6753
    DBUG_RETURN(HA_ERR_END_OF_FILE);
6754
  }
6755
  
6756 6757 6758 6759
  /**
   * Read remaining ranges
   */
  DBUG_RETURN(read_multi_range_first(multi_range_found_p, 
6760 6761 6762 6763
                                     multi_range_curr,
                                     multi_range_end - multi_range_curr, 
                                     multi_range_sorted,
                                     multi_range_buffer));
6764 6765
  
found:
6766 6767 6768
  /**
   * Found a record belonging to a scan
   */
6769
  m_active_cursor= m_multi_cursor;
6770
  * multi_range_found_p= m_multi_ranges + range_no;
6771 6772
  memcpy(table->record[0], m_multi_range_cursor_result_ptr, reclength);
  setup_recattr(m_active_cursor->getFirstRecAttr());
6773 6774 6775
  unpack_record(table->record[0]);
  table->status= 0;     
  DBUG_RETURN(0);
6776
  
6777
found_next:
6778 6779 6780 6781
  /**
   * Found a record belonging to a pk/index op,
   *   copy result and move to next to prepare for next call
   */
6782
  * multi_range_found_p= multi_range_curr;
6783
  memcpy(table->record[0], m_multi_range_result_ptr, reclength);
6784
  setup_recattr(op->getFirstRecAttr());
6785
  unpack_record(table->record[0]);
6786 6787
  table->status= 0;
  
6788
  multi_range_curr++;
6789
  m_current_multi_operation= m_active_trans->getNextCompletedOperation(op);
6790 6791
  m_multi_range_result_ptr += reclength;
  DBUG_RETURN(0);
6792 6793
}

6794 6795 6796 6797 6798 6799 6800 6801
int
ha_ndbcluster::setup_recattr(const NdbRecAttr* curr)
{
  DBUG_ENTER("setup_recattr");

  Field **field, **end;
  NdbValue *value= m_value;
  
joreland@mysql.com's avatar
merge  
joreland@mysql.com committed
6802
  end= table->field + table->s->fields;
6803 6804 6805 6806 6807 6808
  
  for (field= table->field; field < end; field++, value++)
  {
    if ((* value).ptr)
    {
      DBUG_ASSERT(curr != 0);
6809 6810 6811
      NdbValue* val= m_value + curr->getColumn()->getColumnNo();
      DBUG_ASSERT(val->ptr);
      val->rec= curr;
6812
      curr= curr->next();
6813 6814 6815
    }
  }
  
6816
  DBUG_RETURN(0);
6817 6818
}

mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6819 6820
char*
ha_ndbcluster::update_table_comment(
6821 6822
                                /* out: table comment + additional */
        const char*     comment)/* in:  table comment defined by user */
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6823 6824
{
  uint length= strlen(comment);
6825
  if (length > 64000 - 3)
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6826 6827 6828 6829 6830 6831 6832 6833 6834 6835
  {
    return((char*)comment); /* string too long */
  }

  Ndb* ndb;
  if (!(ndb= get_ndb()))
  {
    return((char*)comment);
  }

6836 6837 6838 6839
  if (ndb->setDatabaseName(m_dbname))
  {
    return((char*)comment);
  }
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851
  NDBDICT* dict= ndb->getDictionary();
  const NDBTAB* tab;
  if (!(tab= dict->getTable(m_tabname)))
  {
    return((char*)comment);
  }

  char *str;
  const char *fmt="%s%snumber_of_replicas: %d";
  const unsigned fmt_len_plus_extra= length + strlen(fmt);
  if ((str= my_malloc(fmt_len_plus_extra, MYF(0))) == NULL)
  {
6852 6853
    sql_print_error("ha_ndbcluster::update_table_comment: "
                    "my_malloc(%u) failed", (unsigned int)fmt_len_plus_extra);
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6854 6855 6856
    return (char*)comment;
  }

6857 6858 6859
  my_snprintf(str,fmt_len_plus_extra,fmt,comment,
              length > 0 ? " ":"",
              tab->getReplicaCount());
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6860 6861 6862 6863 6864
  return str;
}


// Utility thread main loop
6865
pthread_handler_t ndb_util_thread_func(void *arg __attribute__((unused)))
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6866 6867
{
  THD *thd; /* needs to be first for thread_stack */
6868
  Ndb* ndb;
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6869 6870 6871 6872
  struct timespec abstime;

  my_thread_init();
  DBUG_ENTER("ndb_util_thread");
6873
  DBUG_PRINT("enter", ("ndb_cache_check_time: %lu", ndb_cache_check_time));
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6874 6875

  thd= new THD; /* note that contructor of THD uses DBUG_ */
6876 6877 6878 6879 6880
  if (thd == NULL)
  {
    my_errno= HA_ERR_OUT_OF_MEM;
    DBUG_RETURN(NULL);
  }
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6881
  THD_CHECK_SENTRY(thd);
6882
  ndb= new Ndb(g_ndb_cluster_connection, "");
6883 6884 6885 6886 6887 6888
  if (ndb == NULL)
  {
    thd->cleanup();
    delete thd;
    DBUG_RETURN(NULL);
  }
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6889 6890 6891 6892
  pthread_detach_this_thread();
  ndb_util_thread= pthread_self();

  thd->thread_stack= (char*)&thd; /* remember where our stack is */
6893
  if (thd->store_globals() || (ndb->init() != 0))
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6894 6895 6896
  {
    thd->cleanup();
    delete thd;
6897
    delete ndb;
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6898 6899 6900
    DBUG_RETURN(NULL);
  }

6901 6902
  uint share_list_size= 0;
  NDB_SHARE **share_list= NULL;
6903
  set_timespec(abstime, 0);
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6904 6905 6906
  for (;;)
  {

6907 6908 6909
    if (abort_loop)
      break; /* Shutting down server */

mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6910
    pthread_mutex_lock(&LOCK_ndb_util_thread);
monty@mysql.com's avatar
monty@mysql.com committed
6911 6912 6913
    pthread_cond_timedwait(&COND_ndb_util_thread,
                           &LOCK_ndb_util_thread,
                           &abstime);
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6914 6915
    pthread_mutex_unlock(&LOCK_ndb_util_thread);

6916
    DBUG_PRINT("ndb_util_thread", ("Started, ndb_cache_check_time: %lu",
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6917 6918 6919 6920 6921 6922 6923
                                   ndb_cache_check_time));

    if (abort_loop)
      break; /* Shutting down server */

    if (ndb_cache_check_time == 0)
    {
6924 6925
      /* Wake up in 1 second to check if value has changed */
      set_timespec(abstime, 1);
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6926 6927 6928 6929 6930 6931
      continue;
    }

    /* Lock mutex and fill list with pointers to all open tables */
    NDB_SHARE *share;
    pthread_mutex_lock(&ndbcluster_mutex);
6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947
    uint i, record_count= ndbcluster_open_tables.records;
    if (share_list_size < record_count)
    {
      NDB_SHARE ** new_share_list= new NDB_SHARE * [record_count];
      if (!new_share_list)
      {
        sql_print_warning("ndb util thread: malloc failure, "
                          "query cache not maintained properly");
        pthread_mutex_unlock(&ndbcluster_mutex);
        goto next;                               // At least do not crash
      }
      delete [] share_list;
      share_list_size= record_count;
      share_list= new_share_list;
    }
    for (i= 0; i < record_count; i++)
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6948 6949 6950 6951 6952 6953 6954 6955
    {
      share= (NDB_SHARE *)hash_element(&ndbcluster_open_tables, i);
      share->use_count++; /* Make sure the table can't be closed */
      DBUG_PRINT("ndb_util_thread",
                 ("Found open table[%d]: %s, use_count: %d",
                  i, share->table_name, share->use_count));

      /* Store pointer to table */
6956
      share_list[i]= share;
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6957 6958 6959
    }
    pthread_mutex_unlock(&ndbcluster_mutex);

6960 6961
    /* Iterate through the open files list */
    for (i= 0; i < record_count; i++)
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6962
    {
6963
      share= share_list[i];
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6964 6965 6966 6967 6968 6969 6970 6971 6972
      /* Split tab- and dbname */
      char buf[FN_REFLEN];
      char *tabname, *db;
      uint length= dirname_length(share->table_name);
      tabname= share->table_name+length;
      memcpy(buf, share->table_name, length-1);
      buf[length-1]= 0;
      db= buf+dirname_length(buf);
      DBUG_PRINT("ndb_util_thread",
6973 6974
                 ("Fetching commit count for: %s",
                  share->table_name));
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6975 6976

      /* Contact NDB to get commit count for table */
6977 6978 6979 6980 6981
      struct Ndb_statistics stat;
      uint lock;
      pthread_mutex_lock(&share->mutex);
      lock= share->commit_count_lock;
      pthread_mutex_unlock(&share->mutex);
6982 6983 6984 6985
      if (ndb->setDatabaseName(db))
      {
        goto loop_next;
      }
stewart@willster.(none)'s avatar
stewart@willster.(none) committed
6986
      if (ndb_get_table_statistics(NULL, false, ndb, tabname, &stat) == 0)
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6987
      {
6988
#ifndef DBUG_OFF
6989
        char buff[22], buff2[22];
6990
#endif
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6991
        DBUG_PRINT("ndb_util_thread",
6992 6993 6994
                   ("Table: %s  commit_count: %s  rows: %s",
                    share->table_name,
                    llstr(stat.commit_count, buff),
monty@mysql.com's avatar
monty@mysql.com committed
6995
                    llstr(stat.row_count, buff2)));
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
6996 6997 6998 6999 7000 7001
      }
      else
      {
        DBUG_PRINT("ndb_util_thread",
                   ("Error: Could not get commit count for table %s",
                    share->table_name));
7002
        stat.commit_count= 0;
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
7003
      }
7004
  loop_next:
7005 7006 7007 7008 7009
      pthread_mutex_lock(&share->mutex);
      if (share->commit_count_lock == lock)
        share->commit_count= stat.commit_count;
      pthread_mutex_unlock(&share->mutex);

mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
7010 7011 7012
      /* Decrease the use count and possibly free share */
      free_share(share);
    }
7013
next:
7014 7015 7016 7017 7018 7019 7020 7021 7022
    /* Calculate new time to wake up */
    int secs= 0;
    int msecs= ndb_cache_check_time;

    struct timeval tick_time;
    gettimeofday(&tick_time, 0);
    abstime.tv_sec=  tick_time.tv_sec;
    abstime.tv_nsec= tick_time.tv_usec * 1000;

7023
    if (msecs >= 1000){
7024 7025 7026 7027 7028 7029 7030 7031 7032 7033
      secs=  msecs / 1000;
      msecs= msecs % 1000;
    }

    abstime.tv_sec+=  secs;
    abstime.tv_nsec+= msecs * 1000000;
    if (abstime.tv_nsec >= 1000000000) {
      abstime.tv_sec+=  1;
      abstime.tv_nsec-= 1000000000;
    }
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
7034 7035
  }

7036 7037
  if (share_list)
    delete [] share_list;
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
7038 7039
  thd->cleanup();
  delete thd;
7040
  delete ndb;
mskold@mysql.com's avatar
Merge  
mskold@mysql.com committed
7041 7042 7043 7044 7045 7046
  DBUG_PRINT("exit", ("ndb_util_thread"));
  my_thread_end();
  pthread_exit(0);
  DBUG_RETURN(NULL);
}

7047 7048 7049 7050 7051 7052 7053 7054 7055
int
ndbcluster_show_status(THD* thd)
{
  Protocol *protocol= thd->protocol;
  DBUG_ENTER("ndbcluster_show_status");
  
  if (have_ndbcluster != SHOW_OPTION_YES) 
  {
    my_message(ER_NOT_SUPPORTED_YET,
7056 7057
	       "Cannot call SHOW NDBCLUSTER STATUS because skip-ndbcluster is "
               "defined",
7058 7059 7060 7061 7062 7063 7064 7065 7066 7067
	       MYF(0));
    DBUG_RETURN(TRUE);
  }
  
  List<Item> field_list;
  field_list.push_back(new Item_empty_string("free_list", 255));
  field_list.push_back(new Item_return_int("created", 10,MYSQL_TYPE_LONG));
  field_list.push_back(new Item_return_int("free", 10,MYSQL_TYPE_LONG));
  field_list.push_back(new Item_return_int("sizeof", 10,MYSQL_TYPE_LONG));

7068 7069
  if (protocol->send_fields(&field_list,
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
7070 7071
    DBUG_RETURN(TRUE);
  
7072
  if (get_thd_ndb(thd) && get_thd_ndb(thd)->ndb)
7073
  {
7074
    Ndb* ndb= (get_thd_ndb(thd))->ndb;
7075 7076
    Ndb::Free_list_usage tmp;
    tmp.m_name= 0;
7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093
    while (ndb->get_free_list_usage(&tmp))
    {
      protocol->prepare_for_resend();
      
      protocol->store(tmp.m_name, &my_charset_bin);
      protocol->store((uint)tmp.m_created);
      protocol->store((uint)tmp.m_free);
      protocol->store((uint)tmp.m_sizeof);
      if (protocol->write())
	DBUG_RETURN(TRUE);
    }
  }
  send_eof(thd);
  
  DBUG_RETURN(FALSE);
}

7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139
/*
  Condition pushdown
*/
/*
  Push a condition to ndbcluster storage engine for evaluation 
  during table   and index scans. The conditions will be stored on a stack
  for possibly storing several conditions. The stack can be popped
  by calling cond_pop, handler::extra(HA_EXTRA_RESET) (handler::reset())
  will clear the stack.
  The current implementation supports arbitrary AND/OR nested conditions
  with comparisons between columns and constants (including constant
  expressions and function calls) and the following comparison operators:
  =, !=, >, >=, <, <=, "is null", and "is not null".
  
  RETURN
    NULL The condition was supported and will be evaluated for each 
    row found during the scan
    cond The condition was not supported and all rows will be returned from
         the scan for evaluation (and thus not saved on stack)
*/
const 
COND* 
ha_ndbcluster::cond_push(const COND *cond) 
{ 
  DBUG_ENTER("cond_push");
  if (!m_cond) 
    m_cond= new ha_ndbcluster_cond;
  if (!m_cond)
  {
    my_errno= HA_ERR_OUT_OF_MEM;
    DBUG_RETURN(NULL);
  }
  DBUG_EXECUTE("where",print_where((COND *)cond, m_tabname););
  DBUG_RETURN(m_cond->cond_push(cond, table, (NDBTAB *)m_table));
}

/*
  Pop the top condition from the condition stack of the handler instance.
*/
void 
ha_ndbcluster::cond_pop() 
{ 
  if (m_cond)
    m_cond->cond_pop();
}

7140
#endif /* HAVE_NDBCLUSTER_DB */