NdbDictionaryImpl.cpp 136 KB
Newer Older
1 2 3 4
/* Copyright (C) 2003 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

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

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

#include "NdbDictionaryImpl.hpp"
#include "API.hpp"
#include <NdbOut.hpp>
#include "NdbApiSignal.hpp"
#include "TransporterFacade.hpp"
#include <signaldata/GetTabInfo.hpp>
#include <signaldata/DictTabInfo.hpp>
#include <signaldata/CreateTable.hpp>
#include <signaldata/CreateIndx.hpp>
#include <signaldata/CreateEvnt.hpp>
#include <signaldata/SumaImpl.hpp>
#include <signaldata/DropTable.hpp>
#include <signaldata/AlterTable.hpp>
#include <signaldata/DropIndx.hpp>
#include <signaldata/ListTables.hpp>
31 32
#include <signaldata/DropFilegroup.hpp>
#include <signaldata/CreateFilegroup.hpp>
33
#include <signaldata/WaitGCP.hpp>
34 35 36 37 38
#include <SimpleProperties.hpp>
#include <Bitmask.hpp>
#include <AttributeList.hpp>
#include <NdbEventOperation.hpp>
#include "NdbEventOperationImpl.hpp"
39 40
#include <NdbBlob.hpp>
#include "NdbBlobImpl.hpp"
joreland@mysql.com's avatar
joreland@mysql.com committed
41
#include <AttributeHeader.hpp>
pekka@mysql.com's avatar
pekka@mysql.com committed
42
#include <my_sys.h>
43
#include <NdbEnv.h>
44
#include <NdbMem.h>
45
#include <ndb_version.h>
46 47 48 49

#define DEBUG_PRINT 0
#define INCOMPATIBLE_VERSION -2

50 51
#define DICT_WAITFOR_TIMEOUT (7*24*60*60*1000)

52 53 54 55 56 57 58
#define ERR_RETURN(a,b) \
{\
   DBUG_PRINT("exit", ("error %d", (a).code));\
   DBUG_RETURN(b);\
}

int ndb_dictionary_is_mysqld = 0;
59

60
bool
61
is_ndb_blob_table(const char* name, Uint32* ptab_id, Uint32* pcol_no)
62
{
63
  return DictTabInfo::isBlobTableName(name, ptab_id, pcol_no);
64 65 66 67 68 69 70 71
}

bool
is_ndb_blob_table(const NdbTableImpl* t)
{
  return is_ndb_blob_table(t->m_internalName.c_str());
}

72 73 74 75 76 77
//#define EVENT_DEBUG

/**
 * Column
 */
NdbColumnImpl::NdbColumnImpl()
78
  : NdbDictionary::Column(* this), m_attrId(-1), m_facade(this)
79
{
80
  DBUG_ENTER("NdbColumnImpl::NdbColumnImpl");
81
  DBUG_PRINT("info", ("this: %p", this));
82
  init();
83
  DBUG_VOID_RETURN;
84 85 86
}

NdbColumnImpl::NdbColumnImpl(NdbDictionary::Column & f)
87
  : NdbDictionary::Column(* this), m_attrId(-1), m_facade(&f)
88
{
89
  DBUG_ENTER("NdbColumnImpl::NdbColumnImpl");
90
  DBUG_PRINT("info", ("this: %p", this));
91
  init();
92
  DBUG_VOID_RETURN;
93 94
}

mysqldev@mysql.com's avatar
mysqldev@mysql.com committed
95 96
NdbColumnImpl&
NdbColumnImpl::operator=(const NdbColumnImpl& col)
97
{
98
  DBUG_ENTER("NdbColumnImpl::operator=");
99
  DBUG_PRINT("info", ("this: %p  &col: %p", this, &col));
100 101 102 103
  m_attrId = col.m_attrId;
  m_name = col.m_name;
  m_type = col.m_type;
  m_precision = col.m_precision;
pekka@mysql.com's avatar
pekka@mysql.com committed
104
  m_cs = col.m_cs;
105 106 107 108 109 110 111 112 113 114
  m_scale = col.m_scale;
  m_length = col.m_length;
  m_pk = col.m_pk;
  m_distributionKey = col.m_distributionKey;
  m_nullable = col.m_nullable;
  m_autoIncrement = col.m_autoIncrement;
  m_autoIncrementInitialValue = col.m_autoIncrementInitialValue;
  m_defaultValue = col.m_defaultValue;
  m_attrSize = col.m_attrSize; 
  m_arraySize = col.m_arraySize;
115 116
  m_arrayType = col.m_arrayType;
  m_storageType = col.m_storageType;
117
  m_keyInfoPos = col.m_keyInfoPos;
118 119 120
  if (col.m_blobTable == NULL)
    m_blobTable = NULL;
  else {
121 122
    if (m_blobTable == NULL)
      m_blobTable = new NdbTableImpl();
123 124
    m_blobTable->assign(*col.m_blobTable);
  }
125
  m_column_no = col.m_column_no;
126 127
  // Do not copy m_facade !!

128
  DBUG_RETURN(*this);
129 130 131
}

void
132
NdbColumnImpl::init(Type t)
133
{
pekka@mysql.com's avatar
pekka@mysql.com committed
134 135
  // do not use default_charset_info as it may not be initialized yet
  // use binary collation until NDB tests can handle charsets
136
  CHARSET_INFO* default_cs = &my_charset_bin;
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  m_type = t;
  switch (m_type) {
  case Tinyint:
  case Tinyunsigned:
  case Smallint:
  case Smallunsigned:
  case Mediumint:
  case Mediumunsigned:
  case Int:
  case Unsigned:
  case Bigint:
  case Bigunsigned:
  case Float:
  case Double:
    m_precision = 0;
    m_scale = 0;
    m_length = 1;
pekka@mysql.com's avatar
pekka@mysql.com committed
154
    m_cs = NULL;
155
    m_arrayType = NDB_ARRAYTYPE_FIXED;
156
    break;
157 158
  case Olddecimal:
  case Olddecimalunsigned:
159 160
  case Decimal:
  case Decimalunsigned:
161 162 163
    m_precision = 10;
    m_scale = 0;
    m_length = 1;
pekka@mysql.com's avatar
pekka@mysql.com committed
164
    m_cs = NULL;
165
    m_arrayType = NDB_ARRAYTYPE_FIXED;
166 167
    break;
  case Char:
168 169 170 171 172 173
    m_precision = 0;
    m_scale = 0;
    m_length = 1;
    m_cs = default_cs;
    m_arrayType = NDB_ARRAYTYPE_FIXED;
    break;
174 175 176 177
  case Varchar:
    m_precision = 0;
    m_scale = 0;
    m_length = 1;
pekka@mysql.com's avatar
pekka@mysql.com committed
178
    m_cs = default_cs;
179
    m_arrayType = NDB_ARRAYTYPE_SHORT_VAR;
180 181
    break;
  case Binary:
182 183 184 185 186 187
    m_precision = 0;
    m_scale = 0;
    m_length = 1;
    m_cs = NULL;
    m_arrayType = NDB_ARRAYTYPE_FIXED;
    break;
188
  case Varbinary:
189 190 191 192 193 194
    m_precision = 0;
    m_scale = 0;
    m_length = 1;
    m_cs = NULL;
    m_arrayType = NDB_ARRAYTYPE_SHORT_VAR;
    break;
195
  case Datetime:
196
  case Date:
197 198 199
    m_precision = 0;
    m_scale = 0;
    m_length = 1;
pekka@mysql.com's avatar
pekka@mysql.com committed
200
    m_cs = NULL;
201
    m_arrayType = NDB_ARRAYTYPE_FIXED;
202 203 204 205 206
    break;
  case Blob:
    m_precision = 256;
    m_scale = 8000;
    m_length = 4;
pekka@mysql.com's avatar
pekka@mysql.com committed
207
    m_cs = NULL;
208
    m_arrayType = NDB_ARRAYTYPE_FIXED;
209 210 211 212 213
    break;
  case Text:
    m_precision = 256;
    m_scale = 8000;
    m_length = 4;
pekka@mysql.com's avatar
pekka@mysql.com committed
214
    m_cs = default_cs;
215
    m_arrayType = NDB_ARRAYTYPE_FIXED;
216
    break;
217
  case Time:
218 219
  case Year:
  case Timestamp:
220 221 222 223
    m_precision = 0;
    m_scale = 0;
    m_length = 1;
    m_cs = NULL;
224
    m_arrayType = NDB_ARRAYTYPE_FIXED;
225
    break;
pekka@mysql.com's avatar
pekka@mysql.com committed
226 227 228 229 230
  case Bit:
    m_precision = 0;
    m_scale = 0;
    m_length = 1;
    m_cs = NULL;
231
    m_arrayType = NDB_ARRAYTYPE_FIXED;
pekka@mysql.com's avatar
pekka@mysql.com committed
232 233 234 235 236 237
    break;
  case Longvarchar:
    m_precision = 0;
    m_scale = 0;
    m_length = 1; // legal
    m_cs = default_cs;
238
    m_arrayType = NDB_ARRAYTYPE_MEDIUM_VAR;
pekka@mysql.com's avatar
pekka@mysql.com committed
239 240 241 242 243 244
    break;
  case Longvarbinary:
    m_precision = 0;
    m_scale = 0;
    m_length = 1; // legal
    m_cs = NULL;
245
    m_arrayType = NDB_ARRAYTYPE_MEDIUM_VAR;
pekka@mysql.com's avatar
pekka@mysql.com committed
246
    break;
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
247
  default:
248
  case Undefined:
249
    assert(false);
250
    break;
251
  }
252 253 254 255
  m_pk = false;
  m_nullable = false;
  m_distributionKey = false;
  m_keyInfoPos = 0;
256 257 258
  // next 2 are set at run time
  m_attrSize = 0;
  m_arraySize = 0;
259 260
  m_autoIncrement = false;
  m_autoIncrementInitialValue = 1;
mskold@mysql.com's avatar
mskold@mysql.com committed
261
  m_blobTable = NULL;
262 263 264 265 266
  m_storageType = NDB_STORAGETYPE_MEMORY;
#ifdef VM_TRACE
  if(NdbEnv_GetEnv("NDB_DEFAULT_DISK", (char *)0, 0))
    m_storageType = NDB_STORAGETYPE_DISK;
#endif
267 268 269 270
}

NdbColumnImpl::~NdbColumnImpl()
{
271
  DBUG_ENTER("NdbColumnImpl::~NdbColumnImpl");
272
  DBUG_PRINT("info", ("this: %p", this));
273 274 275
  if (m_blobTable != NULL)
    delete m_blobTable;
  m_blobTable = NULL;
276
  DBUG_VOID_RETURN;
277 278 279 280 281
}

bool
NdbColumnImpl::equal(const NdbColumnImpl& col) const 
{
282
  DBUG_ENTER("NdbColumnImpl::equal");
283
  DBUG_PRINT("info", ("this: %p  &col: %p", this, &col));
284
  if(strcmp(m_name.c_str(), col.m_name.c_str()) != 0){
285
    DBUG_RETURN(false);
286 287
  }
  if(m_type != col.m_type){
288
    DBUG_RETURN(false);
289 290
  }
  if(m_pk != col.m_pk){
291
    DBUG_RETURN(false);
292 293
  }
  if(m_nullable != col.m_nullable){
294
    DBUG_RETURN(false);
295
  }
296
  if (m_pk) {
297
    if (m_distributionKey != col.m_distributionKey) {
298
      DBUG_RETURN(false);
299 300
    }
  }
pekka@mysql.com's avatar
pekka@mysql.com committed
301 302 303 304
  if (m_precision != col.m_precision ||
      m_scale != col.m_scale ||
      m_length != col.m_length ||
      m_cs != col.m_cs) {
305
    DBUG_RETURN(false);
306 307
  }
  if (m_autoIncrement != col.m_autoIncrement){
308
    DBUG_RETURN(false);
309 310
  }
  if(strcmp(m_defaultValue.c_str(), col.m_defaultValue.c_str()) != 0){
311
    DBUG_RETURN(false);
312
  }
joreland@mysql.com's avatar
joreland@mysql.com committed
313

314 315 316 317
  if (m_arrayType != col.m_arrayType || m_storageType != col.m_storageType){
    DBUG_RETURN(false);
  }

318
  DBUG_RETURN(true);
319 320
}

joreland@mysql.com's avatar
joreland@mysql.com committed
321
NdbDictionary::Column *
322
NdbColumnImpl::create_pseudo(const char * name){
joreland@mysql.com's avatar
joreland@mysql.com committed
323 324 325 326 327
  NdbDictionary::Column * col = new NdbDictionary::Column();
  col->setName(name);
  if(!strcmp(name, "NDB$FRAGMENT")){
    col->setType(NdbDictionary::Column::Unsigned);
    col->m_impl.m_attrId = AttributeHeader::FRAGMENT;
328 329
    col->m_impl.m_attrSize = 4;
    col->m_impl.m_arraySize = 1;
330
  } else if(!strcmp(name, "NDB$FRAGMENT_FIXED_MEMORY")){
331
    col->setType(NdbDictionary::Column::Bigunsigned);
332 333 334 335 336 337
    col->m_impl.m_attrId = AttributeHeader::FRAGMENT_FIXED_MEMORY;
    col->m_impl.m_attrSize = 8;
    col->m_impl.m_arraySize = 1;
  } else if(!strcmp(name, "NDB$FRAGMENT_VARSIZED_MEMORY")){
    col->setType(NdbDictionary::Column::Bigunsigned);
    col->m_impl.m_attrId = AttributeHeader::FRAGMENT_VARSIZED_MEMORY;
338 339
    col->m_impl.m_attrSize = 8;
    col->m_impl.m_arraySize = 1;
joreland@mysql.com's avatar
joreland@mysql.com committed
340 341 342
  } else if(!strcmp(name, "NDB$ROW_COUNT")){
    col->setType(NdbDictionary::Column::Bigunsigned);
    col->m_impl.m_attrId = AttributeHeader::ROW_COUNT;
343
    col->m_impl.m_attrSize = 8;
344
    col->m_impl.m_arraySize = 1;
joreland@mysql.com's avatar
joreland@mysql.com committed
345 346 347
  } else if(!strcmp(name, "NDB$COMMIT_COUNT")){
    col->setType(NdbDictionary::Column::Bigunsigned);
    col->m_impl.m_attrId = AttributeHeader::COMMIT_COUNT;
348
    col->m_impl.m_attrSize = 8;
349
    col->m_impl.m_arraySize = 1;
350 351 352 353 354
  } else if(!strcmp(name, "NDB$ROW_SIZE")){
    col->setType(NdbDictionary::Column::Unsigned);
    col->m_impl.m_attrId = AttributeHeader::ROW_SIZE;
    col->m_impl.m_attrSize = 4;
    col->m_impl.m_arraySize = 1;
355 356 357 358 359
  } else if(!strcmp(name, "NDB$RANGE_NO")){
    col->setType(NdbDictionary::Column::Unsigned);
    col->m_impl.m_attrId = AttributeHeader::RANGE_NO;
    col->m_impl.m_attrSize = 4;
    col->m_impl.m_arraySize = 1;
360 361 362 363 364
  } else if(!strcmp(name, "NDB$DISK_REF")){
    col->setType(NdbDictionary::Column::Bigunsigned);
    col->m_impl.m_attrId = AttributeHeader::DISK_REF;
    col->m_impl.m_attrSize = 8;
    col->m_impl.m_arraySize = 1;
365 366 367 368 369
  } else if(!strcmp(name, "NDB$RECORDS_IN_RANGE")){
    col->setType(NdbDictionary::Column::Unsigned);
    col->m_impl.m_attrId = AttributeHeader::RECORDS_IN_RANGE;
    col->m_impl.m_attrSize = 4;
    col->m_impl.m_arraySize = 4;
370 371 372 373 374 375 376 377 378 379 380
  } else if(!strcmp(name, "NDB$ROWID")){
    col->setType(NdbDictionary::Column::Bigunsigned);
    col->m_impl.m_attrId = AttributeHeader::ROWID;
    col->m_impl.m_attrSize = 4;
    col->m_impl.m_arraySize = 2;
  } else if(!strcmp(name, "NDB$ROW_GCI")){
    col->setType(NdbDictionary::Column::Bigunsigned);
    col->m_impl.m_attrId = AttributeHeader::ROW_GCI;
    col->m_impl.m_attrSize = 8;
    col->m_impl.m_arraySize = 1;
    col->m_impl.m_nullable = true;
joreland@mysql.com's avatar
joreland@mysql.com committed
381 382 383
  } else {
    abort();
  }
384
  col->m_impl.m_storageType = NDB_STORAGETYPE_MEMORY;
385
  return col;
386 387 388 389 390 391 392
}

/**
 * NdbTableImpl
 */

NdbTableImpl::NdbTableImpl()
393 394
  : NdbDictionary::Table(* this), 
    NdbDictObjectImpl(NdbDictionary::Object::UserTable), m_facade(this)
395
{
396
  DBUG_ENTER("NdbTableImpl::NdbTableImpl");
397
  DBUG_PRINT("info", ("this: %p", this));
398
  init();
399
  DBUG_VOID_RETURN;
400 401 402
}

NdbTableImpl::NdbTableImpl(NdbDictionary::Table & f)
403 404
  : NdbDictionary::Table(* this), 
    NdbDictObjectImpl(NdbDictionary::Object::UserTable), m_facade(&f)
405
{
406
  DBUG_ENTER("NdbTableImpl::NdbTableImpl");
407
  DBUG_PRINT("info", ("this: %p", this));
408
  init();
409
  DBUG_VOID_RETURN;
410 411 412 413
}

NdbTableImpl::~NdbTableImpl()
{
414
  DBUG_ENTER("NdbTableImpl::~NdbTableImpl");
415
  DBUG_PRINT("info", ("this: %p", this));
416 417 418 419 420
  if (m_index != 0) {
    delete m_index;
    m_index = 0;
  }
  for (unsigned i = 0; i < m_columns.size(); i++)
421 422
    delete m_columns[i];
  DBUG_VOID_RETURN;
423 424 425 426
}

void
NdbTableImpl::init(){
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
427
  m_changeMask= 0;
428
  m_id= RNIL;
429 430 431
  m_version = ~0;
  m_status = NdbDictionary::Object::Invalid;
  m_type = NdbDictionary::Object::TypeUndefined;
432
  m_primaryTableId= RNIL;
433 434 435 436
  m_internalName.clear();
  m_externalName.clear();
  m_newExternalName.clear();
  m_mysqlName.clear();
437
  m_frm.clear();
438
  m_newFrm.clear();
439 440 441 442 443 444 445 446 447
  m_ts_name.clear();
  m_new_ts_name.clear();
  m_ts.clear();
  m_new_ts.clear();
  m_fd.clear();
  m_new_fd.clear();
  m_range.clear();
  m_new_range.clear();
  m_fragmentType= NdbDictionary::Object::FragAllSmall;
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
448 449
  m_hashValueMask= 0;
  m_hashpointerValue= 0;
450
  m_linear_flag= true;
451 452
  m_primaryTable.clear();
  m_default_no_part_flag = 1;
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
453
  m_logging= true;
knielsen@ymer.(none)'s avatar
knielsen@ymer.(none) committed
454
  m_temporary = false;
455 456
  m_row_gci = true;
  m_row_checksum = true;
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
457 458 459 460 461 462
  m_kvalue= 6;
  m_minLoadFactor= 78;
  m_maxLoadFactor= 80;
  m_keyLenInWords= 0;
  m_fragmentCount= 0;
  m_index= NULL;
463
  m_indexType= NdbDictionary::Object::TypeUndefined;
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
464 465 466 467
  m_noOfKeys= 0;
  m_noOfDistributionKeys= 0;
  m_noOfBlobs= 0;
  m_replicaCount= 0;
468 469
  m_min_rows = 0;
  m_max_rows = 0;
470
  m_tablespace_name.clear();
471
  m_tablespace_id = ~0;
472
  m_tablespace_version = ~0;
473
  m_single_user_mode = 0;
474 475 476 477 478
}

bool
NdbTableImpl::equal(const NdbTableImpl& obj) const 
{
479
  DBUG_ENTER("NdbTableImpl::equal");
480 481 482
  if ((m_internalName.c_str() == NULL) || 
      (strcmp(m_internalName.c_str(), "") == 0) ||
      (obj.m_internalName.c_str() == NULL) || 
483 484
      (strcmp(obj.m_internalName.c_str(), "") == 0))
  {
485
    // Shallow equal
486 487
    if(strcmp(getName(), obj.getName()) != 0)
    {
488 489
      DBUG_PRINT("info",("name %s != %s",getName(),obj.getName()));
      DBUG_RETURN(false);    
490
    }
491 492 493
  }
  else
  {
494
    // Deep equal
495
    if(strcmp(m_internalName.c_str(), obj.m_internalName.c_str()) != 0)
496 497 498 499 500
    {
      DBUG_PRINT("info",("m_internalName %s != %s",
			 m_internalName.c_str(),obj.m_internalName.c_str()));
      DBUG_RETURN(false);
    }
501
  }
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
  if (m_frm.length() != obj.m_frm.length() ||
      (memcmp(m_frm.get_data(), obj.m_frm.get_data(), m_frm.length())))
  {
    DBUG_PRINT("info",("m_frm not equal"));
    DBUG_RETURN(false);
  }
  if (m_fd.length() != obj.m_fd.length() ||
      (memcmp(m_fd.get_data(), obj.m_fd.get_data(), m_fd.length())))
  {
    DBUG_PRINT("info",("m_fd not equal"));
    DBUG_RETURN(false);
  }
  if (m_ts.length() != obj.m_ts.length() ||
      (memcmp(m_ts.get_data(), obj.m_ts.get_data(), m_ts.length())))
  {
    DBUG_PRINT("info",("m_ts not equal"));
    DBUG_RETURN(false);
  }
  if (m_range.length() != obj.m_range.length() ||
      (memcmp(m_range.get_data(), obj.m_range.get_data(), m_range.length())))
  {
    DBUG_PRINT("info",("m_range not equal"));
    DBUG_RETURN(false);
  }
  if(m_fragmentType != obj.m_fragmentType)
  {
    DBUG_PRINT("info",("m_fragmentType %d != %d",m_fragmentType,
                        obj.m_fragmentType));
530
    DBUG_RETURN(false);
531
  }
532 533 534 535
  if(m_columns.size() != obj.m_columns.size())
  {
    DBUG_PRINT("info",("m_columns.size %d != %d",m_columns.size(),
                       obj.m_columns.size()));
536
    DBUG_RETURN(false);
537 538
  }

539 540 541 542
  for(unsigned i = 0; i<obj.m_columns.size(); i++)
  {
    if(!m_columns[i]->equal(* obj.m_columns[i]))
    {
543 544
      DBUG_PRINT("info",("m_columns [%d] != [%d]",i,i));
      DBUG_RETURN(false);
545 546 547
    }
  }
  
548 549 550 551 552 553 554
  if(m_linear_flag != obj.m_linear_flag)
  {
    DBUG_PRINT("info",("m_linear_flag %d != %d",m_linear_flag,
                        obj.m_linear_flag));
    DBUG_RETURN(false);
  }

555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
  if(m_max_rows != obj.m_max_rows)
  {
    DBUG_PRINT("info",("m_max_rows %d != %d",(int32)m_max_rows,
                       (int32)obj.m_max_rows));
    DBUG_RETURN(false);
  }

  if(m_default_no_part_flag != obj.m_default_no_part_flag)
  {
    DBUG_PRINT("info",("m_default_no_part_flag %d != %d",m_default_no_part_flag,
                        obj.m_default_no_part_flag));
    DBUG_RETURN(false);
  }

  if(m_logging != obj.m_logging)
  {
571 572
    DBUG_PRINT("info",("m_logging %d != %d",m_logging,obj.m_logging));
    DBUG_RETURN(false);
573 574
  }

knielsen@ymer.(none)'s avatar
knielsen@ymer.(none) committed
575 576 577 578 579 580
  if(m_temporary != obj.m_temporary)
  {
    DBUG_PRINT("info",("m_temporary %d != %d",m_temporary,obj.m_temporary));
    DBUG_RETURN(false);
  }

581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
  if(m_row_gci != obj.m_row_gci)
  {
    DBUG_PRINT("info",("m_row_gci %d != %d",m_row_gci,obj.m_row_gci));
    DBUG_RETURN(false);
  }

  if(m_row_checksum != obj.m_row_checksum)
  {
    DBUG_PRINT("info",("m_row_checksum %d != %d",m_row_checksum,
                        obj.m_row_checksum));
    DBUG_RETURN(false);
  }

  if(m_kvalue != obj.m_kvalue)
  {
596 597
    DBUG_PRINT("info",("m_kvalue %d != %d",m_kvalue,obj.m_kvalue));
    DBUG_RETURN(false);
598 599
  }

600 601 602 603
  if(m_minLoadFactor != obj.m_minLoadFactor)
  {
    DBUG_PRINT("info",("m_minLoadFactor %d != %d",m_minLoadFactor,
                        obj.m_minLoadFactor));
604
    DBUG_RETURN(false);
605 606
  }

607 608 609 610
  if(m_maxLoadFactor != obj.m_maxLoadFactor)
  {
    DBUG_PRINT("info",("m_maxLoadFactor %d != %d",m_maxLoadFactor,
                        obj.m_maxLoadFactor));
611
    DBUG_RETURN(false);
612
  }
613

614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
  if(m_tablespace_id != obj.m_tablespace_id)
  {
    DBUG_PRINT("info",("m_tablespace_id %d != %d",m_tablespace_id,
                        obj.m_tablespace_id));
    DBUG_RETURN(false);
  }

  if(m_tablespace_version != obj.m_tablespace_version)
  {
    DBUG_PRINT("info",("m_tablespace_version %d != %d",m_tablespace_version,
                        obj.m_tablespace_version));
    DBUG_RETURN(false);
  }

  if(m_id != obj.m_id)
  {
    DBUG_PRINT("info",("m_id %d != %d",m_id,obj.m_id));
    DBUG_RETURN(false);
  }

  if(m_version != obj.m_version)
  {
    DBUG_PRINT("info",("m_version %d != %d",m_version,obj.m_version));
    DBUG_RETURN(false);
  }

  if(m_type != obj.m_type)
  {
    DBUG_PRINT("info",("m_type %d != %d",m_type,obj.m_type));
    DBUG_RETURN(false);
  }

  if (m_type == NdbDictionary::Object::UniqueHashIndex ||
      m_type == NdbDictionary::Object::OrderedIndex)
  {
    if(m_primaryTableId != obj.m_primaryTableId)
    {
      DBUG_PRINT("info",("m_primaryTableId %d != %d",m_primaryTableId,
                 obj.m_primaryTableId));
      DBUG_RETURN(false);
    }
    if (m_indexType != obj.m_indexType)
    {
      DBUG_PRINT("info",("m_indexType %d != %d",m_indexType,obj.m_indexType));
      DBUG_RETURN(false);
    }
    if(strcmp(m_primaryTable.c_str(), obj.m_primaryTable.c_str()) != 0)
    {
      DBUG_PRINT("info",("m_primaryTable %s != %s",
			 m_primaryTable.c_str(),obj.m_primaryTable.c_str()));
      DBUG_RETURN(false);
    }
  }
667 668 669 670 671 672 673 674 675
  
  if(m_single_user_mode != obj.m_single_user_mode)
  {
    DBUG_PRINT("info",("m_single_user_mode %d != %d",
                       (int32)m_single_user_mode,
                       (int32)obj.m_single_user_mode));
    DBUG_RETURN(false);
  }

676
  DBUG_RETURN(true);
677 678 679 680 681
}

void
NdbTableImpl::assign(const NdbTableImpl& org)
{
682
  DBUG_ENTER("NdbColumnImpl::assign");
683
  DBUG_PRINT("info", ("this: %p  &org: %p", this, &org));
684 685
  /* m_changeMask intentionally not copied */
  m_primaryTableId = org.m_primaryTableId;
686
  m_internalName.assign(org.m_internalName);
687
  updateMysqlName();
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
688 689 690 691 692 693
  // If the name has been explicitly set, use that name
  // otherwise use the fetched name
  if (!org.m_newExternalName.empty())
    m_externalName.assign(org.m_newExternalName);
  else
    m_externalName.assign(org.m_externalName);
694
  m_frm.assign(org.m_frm.get_data(), org.m_frm.length());
695 696 697 698 699 700 701 702 703
  m_ts_name.assign(org.m_ts_name.get_data(), org.m_ts_name.length());
  m_new_ts_name.assign(org.m_new_ts_name.get_data(),
                       org.m_new_ts_name.length());
  m_ts.assign(org.m_ts.get_data(), org.m_ts.length());
  m_new_ts.assign(org.m_new_ts.get_data(), org.m_new_ts.length());
  m_fd.assign(org.m_fd.get_data(), org.m_fd.length());
  m_new_fd.assign(org.m_new_fd.get_data(), org.m_new_fd.length());
  m_range.assign(org.m_range.get_data(), org.m_range.length());
  m_new_range.assign(org.m_new_range.get_data(), org.m_new_range.length());
704

705 706 707 708 709
  m_fragmentType = org.m_fragmentType;
  /*
    m_columnHashMask, m_columnHash, m_hashValueMask, m_hashpointerValue
    is state calculated by computeAggregates and buildColumnHash
  */
710 711 712 713 714 715 716 717
  unsigned i;
  for(i = 0; i < m_columns.size(); i++)
  {
    delete m_columns[i];
  }
  m_columns.clear();
  for(i = 0; i < org.m_columns.size(); i++)
  {
718 719
    NdbColumnImpl * col = new NdbColumnImpl();
    const NdbColumnImpl * iorg = org.m_columns[i];
joreland@mysql.com's avatar
joreland@mysql.com committed
720
    (* col) = (* iorg);
721 722 723
    m_columns.push_back(col);
  }

724 725
  m_fragments = org.m_fragments;

726
  m_linear_flag = org.m_linear_flag;
727 728
  m_max_rows = org.m_max_rows;
  m_default_no_part_flag = org.m_default_no_part_flag;
729
  m_logging = org.m_logging;
knielsen@ymer.(none)'s avatar
knielsen@ymer.(none) committed
730
  m_temporary = org.m_temporary;
731 732
  m_row_gci = org.m_row_gci;
  m_row_checksum = org.m_row_checksum;
733 734 735
  m_kvalue = org.m_kvalue;
  m_minLoadFactor = org.m_minLoadFactor;
  m_maxLoadFactor = org.m_maxLoadFactor;
736 737
  m_keyLenInWords = org.m_keyLenInWords;
  m_fragmentCount = org.m_fragmentCount;
738
  
739 740
  m_single_user_mode = org.m_single_user_mode;

741 742 743
  if (m_index != 0)
    delete m_index;
  m_index = org.m_index;
744 745 746 747
 
  m_primaryTable = org.m_primaryTable;
  m_indexType = org.m_indexType;

748
  m_noOfKeys = org.m_noOfKeys;
749
  m_noOfDistributionKeys = org.m_noOfDistributionKeys;
pekka@mysql.com's avatar
pekka@mysql.com committed
750
  m_noOfBlobs = org.m_noOfBlobs;
751
  m_replicaCount = org.m_replicaCount;
752

753
  m_id = org.m_id;
754 755
  m_version = org.m_version;
  m_status = org.m_status;
756

757 758 759
  m_max_rows = org.m_max_rows;
  m_min_rows = org.m_min_rows;

760 761 762
  m_tablespace_name = org.m_tablespace_name;
  m_tablespace_id= org.m_tablespace_id;
  m_tablespace_version = org.m_tablespace_version;
763
  DBUG_VOID_RETURN;
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
}

void NdbTableImpl::setName(const char * name)
{
  m_newExternalName.assign(name);
}

const char * 
NdbTableImpl::getName() const
{
  if (m_newExternalName.empty())
    return m_externalName.c_str();
  else
    return m_newExternalName.c_str();
}

780 781 782 783 784 785 786
void
NdbTableImpl::computeAggregates()
{
  m_noOfKeys = 0;
  m_keyLenInWords = 0;
  m_noOfDistributionKeys = 0;
  m_noOfBlobs = 0;
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
787
  m_noOfDiskColumns = 0;
788 789 790 791 792 793 794
  Uint32 i, n;
  for (i = 0; i < m_columns.size(); i++) {
    NdbColumnImpl* col = m_columns[i];
    if (col->m_pk) {
      m_noOfKeys++;
      m_keyLenInWords += (col->m_attrSize * col->m_arraySize + 3) / 4;
    }
795 796
    if (col->m_distributionKey)
      m_noOfDistributionKeys++; // XXX check PK
797 798 799
    
    if (col->getBlobType())
      m_noOfBlobs++;
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
800 801 802 803

    if (col->getStorageType() == NdbDictionary::Column::StorageTypeDisk)
      m_noOfDiskColumns++;
    
804 805 806 807 808 809 810 811 812 813 814 815 816
    col->m_keyInfoPos = ~0;
  }
  if (m_noOfDistributionKeys == m_noOfKeys) {
    // all is none!
    m_noOfDistributionKeys = 0;
  }

  if (m_noOfDistributionKeys == 0) 
  {
    // none is all!
    for (i = 0, n = m_noOfKeys; n != 0; i++) {
      NdbColumnImpl* col = m_columns[i];
      if (col->m_pk) {
817
        col->m_distributionKey = true;
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
        n--;
      }
    }
  }
  
  Uint32 keyInfoPos = 0;
  for (i = 0, n = m_noOfKeys; n != 0; i++) {
    NdbColumnImpl* col = m_columns[i];
    if (col->m_pk) {
      col->m_keyInfoPos = keyInfoPos++;
      n--;
    }
  }
}

833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
// TODO add error checks
// TODO use these internally at create and retrieve
int
NdbTableImpl::aggregate(NdbError& error)
{
  computeAggregates();
  return 0;
}
int
NdbTableImpl::validate(NdbError& error)
{
  if (aggregate(error) == -1)
    return -1;
  return 0;
}

849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
const void*
NdbTableImpl::getTablespaceNames() const
{
  if (m_new_ts_name.empty())
    return m_ts_name.get_data();
  else
    return m_new_ts_name.get_data();
}

Uint32
NdbTableImpl::getTablespaceNamesLen() const
{
  if (m_new_ts_name.empty())
    return m_ts_name.length();
  else
    return m_new_ts_name.length();
}

void NdbTableImpl::setTablespaceNames(const void *data, Uint32 len)
{
  m_new_ts_name.assign(data, len);
}

void NdbTableImpl::setFragmentCount(Uint32 count)
{
  m_fragmentCount= count;
}

Uint32 NdbTableImpl::getFragmentCount() const
{
  return m_fragmentCount;
}

882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
void NdbTableImpl::setFrm(const void* data, Uint32 len)
{
  m_newFrm.assign(data, len);
}

const void * 
NdbTableImpl::getFrmData() const
{
  if (m_newFrm.empty())
    return m_frm.get_data();
  else
    return m_newFrm.get_data();
}

Uint32
NdbTableImpl::getFrmLength() const 
{
  if (m_newFrm.empty())
    return m_frm.length();
  else
    return m_newFrm.length();
}

905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
void NdbTableImpl::setFragmentData(const void* data, Uint32 len)
{
  m_new_fd.assign(data, len);
}

const void * 
NdbTableImpl::getFragmentData() const
{
  if (m_new_fd.empty())
    return m_fd.get_data();
  else
    return m_new_fd.get_data();
}

Uint32
NdbTableImpl::getFragmentDataLen() const 
{
  if (m_new_fd.empty())
    return m_fd.length();
  else
    return m_new_fd.length();
}

void NdbTableImpl::setTablespaceData(const void* data, Uint32 len)
{
  m_new_ts.assign(data, len);
}

const void * 
NdbTableImpl::getTablespaceData() const
{
  if (m_new_ts.empty())
    return m_ts.get_data();
  else
    return m_new_ts.get_data();
}

Uint32
NdbTableImpl::getTablespaceDataLen() const 
{
  if (m_new_ts.empty())
    return m_ts.length();
  else
    return m_new_ts.length();
}

void NdbTableImpl::setRangeListData(const void* data, Uint32 len)
{
  m_new_range.assign(data, len);
}

const void * 
NdbTableImpl::getRangeListData() const
{
  if (m_new_range.empty())
    return m_range.get_data();
  else
    return m_new_range.get_data();
}

Uint32
NdbTableImpl::getRangeListDataLen() const 
{
  if (m_new_range.empty())
    return m_range.length();
  else
    return m_new_range.length();
}

974 975 976 977 978 979 980 981 982 983 984
void
NdbTableImpl::updateMysqlName()
{
  Vector<BaseString> v;
  if (m_internalName.split(v,"/") == 3)
  {
    m_mysqlName.assfmt("%s/%s",v[0].c_str(),v[2].c_str());
    return;
  }
  m_mysqlName.assign("");
}
985 986 987 988

void
NdbTableImpl::buildColumnHash(){
  const Uint32 size = m_columns.size();
989
  int i;
990
  for(i = 31; i >= 0; i--){
991 992 993 994 995 996 997 998
    if(((1 << i) & size) != 0){
      m_columnHashMask = (1 << (i + 1)) - 1;
      break;
    }
  }

  Vector<Uint32> hashValues;
  Vector<Vector<Uint32> > chains; chains.fill(size, hashValues);
999
  for(i = 0; i< (int) size; i++){
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
    Uint32 hv = Hash(m_columns[i]->getName()) & 0xFFFE;
    Uint32 bucket = hv & m_columnHashMask;
    bucket = (bucket < size ? bucket : bucket - size);
    assert(bucket < size);
    hashValues.push_back(hv);
    chains[bucket].push_back(i);
  }

  m_columnHash.clear();
  Uint32 tmp = 1; 
  m_columnHash.fill((unsigned)size-1, tmp);   // Default no chaining

  Uint32 pos = 0; // In overflow vector
1013
  for(i = 0; i< (int) size; i++){
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
    Uint32 sz = chains[i].size();
    if(sz == 1){
      Uint32 col = chains[i][0];
      Uint32 hv = hashValues[col];
      Uint32 bucket = hv & m_columnHashMask;
      bucket = (bucket < size ? bucket : bucket - size);
      m_columnHash[bucket] = (col << 16) | hv | 1;
    } else if(sz > 1){
      Uint32 col = chains[i][0];
      Uint32 hv = hashValues[col];
      Uint32 bucket = hv & m_columnHashMask;
      bucket = (bucket < size ? bucket : bucket - size);
      m_columnHash[bucket] = (sz << 16) | (((size - bucket) + pos) << 1);
      for(size_t j = 0; j<sz; j++, pos++){
	Uint32 col = chains[i][j];	
	Uint32 hv = hashValues[col];
	m_columnHash.push_back((col << 16) | hv);
      }
    }
  }

  m_columnHash.push_back(0); // Overflow when looping in end of array

#if 0
  for(size_t i = 0; i<m_columnHash.size(); i++){
    Uint32 tmp = m_columnHash[i];
    int col = -1;
    if(i < size && (tmp & 1) == 1){
      col = (tmp >> 16);
    } else if(i >= size){
      col = (tmp >> 16);
    }
    ndbout_c("m_columnHash[%d] %s = %x", 
	     i, col > 0 ? m_columns[col]->getName() : "" , m_columnHash[i]);
  }
#endif
}
1051 1052 1053 1054

Uint32
NdbTableImpl::get_nodes(Uint32 hashValue, const Uint16 ** nodes) const
{
1055 1056 1057 1058
  Uint32 fragmentId;
  if(m_replicaCount == 0)
    return 0;
  switch (m_fragmentType)
1059
  {
1060 1061 1062 1063 1064
    case NdbDictionary::Object::FragAllSmall:
    case NdbDictionary::Object::FragAllMedium:
    case NdbDictionary::Object::FragAllLarge:
    case NdbDictionary::Object::FragSingle:
    case NdbDictionary::Object::DistrKeyLin:
1065
    {
1066
      fragmentId = hashValue & m_hashValueMask;
1067 1068 1069
      if(fragmentId < m_hashpointerValue) 
        fragmentId = hashValue & ((m_hashValueMask << 1) + 1);
      break;
1070
    }
1071
    case NdbDictionary::Object::DistrKeyHash:
1072
    {
1073 1074
      fragmentId = hashValue % m_fragmentCount;
      break;
1075
    }
1076 1077 1078 1079 1080 1081 1082 1083
    default:
      return 0;
  }
  Uint32 pos = fragmentId * m_replicaCount;
  if (pos + m_replicaCount <= m_fragments.size())
  {
    *nodes = m_fragments.getBase()+pos;
    return m_replicaCount;
1084 1085 1086
  }
  return 0;
}
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134

int
NdbDictionary::Table::checkColumns(const Uint32* map, Uint32 len) const
{
  int ret = 0;
  Uint32 colCnt = m_impl.m_columns.size();
  if (map == 0)
  {
    ret |= 1;
    ret |= (m_impl.m_noOfDiskColumns) ? 2 : 0;
    ret |= (colCnt > m_impl.m_noOfDiskColumns) ? 4 : 0;
    return ret;
  }

  NdbColumnImpl** cols = m_impl.m_columns.getBase();
  const char * ptr = reinterpret_cast<const char*>(map);
  const char * end = ptr + len;
  Uint32 no = 0;
  while (ptr < end)
  {
    Uint32 val = (Uint32)* ptr;
    Uint32 idx = 1;
    for (Uint32 i = 0; i<8; i++)
    {
      if (val & idx)
      {
	if (cols[no]->getPrimaryKey())
	  ret |= 1;
	else
	{
	  if (cols[no]->getStorageType() == NdbDictionary::Column::StorageTypeDisk)
	    ret |= 2;
	  else
	    ret |= 4;
	}
      }
      no ++;
      idx *= 2; 
      if (no == colCnt)
	return ret;
    }
    
    ptr++;
  }
  return ret;
}


1135 1136 1137 1138 1139 1140
  
/**
 * NdbIndexImpl
 */

NdbIndexImpl::NdbIndexImpl() : 
1141
  NdbDictionary::Index(* this),
1142
  NdbDictObjectImpl(NdbDictionary::Object::OrderedIndex), m_facade(this)
1143
{
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
1144
  init();
1145 1146 1147 1148
}

NdbIndexImpl::NdbIndexImpl(NdbDictionary::Index & f) : 
  NdbDictionary::Index(* this), 
1149
  NdbDictObjectImpl(NdbDictionary::Object::OrderedIndex), m_facade(&f)
1150
{
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
1151 1152 1153 1154 1155
  init();
}

void NdbIndexImpl::init()
{
1156 1157
  m_id= RNIL;
  m_type= NdbDictionary::Object::TypeUndefined;
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
1158
  m_logging= true;
knielsen@ymer.(none)'s avatar
knielsen@ymer.(none) committed
1159
  m_temporary= false;
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
1160
  m_table= NULL;
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
}

NdbIndexImpl::~NdbIndexImpl(){
  for (unsigned i = 0; i < m_columns.size(); i++)
    delete m_columns[i];  
}

void NdbIndexImpl::setName(const char * name)
{
  m_externalName.assign(name);
}

const char * 
NdbIndexImpl::getName() const
{
  return m_externalName.c_str();
}
 
void 
NdbIndexImpl::setTable(const char * table)
{
  m_tableName.assign(table);
}
 
const char * 
NdbIndexImpl::getTable() const
{
  return m_tableName.c_str();
}

1191 1192 1193 1194 1195 1196
const NdbTableImpl *
NdbIndexImpl::getIndexTable() const
{
  return m_table;
}

1197 1198 1199 1200 1201 1202
/**
 * NdbEventImpl
 */

NdbEventImpl::NdbEventImpl() : 
  NdbDictionary::Event(* this),
1203
  NdbDictObjectImpl(NdbDictionary::Object::TypeUndefined), m_facade(this)
1204
{
1205
  DBUG_ENTER("NdbEventImpl::NdbEventImpl");
1206
  DBUG_PRINT("info", ("this: %p", this));
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
1207
  init();
1208
  DBUG_VOID_RETURN;
1209 1210 1211 1212
}

NdbEventImpl::NdbEventImpl(NdbDictionary::Event & f) : 
  NdbDictionary::Event(* this),
1213
  NdbDictObjectImpl(NdbDictionary::Object::TypeUndefined), m_facade(&f)
1214
{
1215
  DBUG_ENTER("NdbEventImpl::NdbEventImpl");
1216
  DBUG_PRINT("info", ("this: %p", this));
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
1217
  init();
1218
  DBUG_VOID_RETURN;
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
1219 1220 1221 1222 1223 1224 1225 1226
}

void NdbEventImpl::init()
{
  m_eventId= RNIL;
  m_eventKey= RNIL;
  mi_type= 0;
  m_dur= NdbDictionary::Event::ED_UNDEFINED;
1227
  m_mergeEvents = false;
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
1228
  m_tableImpl= NULL;
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
1229
  m_rep= NdbDictionary::Event::ER_UPDATED;
1230 1231 1232 1233
}

NdbEventImpl::~NdbEventImpl()
{
1234
  DBUG_ENTER("NdbEventImpl::~NdbEventImpl");
1235
  DBUG_PRINT("info", ("this: %p", this));
1236 1237
  for (unsigned i = 0; i < m_columns.size(); i++)
    delete  m_columns[i];
1238 1239
  if (m_tableImpl)
    delete m_tableImpl;
1240
  DBUG_VOID_RETURN;
1241 1242 1243 1244
}

void NdbEventImpl::setName(const char * name)
{
1245
  m_name.assign(name);
1246 1247
}

1248 1249
const char *NdbEventImpl::getName() const
{
1250
  return m_name.c_str();
1251 1252
}

1253 1254 1255
void 
NdbEventImpl::setTable(const NdbDictionary::Table& table)
{
1256
  setTable(&NdbTableImpl::getImpl(table));
1257 1258 1259
  m_tableName.assign(m_tableImpl->getName());
}

1260 1261 1262
void 
NdbEventImpl::setTable(NdbTableImpl *tableImpl)
{
1263
  DBUG_ENTER("NdbEventImpl::setTable");
1264
  DBUG_PRINT("info", ("this: %p  tableImpl: %p", this, tableImpl));
1265 1266 1267 1268 1269
  DBUG_ASSERT(tableImpl->m_status != NdbDictionary::Object::Invalid);
  if (!m_tableImpl) 
    m_tableImpl = new NdbTableImpl();
  // Copy table, since event might be accessed from different threads
  m_tableImpl->assign(*tableImpl);
1270
  DBUG_VOID_RETURN;
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
}

const NdbDictionary::Table *
NdbEventImpl::getTable() const
{
  if (m_tableImpl) 
    return m_tableImpl->m_facade;
  else
    return NULL;
}

1282 1283 1284 1285 1286 1287
void 
NdbEventImpl::setTable(const char * table)
{
  m_tableName.assign(table);
}

1288 1289
const char *
NdbEventImpl::getTableName() const
1290 1291 1292 1293 1294 1295 1296
{
  return m_tableName.c_str();
}

void
NdbEventImpl::addTableEvent(const NdbDictionary::Event::TableEvent t =  NdbDictionary::Event::TE_ALL)
{
1297
  mi_type |= (unsigned)t;
1298 1299
}

1300 1301 1302 1303 1304 1305
bool
NdbEventImpl::getTableEvent(const NdbDictionary::Event::TableEvent t) const
{
  return (mi_type & (unsigned)t) == (unsigned)t;
}

1306
void
1307
NdbEventImpl::setDurability(NdbDictionary::Event::EventDurability d)
1308 1309 1310 1311
{
  m_dur = d;
}

1312 1313 1314 1315 1316 1317
NdbDictionary::Event::EventDurability
NdbEventImpl::getDurability() const
{
  return m_dur;
}

tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
void
NdbEventImpl::setReport(NdbDictionary::Event::EventReport r)
{
  m_rep = r;
}

NdbDictionary::Event::EventReport
NdbEventImpl::getReport() const
{
  return m_rep;
}

1330 1331 1332 1333 1334
int NdbEventImpl::getNoOfEventColumns() const
{
  return m_attrIds.size() + m_columns.size();
}

1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
const NdbDictionary::Column *
NdbEventImpl::getEventColumn(unsigned no) const
{
  if (m_columns.size())
  {
    if (no < m_columns.size())
    {
      return m_columns[no];
    }
  }
  else if (m_attrIds.size())
  {
    if (no < m_attrIds.size())
    {
      NdbTableImpl* tab= m_tableImpl;
      if (tab == 0)
        return 0;
      return tab->getColumn(m_attrIds[no]);
    }
  }
  return 0;
}

1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
/**
 * NdbDictionaryImpl
 */

NdbDictionaryImpl::NdbDictionaryImpl(Ndb &ndb)
  : NdbDictionary::Dictionary(* this), 
    m_facade(this), 
    m_receiver(m_error),
    m_ndb(ndb)
{
  m_globalHash = 0;
1369
  m_local_table_data_size= 0;
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
}

NdbDictionaryImpl::NdbDictionaryImpl(Ndb &ndb,
				     NdbDictionary::Dictionary & f)
  : NdbDictionary::Dictionary(* this), 
    m_facade(&f), 
    m_receiver(m_error),
    m_ndb(ndb)
{
  m_globalHash = 0;
1380
  m_local_table_data_size= 0;
1381 1382 1383 1384
}

NdbDictionaryImpl::~NdbDictionaryImpl()
{
1385
  NdbElement_t<Ndb_local_table_info> * curr = m_localHash.m_tableHash.getNext(0);
1386 1387 1388
  if(m_globalHash){
    while(curr != 0){
      m_globalHash->lock();
1389
      m_globalHash->release(curr->theData->m_table_impl);
1390
      Ndb_local_table_info::destroy(curr->theData);
1391 1392 1393 1394 1395 1396
      m_globalHash->unlock();
      
      curr = m_localHash.m_tableHash.getNext(curr);
    }
  } else {
    assert(curr == 0);
joreland@mysql.com's avatar
joreland@mysql.com committed
1397
  }
1398 1399
}

1400 1401
NdbTableImpl *
NdbDictionaryImpl::fetchGlobalTableImplRef(const GlobalCacheInitObject &obj)
1402
{
1403
  DBUG_ENTER("fetchGlobalTableImplRef");
1404
  NdbTableImpl *impl;
1405

1406
  m_globalHash->lock();
1407
  impl = m_globalHash->get(obj.m_name.c_str());
1408 1409 1410
  m_globalHash->unlock();

  if (impl == 0){
1411
    impl = m_receiver.getTable(obj.m_name.c_str(),
1412
			       m_ndb.usingFullyQualifiedNames());
1413 1414 1415 1416
    if (impl != 0 && obj.init(*impl))
    {
      delete impl;
      impl = 0;
1417
    }
1418
    m_globalHash->lock();
1419
    m_globalHash->put(obj.m_name.c_str(), impl);
1420 1421 1422
    m_globalHash->unlock();
  }

1423
  DBUG_RETURN(impl);
1424
}
1425

1426 1427 1428
void
NdbDictionaryImpl::putTable(NdbTableImpl *impl)
{
1429 1430
  NdbTableImpl *old;

1431 1432 1433
  int ret = getBlobTables(*impl);
  assert(ret == 0);

1434
  m_globalHash->lock();
1435 1436
  if ((old= m_globalHash->get(impl->m_internalName.c_str())))
  {
1437 1438 1439 1440
    m_globalHash->alter_table_rep(old->m_internalName.c_str(),
                                  impl->m_id,
                                  impl->m_version,
                                  FALSE);
1441
  }
1442 1443 1444 1445 1446 1447 1448 1449
  m_globalHash->put(impl->m_internalName.c_str(), impl);
  m_globalHash->unlock();
  Ndb_local_table_info *info=
    Ndb_local_table_info::create(impl, m_local_table_data_size);
  
  m_localHash.put(impl->m_internalName.c_str(), info);
}

1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
int
NdbDictionaryImpl::getBlobTables(NdbTableImpl &t)
{
  unsigned n= t.m_noOfBlobs;
  DBUG_ENTER("NdbDictionaryImpl::addBlobTables");
  // optimized for blob column being the last one
  // and not looking for more than one if not neccessary
  for (unsigned i = t.m_columns.size(); i > 0 && n > 0;) {
    i--;
    NdbColumnImpl & c = *t.m_columns[i];
    if (! c.getBlobType() || c.getPartSize() == 0)
      continue;
    n--;
    // retrieve blob table def from DICT - by-pass cache
    char btname[NdbBlobImpl::BlobTableNameSize];
    NdbBlob::getBlobTableName(btname, &t, &c);
    BaseString btname_internal = m_ndb.internalize_table_name(btname);
    NdbTableImpl* bt =
      m_receiver.getTable(btname_internal, m_ndb.usingFullyQualifiedNames());
    if (bt == NULL)
      DBUG_RETURN(-1);

    // TODO check primary id/version when returned by DICT

    // the blob column owns the blob table
    assert(c.m_blobTable == NULL);
    c.m_blobTable = bt;
  }
  DBUG_RETURN(0); 
}

1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
NdbTableImpl*
NdbDictionaryImpl::getBlobTable(const NdbTableImpl& tab, uint col_no)
{
  if (col_no < tab.m_columns.size()) {
    NdbColumnImpl* col = tab.m_columns[col_no];
    if (col != NULL) {
      NdbTableImpl* bt = col->m_blobTable;
      if (bt != NULL)
        return bt;
      else
        m_error.code = 4273; // No blob table..
    } else
      m_error.code = 4249; // Invalid table..
  } else
    m_error.code = 4318; // Invalid attribute..
  return NULL;
}

NdbTableImpl*
NdbDictionaryImpl::getBlobTable(uint tab_id, uint col_no)
{
  DBUG_ENTER("NdbDictionaryImpl::getBlobTable");
  DBUG_PRINT("enter", ("tab_id: %u col_no %u", tab_id, col_no));

  NdbTableImpl* tab = m_receiver.getTable(tab_id,
                                          m_ndb.usingFullyQualifiedNames());
  if (tab == NULL)
    DBUG_RETURN(NULL);
  Ndb_local_table_info* info =
    get_local_table_info(tab->m_internalName);
  delete tab;
  if (info == NULL)
    DBUG_RETURN(NULL);
  NdbTableImpl* bt = getBlobTable(*info->m_table_impl, col_no);
  DBUG_RETURN(bt);
}

joreland@mysql.com's avatar
joreland@mysql.com committed
1518
#if 0
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
bool
NdbDictionaryImpl::setTransporter(class TransporterFacade * tf)
{
  if(tf != 0){
    m_globalHash = &tf->m_globalDictCache;
    return m_receiver.setTransporter(tf);
  }
  
  return false;
}
joreland@mysql.com's avatar
joreland@mysql.com committed
1529
#endif
1530 1531 1532 1533 1534 1535

bool
NdbDictionaryImpl::setTransporter(class Ndb* ndb, 
				  class TransporterFacade * tf)
{
  m_globalHash = &tf->m_globalDictCache;
joreland@mysql.com's avatar
joreland@mysql.com committed
1536
  if(m_receiver.setTransporter(ndb, tf)){
1537
    return true;
joreland@mysql.com's avatar
joreland@mysql.com committed
1538
  }
1539
  return false;
1540 1541
}

1542 1543
NdbTableImpl *
NdbDictionaryImpl::getIndexTable(NdbIndexImpl * index,
1544 1545
				 NdbTableImpl * table)
{
1546 1547
  const char *current_db= m_ndb.getDatabaseName();
  NdbTableImpl *index_table;
1548 1549
  const BaseString internalName(
    m_ndb.internalize_index_name(table, index->getName()));
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
  // Get index table in system database
  m_ndb.setDatabaseName(NDB_SYSTEM_DATABASE);
  index_table= getTable(m_ndb.externalizeTableName(internalName.c_str()));
  m_ndb.setDatabaseName(current_db);
  if (!index_table)
  {
    // Index table not found
    // Try geting index table in current database (old format)
    index_table= getTable(m_ndb.externalizeTableName(internalName.c_str()));    
  }
  return index_table;
1561 1562
}

1563
#if 0
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
bool
NdbDictInterface::setTransporter(class TransporterFacade * tf)
{
  if(tf == 0)
    return false;
  
  Guard g(tf->theMutexPtr);
  
  m_blockNumber = tf->open(this,
			   execSignal,
			   execNodeStatus);
  
  if ( m_blockNumber == -1 ) {
1577
    m_error.code= 4105;
1578 1579 1580 1581 1582 1583 1584 1585 1586
    return false; // no more free blocknumbers
  }//if
  Uint32 theNode = tf->ownId();
  m_reference = numberToRef(m_blockNumber, theNode);
  m_transporter = tf;
  m_waiter.m_mutex = tf->theMutexPtr;

  return true;
}
1587
#endif
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 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 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674

bool
NdbDictInterface::setTransporter(class Ndb* ndb, class TransporterFacade * tf)
{
  m_reference = ndb->getReference();
  m_transporter = tf;
  m_waiter.m_mutex = tf->theMutexPtr;
  
  return true;
}

NdbDictInterface::~NdbDictInterface()
{
}

void 
NdbDictInterface::execSignal(void* dictImpl, 
			     class NdbApiSignal* signal, 
			     class LinearSectionPtr ptr[3])
{
  NdbDictInterface * tmp = (NdbDictInterface*)dictImpl;
  
  const Uint32 gsn = signal->readSignalNumber();
  switch(gsn){
  case GSN_GET_TABINFOREF:
    tmp->execGET_TABINFO_REF(signal, ptr);
    break;
  case GSN_GET_TABINFO_CONF:
    tmp->execGET_TABINFO_CONF(signal, ptr);
    break;
  case GSN_CREATE_TABLE_REF:
    tmp->execCREATE_TABLE_REF(signal, ptr);
    break;
  case GSN_CREATE_TABLE_CONF:
    tmp->execCREATE_TABLE_CONF(signal, ptr);
    break;
  case GSN_DROP_TABLE_REF:
    tmp->execDROP_TABLE_REF(signal, ptr);
    break;
  case GSN_DROP_TABLE_CONF:
    tmp->execDROP_TABLE_CONF(signal, ptr);
    break;
  case GSN_ALTER_TABLE_REF:
    tmp->execALTER_TABLE_REF(signal, ptr);
    break;
  case GSN_ALTER_TABLE_CONF:
    tmp->execALTER_TABLE_CONF(signal, ptr);
    break;
  case GSN_CREATE_INDX_REF:
    tmp->execCREATE_INDX_REF(signal, ptr);
    break;
  case GSN_CREATE_INDX_CONF:
    tmp->execCREATE_INDX_CONF(signal, ptr);
    break;
  case GSN_DROP_INDX_REF:
    tmp->execDROP_INDX_REF(signal, ptr);
    break;
  case GSN_DROP_INDX_CONF:
    tmp->execDROP_INDX_CONF(signal, ptr);
    break;
  case GSN_CREATE_EVNT_REF:
    tmp->execCREATE_EVNT_REF(signal, ptr);
    break;
  case GSN_CREATE_EVNT_CONF:
    tmp->execCREATE_EVNT_CONF(signal, ptr);
    break;
  case GSN_SUB_START_CONF:
    tmp->execSUB_START_CONF(signal, ptr);
    break;
  case GSN_SUB_START_REF:
    tmp->execSUB_START_REF(signal, ptr);
    break;
  case GSN_SUB_STOP_CONF:
    tmp->execSUB_STOP_CONF(signal, ptr);
    break;
  case GSN_SUB_STOP_REF:
    tmp->execSUB_STOP_REF(signal, ptr);
    break;
  case GSN_DROP_EVNT_REF:
    tmp->execDROP_EVNT_REF(signal, ptr);
    break;
  case GSN_DROP_EVNT_CONF:
    tmp->execDROP_EVNT_CONF(signal, ptr);
    break;
  case GSN_LIST_TABLES_CONF:
    tmp->execLIST_TABLES_CONF(signal, ptr);
    break;
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698
  case GSN_CREATE_FILEGROUP_REF:
    tmp->execCREATE_FILEGROUP_REF(signal, ptr);
    break;
  case GSN_CREATE_FILEGROUP_CONF:
    tmp->execCREATE_FILEGROUP_CONF(signal, ptr);
    break;
  case GSN_CREATE_FILE_REF:
    tmp->execCREATE_FILE_REF(signal, ptr);
    break;
  case GSN_CREATE_FILE_CONF:
    tmp->execCREATE_FILE_CONF(signal, ptr);
    break;
  case GSN_DROP_FILEGROUP_REF:
    tmp->execDROP_FILEGROUP_REF(signal, ptr);
    break;
  case GSN_DROP_FILEGROUP_CONF:
    tmp->execDROP_FILEGROUP_CONF(signal, ptr);
    break;
  case GSN_DROP_FILE_REF:
    tmp->execDROP_FILE_REF(signal, ptr);
    break;
  case GSN_DROP_FILE_CONF:
    tmp->execDROP_FILE_CONF(signal, ptr);
    break;
1699 1700 1701 1702 1703 1704
  case GSN_WAIT_GCP_CONF:
    tmp->execWAIT_GCP_CONF(signal, ptr);
    break;
  case GSN_WAIT_GCP_REF:
    tmp->execWAIT_GCP_REF(signal, ptr);
    break;
1705 1706 1707 1708 1709 1710
  default:
    abort();
  }
}

void
1711
NdbDictInterface::execNodeStatus(void* dictImpl, Uint32 aNode,
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
				 bool alive, bool nfCompleted)
{
  NdbDictInterface * tmp = (NdbDictInterface*)dictImpl;
  
  if(!alive && !nfCompleted){
    return;
  }
  
  if (!alive && nfCompleted){
    tmp->m_waiter.nodeFail(aNode);
  }
}

int
1726 1727 1728 1729 1730 1731
NdbDictInterface::dictSignal(NdbApiSignal* sig, 
			     LinearSectionPtr ptr[3], int secs,
			     int node_specification,
			     WaitSignalType wst,
			     int timeout, Uint32 RETRIES,
			     const int *errcodes, int temporaryMask)
1732
{
magnus@neptunus.(none)'s avatar
magnus@neptunus.(none) committed
1733
  DBUG_ENTER("NdbDictInterface::dictSignal");
1734
  DBUG_PRINT("enter", ("useMasterNodeId: %d", node_specification));
1735 1736 1737 1738
  for(Uint32 i = 0; i<RETRIES; i++){
    m_buffer.clear();

    // Protected area
mronstrom@mysql.com[mikron]'s avatar
mronstrom@mysql.com[mikron] committed
1739 1740 1741 1742 1743 1744 1745
    /*
      The PollGuard has an implicit call of unlock_and_signal through the
      ~PollGuard method. This method is called implicitly by the compiler
      in all places where the object is out of context due to a return,
      break, continue or simply end of statement block
    */
    PollGuard poll_guard(m_transporter, &m_waiter, refToBlock(m_reference));
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
    Uint32 node;
    switch(node_specification){
    case 0:
      node = (m_transporter->get_node_alive(m_masterNodeId) ? m_masterNodeId :
	      (m_masterNodeId = m_transporter->get_an_alive_node()));
      break;
    case -1:
      node = m_transporter->get_an_alive_node();
      break;
    default:
      node = node_specification;
1757
    }
1758 1759
    DBUG_PRINT("info", ("node %d", node));
    if(node == 0){
1760
      m_error.code= 4009;
magnus@neptunus.(none)'s avatar
magnus@neptunus.(none) committed
1761
      DBUG_RETURN(-1);
1762
    }
1763 1764 1765 1766 1767
    int res = (ptr ? 
	       m_transporter->sendFragmentedSignal(sig, node, ptr, secs):
	       m_transporter->sendSignal(sig, node));
    if(res != 0){
      DBUG_PRINT("info", ("dictSignal failed to send signal"));
1768
      m_error.code = 4007;
1769 1770
      continue;
    }    
1771
    
1772
    m_error.code= 0;
1773
    int ret_val= poll_guard.wait_n_unlock(timeout, node, wst);
1774 1775
    // End of Protected area  
    
mronstrom@mysql.com[mikron]'s avatar
mronstrom@mysql.com[mikron] committed
1776
    if(ret_val == 0 && m_error.code == 0){
1777
      // Normal return
magnus@neptunus.(none)'s avatar
magnus@neptunus.(none) committed
1778
      DBUG_RETURN(0);
1779 1780 1781 1782 1783
    }
    
    /**
     * Handle error codes
     */
mronstrom@mysql.com[mikron]'s avatar
mronstrom@mysql.com[mikron] committed
1784
    if(ret_val == -2) //WAIT_NODE_FAILURE
1785
    {
1786
      m_error.code = 4013;
1787
      continue;
1788
    }
1789 1790
    if(m_waiter.m_state == WST_WAIT_TIMEOUT)
    {
1791
      DBUG_PRINT("info", ("dictSignal caught time-out"));
1792 1793 1794 1795
      m_error.code = 4008;
      DBUG_RETURN(-1);
    }
    
1796 1797 1798 1799 1800 1801 1802
    if ( temporaryMask == -1)
    {
      const NdbError &error= getNdbError();
      if (error.status ==  NdbError::TemporaryError)
	continue;
    }
    else if ( (temporaryMask & m_error.code) != 0 ) {
1803 1804
      continue;
    }
1805 1806 1807 1808
    DBUG_PRINT("info", ("dictSignal caught error= %d", m_error.code));
    
    if(m_error.code && errcodes)
    {
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
1809 1810
      int j;
      for(j = 0; errcodes[j] ; j++){
1811
	if(m_error.code == errcodes[j]){
1812
	  break;
1813
	}
1814
      }
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
1815
      if(errcodes[j]) // Accepted error code
1816 1817
	continue;
    }
1818
    break;
1819
  }
magnus@neptunus.(none)'s avatar
magnus@neptunus.(none) committed
1820
  DBUG_RETURN(-1);
1821
}
1822

msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
1823 1824 1825 1826 1827
/*
  Get dictionary information for a table using table id as reference

  DESCRIPTION
    Sends a GET_TABINFOREQ signal containing the table id
1828
 */
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
1829
NdbTableImpl *
1830
NdbDictInterface::getTable(int tableId, bool fullyQualifiedNames)
1831 1832
{
  NdbApiSignal tSignal(m_reference);
1833 1834
  GetTabInfoReq * req = CAST_PTR(GetTabInfoReq, tSignal.getDataPtrSend());
  
1835 1836
  req->senderRef = m_reference;
  req->senderData = 0;
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
1837
  req->requestType =
1838 1839 1840 1841 1842
    GetTabInfoReq::RequestById | GetTabInfoReq::LongSignalConf;
  req->tableId = tableId;
  tSignal.theReceiversBlockNumber = DBDICT;
  tSignal.theVerId_signalNumber   = GSN_GET_TABINFOREQ;
  tSignal.theLength = GetTabInfoReq::SignalLength;
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
1843

1844
  return getTable(&tSignal, 0, 0, fullyQualifiedNames);
1845
}
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
1846 1847 1848 1849 1850 1851 1852 1853

/*
  Get dictionary information for a table using table name as the reference

  DESCRIPTION
    Send GET_TABINFOREQ signal with the table name in the first
    long section part
*/
1854

msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
1855
NdbTableImpl *
1856
NdbDictInterface::getTable(const BaseString& name, bool fullyQualifiedNames)
1857 1858
{
  NdbApiSignal tSignal(m_reference);
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
1859
  GetTabInfoReq* const req = CAST_PTR(GetTabInfoReq, tSignal.getDataPtrSend());
1860

1861 1862
  const Uint32 namelen= name.length() + 1; // NULL terminated
  const Uint32 namelen_words= (namelen + 3) >> 2; // Size in words
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
1863 1864 1865 1866

  req->senderRef= m_reference;
  req->senderData= 0;
  req->requestType=
1867
    GetTabInfoReq::RequestByName | GetTabInfoReq::LongSignalConf;
1868
  req->tableNameLen= namelen;
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
1869 1870 1871 1872
  tSignal.theReceiversBlockNumber= DBDICT;
  tSignal.theVerId_signalNumber= GSN_GET_TABINFOREQ;
  tSignal.theLength= GetTabInfoReq::SignalLength;

1873 1874
  // Copy name to m_buffer to get a word sized buffer
  m_buffer.clear();
1875
  m_buffer.grow(namelen_words*4+4);
1876 1877
  m_buffer.append(name.c_str(), namelen);

1878 1879 1880 1881 1882
#ifndef IGNORE_VALGRIND_WARNINGS
  Uint32 pad = 0;
  m_buffer.append(&pad, 4);
#endif
  
1883
  LinearSectionPtr ptr[1];
1884 1885
  ptr[0].p= (Uint32*)m_buffer.get_data();
  ptr[0].sz= namelen_words;
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
1886

1887
  return getTable(&tSignal, ptr, 1, fullyQualifiedNames);
1888 1889
}

msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
1890

1891
NdbTableImpl *
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
1892
NdbDictInterface::getTable(class NdbApiSignal * signal,
1893
			   LinearSectionPtr ptr[3],
1894
			   Uint32 noOfSections, bool fullyQualifiedNames)
1895
{
1896 1897 1898
  int errCodes[] = {GetTabInfoRef::Busy, 0 };
  int r = dictSignal(signal, ptr, noOfSections,
		     -1, // any node
1899
		     WAIT_GET_TAB_INFO_REQ,
1900
		     DICT_WAITFOR_TIMEOUT, 100, errCodes);
1901

1902 1903 1904
  if (r)
    return 0;
  
1905
  NdbTableImpl * rt = 0;
1906 1907 1908 1909 1910 1911 1912
  m_error.code = parseTableInfo(&rt, 
				(Uint32*)m_buffer.get_data(), 
  				m_buffer.length() / 4, 
				fullyQualifiedNames);
  if(rt)
    rt->buildColumnHash();
  
1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
  return rt;
}

void
NdbDictInterface::execGET_TABINFO_CONF(NdbApiSignal * signal, 
				       LinearSectionPtr ptr[3])
{
  const GetTabInfoConf* conf = CAST_CONSTPTR(GetTabInfoConf, signal->getDataPtr());
  if(signal->isFirstFragment()){
    m_fragmentId = signal->getFragmentId();
    m_buffer.grow(4 * conf->totalLen);
  } else {
    if(m_fragmentId != signal->getFragmentId()){
      abort();
    }
  }
  
  const Uint32 i = GetTabInfoConf::DICT_TAB_INFO;
  m_buffer.append(ptr[i].p, 4 * ptr[i].sz);

  if(!signal->isLastFragment()){
    return;
  }  
  
  m_waiter.signal(NO_WAIT);
}

void
NdbDictInterface::execGET_TABINFO_REF(NdbApiSignal * signal,
				      LinearSectionPtr ptr[3])
{
1944 1945 1946
  const GetTabInfoRef* ref = CAST_CONSTPTR(GetTabInfoRef, 
					   signal->getDataPtr());
  
1947
  m_error.code= ref->errorCode;
1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993
  m_waiter.signal(NO_WAIT);
}

/*****************************************************************
 * Pack/Unpack tables
 */
struct ApiKernelMapping {
  Int32 kernelConstant;
  Int32 apiConstant;
};

Uint32
getApiConstant(Int32 kernelConstant, const ApiKernelMapping map[], Uint32 def)
{
  int i = 0;
  while(map[i].kernelConstant != kernelConstant){
    if(map[i].kernelConstant == -1 &&
       map[i].apiConstant == -1){
      return def;
    }
    i++;
  }
  return map[i].apiConstant;
}

Uint32
getKernelConstant(Int32 apiConstant, const ApiKernelMapping map[], Uint32 def)
{
  int i = 0;
  while(map[i].apiConstant != apiConstant){
    if(map[i].kernelConstant == -1 &&
       map[i].apiConstant == -1){
      return def;
    }
    i++;
  }
  return map[i].kernelConstant;
}

static const
ApiKernelMapping 
fragmentTypeMapping[] = {
  { DictTabInfo::AllNodesSmallTable,  NdbDictionary::Object::FragAllSmall },
  { DictTabInfo::AllNodesMediumTable, NdbDictionary::Object::FragAllMedium },
  { DictTabInfo::AllNodesLargeTable,  NdbDictionary::Object::FragAllLarge },
  { DictTabInfo::SingleFragment,      NdbDictionary::Object::FragSingle },
1994 1995 1996
  { DictTabInfo::DistrKeyHash,      NdbDictionary::Object::DistrKeyHash },
  { DictTabInfo::DistrKeyLin,      NdbDictionary::Object::DistrKeyLin },
  { DictTabInfo::UserDefined,      NdbDictionary::Object::UserDefined },
1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
  { -1, -1 }
};

static const
ApiKernelMapping
objectTypeMapping[] = {
  { DictTabInfo::SystemTable,        NdbDictionary::Object::SystemTable },
  { DictTabInfo::UserTable,          NdbDictionary::Object::UserTable },
  { DictTabInfo::UniqueHashIndex,    NdbDictionary::Object::UniqueHashIndex },
  { DictTabInfo::OrderedIndex,       NdbDictionary::Object::OrderedIndex },
  { DictTabInfo::HashIndexTrigger,   NdbDictionary::Object::HashIndexTrigger },
  { DictTabInfo::IndexTrigger,       NdbDictionary::Object::IndexTrigger },
  { DictTabInfo::SubscriptionTrigger,NdbDictionary::Object::SubscriptionTrigger },
  { DictTabInfo::ReadOnlyConstraint ,NdbDictionary::Object::ReadOnlyConstraint },
2011 2012 2013 2014
  { DictTabInfo::Tablespace,         NdbDictionary::Object::Tablespace },
  { DictTabInfo::LogfileGroup,       NdbDictionary::Object::LogfileGroup },
  { DictTabInfo::Datafile,           NdbDictionary::Object::Datafile },
  { DictTabInfo::Undofile,           NdbDictionary::Object::Undofile },
2015 2016 2017 2018 2019 2020 2021 2022 2023 2024
  { -1, -1 }
};

static const
ApiKernelMapping
objectStateMapping[] = {
  { DictTabInfo::StateOffline,       NdbDictionary::Object::StateOffline },
  { DictTabInfo::StateBuilding,      NdbDictionary::Object::StateBuilding },
  { DictTabInfo::StateDropping,      NdbDictionary::Object::StateDropping },
  { DictTabInfo::StateOnline,        NdbDictionary::Object::StateOnline },
joreland@mysql.com's avatar
joreland@mysql.com committed
2025
  { DictTabInfo::StateBackup,        NdbDictionary::Object::StateBackup },
2026 2027 2028 2029 2030 2031 2032
  { DictTabInfo::StateBroken,        NdbDictionary::Object::StateBroken }, 
  { -1, -1 }
};

static const
ApiKernelMapping
objectStoreMapping[] = {
knielsen@ymer.(none)'s avatar
knielsen@ymer.(none) committed
2033
  { DictTabInfo::StoreNotLogged,     NdbDictionary::Object::StoreNotLogged },
2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047
  { DictTabInfo::StorePermanent,     NdbDictionary::Object::StorePermanent },
  { -1, -1 }
};

static const
ApiKernelMapping
indexTypeMapping[] = {
  { DictTabInfo::UniqueHashIndex,    NdbDictionary::Index::UniqueHashIndex },  
  { DictTabInfo::OrderedIndex,       NdbDictionary::Index::OrderedIndex },
  { -1, -1 }
};

int
NdbDictInterface::parseTableInfo(NdbTableImpl ** ret,
2048
				 const Uint32 * data, Uint32 len,
2049 2050
				 bool fullyQualifiedNames,
                                 Uint32 version)
2051 2052
{
  SimplePropertiesLinearReader it(data, len);
2053
  DictTabInfo::Table *tableDesc;
2054
  SimpleProperties::UnpackStatus s;
2055 2056 2057 2058 2059 2060 2061 2062 2063
  DBUG_ENTER("NdbDictInterface::parseTableInfo");

  tableDesc = (DictTabInfo::Table*)NdbMem_Allocate(sizeof(DictTabInfo::Table));
  if (!tableDesc)
  {
    DBUG_RETURN(4000);
  }
  tableDesc->init();
  s = SimpleProperties::unpack(it, tableDesc, 
2064 2065 2066 2067 2068
			       DictTabInfo::TableMapping, 
			       DictTabInfo::TableMappingSize, 
			       true, true);
  
  if(s != SimpleProperties::Break){
2069
    NdbMem_Free((void*)tableDesc);
pekka@mysql.com's avatar
pekka@mysql.com committed
2070
    DBUG_RETURN(703);
2071
  }
2072
  const char * internalName = tableDesc->TableName;
2073
  const char * externalName = Ndb::externalizeTableName(internalName, fullyQualifiedNames);
2074 2075

  NdbTableImpl * impl = new NdbTableImpl();
2076 2077
  impl->m_id = tableDesc->TableId;
  impl->m_version = tableDesc->TableVersion;
2078 2079
  impl->m_status = NdbDictionary::Object::Retrieved;
  impl->m_internalName.assign(internalName);
2080
  impl->updateMysqlName();
2081 2082
  impl->m_externalName.assign(externalName);

2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
  impl->m_frm.assign(tableDesc->FrmData, tableDesc->FrmLen);
  impl->m_fd.assign(tableDesc->FragmentData, tableDesc->FragmentDataLen);
  impl->m_range.assign(tableDesc->RangeListData, tableDesc->RangeListDataLen);
  impl->m_fragmentCount = tableDesc->FragmentCount;

  /*
    We specifically don't get tablespace data and range/list arrays here
    since those are known by the MySQL Server through analysing the
    frm file.
    Fragment Data contains the real node group mapping and the fragment
    identities used for each fragment. At the moment we have no need for
    this.
    Frm file is needed for autodiscovery.
  */
2097 2098
  
  impl->m_fragmentType = (NdbDictionary::Object::FragmentType)
2099
    getApiConstant(tableDesc->FragmentType, 
2100 2101 2102
		   fragmentTypeMapping, 
		   (Uint32)NdbDictionary::Object::FragUndefined);
  
2103 2104
  Uint64 max_rows = ((Uint64)tableDesc->MaxRowsHigh) << 32;
  max_rows += tableDesc->MaxRowsLow;
2105
  impl->m_max_rows = max_rows;
2106 2107
  Uint64 min_rows = ((Uint64)tableDesc->MinRowsHigh) << 32;
  min_rows += tableDesc->MinRowsLow;
2108
  impl->m_min_rows = min_rows;
2109
  impl->m_default_no_part_flag = tableDesc->DefaultNoPartFlag;
2110
  impl->m_linear_flag = tableDesc->LinearHashFlag;
2111
  impl->m_logging = tableDesc->TableLoggedFlag;
knielsen@ymer.(none)'s avatar
knielsen@ymer.(none) committed
2112
  impl->m_temporary = tableDesc->TableTemporaryFlag;
2113 2114 2115 2116 2117
  impl->m_row_gci = tableDesc->RowGCIFlag;
  impl->m_row_checksum = tableDesc->RowChecksumFlag;
  impl->m_kvalue = tableDesc->TableKValue;
  impl->m_minLoadFactor = tableDesc->MinLoadFactor;
  impl->m_maxLoadFactor = tableDesc->MaxLoadFactor;
2118
  impl->m_single_user_mode = tableDesc->SingleUserMode;
2119

2120
  impl->m_indexType = (NdbDictionary::Object::Type)
2121
    getApiConstant(tableDesc->TableType,
2122
		   indexTypeMapping,
2123
		   NdbDictionary::Object::TypeUndefined);
2124
  
2125
  if(impl->m_indexType == NdbDictionary::Object::TypeUndefined){
2126 2127
  } else {
    const char * externalPrimary = 
2128
      Ndb::externalizeTableName(tableDesc->PrimaryTable, fullyQualifiedNames);
2129 2130 2131
    impl->m_primaryTable.assign(externalPrimary);
  }
  
2132
  Uint32 i;
2133
  for(i = 0; i < tableDesc->NoOfAttributes; i++) {
2134 2135 2136 2137 2138 2139 2140 2141
    DictTabInfo::Attribute attrDesc; attrDesc.init();
    s = SimpleProperties::unpack(it, 
				 &attrDesc, 
				 DictTabInfo::AttributeMapping, 
				 DictTabInfo::AttributeMappingSize, 
				 true, true);
    if(s != SimpleProperties::Break){
      delete impl;
2142
      NdbMem_Free((void*)tableDesc);
pekka@mysql.com's avatar
pekka@mysql.com committed
2143
      DBUG_RETURN(703);
2144 2145 2146 2147 2148
    }
    
    NdbColumnImpl * col = new NdbColumnImpl();
    col->m_attrId = attrDesc.AttributeId;
    col->setName(attrDesc.AttributeName);
pekka@mysql.com's avatar
pekka@mysql.com committed
2149 2150 2151

    // check type and compute attribute size and array size
    if (! attrDesc.translateExtType()) {
2152
      delete impl;
2153
      NdbMem_Free((void*)tableDesc);
pekka@mysql.com's avatar
pekka@mysql.com committed
2154
      DBUG_RETURN(703);
2155
    }
2156
    col->m_type = (NdbDictionary::Column::Type)attrDesc.AttributeExtType;
pekka@mysql.com's avatar
pekka@mysql.com committed
2157
    col->m_precision = (attrDesc.AttributeExtPrecision & 0xFFFF);
2158 2159
    col->m_scale = attrDesc.AttributeExtScale;
    col->m_length = attrDesc.AttributeExtLength;
pekka@mysql.com's avatar
pekka@mysql.com committed
2160 2161 2162 2163 2164
    // charset in upper half of precision
    unsigned cs_number = (attrDesc.AttributeExtPrecision >> 16);
    // charset is defined exactly for char types
    if (col->getCharType() != (cs_number != 0)) {
      delete impl;
2165
      NdbMem_Free((void*)tableDesc);
pekka@mysql.com's avatar
pekka@mysql.com committed
2166
      DBUG_RETURN(703);
pekka@mysql.com's avatar
pekka@mysql.com committed
2167 2168 2169 2170 2171
    }
    if (col->getCharType()) {
      col->m_cs = get_charset(cs_number, MYF(0));
      if (col->m_cs == NULL) {
        delete impl;
2172
        NdbMem_Free((void*)tableDesc);
pekka@mysql.com's avatar
pekka@mysql.com committed
2173
        DBUG_RETURN(743);
pekka@mysql.com's avatar
pekka@mysql.com committed
2174 2175
      }
    }
2176 2177
    col->m_attrSize = (1 << attrDesc.AttributeSize) / 8;
    col->m_arraySize = attrDesc.AttributeArraySize;
2178
    col->m_arrayType = attrDesc.AttributeArrayType;
2179 2180 2181 2182 2183
    if(attrDesc.AttributeSize == 0)
    {
      col->m_attrSize = 4;
      col->m_arraySize = (attrDesc.AttributeArraySize + 31) >> 5;
    }
2184
    col->m_storageType = attrDesc.AttributeStorageType;
2185 2186
    
    col->m_pk = attrDesc.AttributeKeyFlag;
2187
    col->m_distributionKey = (attrDesc.AttributeDKey != 0);
2188
    col->m_nullable = attrDesc.AttributeNullableFlag;
2189
    col->m_autoIncrement = (attrDesc.AttributeAutoIncrement != 0);
2190 2191 2192
    col->m_autoIncrementInitialValue = ~0;
    col->m_defaultValue.assign(attrDesc.AttributeDefaultValue);

2193 2194
    col->m_column_no = impl->m_columns.size();
    impl->m_columns.push_back(col);
2195 2196
    it.next();
  }
joreland@mysql.com's avatar
joreland@mysql.com committed
2197

2198
  impl->computeAggregates();
2199

2200
  if(tableDesc->ReplicaDataLen > 0)
2201
  {
2202 2203
    Uint16 replicaCount = ntohs(tableDesc->ReplicaData[0]);
    Uint16 fragCount = ntohs(tableDesc->ReplicaData[1]);
2204

2205 2206
    impl->m_replicaCount = replicaCount;
    impl->m_fragmentCount = fragCount;
2207
    DBUG_PRINT("info", ("replicaCount=%x , fragCount=%x",replicaCount,fragCount));
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
2208 2209
    Uint32 pos = 2;
    for(i = 0; i < (Uint32) fragCount;i++)
2210
    {
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
2211 2212 2213 2214 2215
      pos++; // skip logpart
      for (Uint32 j = 0; j<(Uint32)replicaCount; j++)
      {
	impl->m_fragments.push_back(ntohs(tableDesc->ReplicaData[pos++]));
      }
2216 2217 2218
    }

    Uint32 topBit = (1 << 31);
2219
    for(; topBit && !(fragCount & topBit); ){
2220 2221 2222 2223 2224 2225 2226
      topBit >>= 1;
    }
    impl->m_hashValueMask = topBit - 1;
    impl->m_hashpointerValue = fragCount - (impl->m_hashValueMask + 1);
  }
  else
  {
2227
    impl->m_fragmentCount = tableDesc->FragmentCount;
2228 2229 2230 2231 2232
    impl->m_replicaCount = 0;
    impl->m_hashValueMask = 0;
    impl->m_hashpointerValue = 0;
  }

2233 2234
  impl->m_tablespace_id = tableDesc->TablespaceId;
  impl->m_tablespace_version = tableDesc->TablespaceVersion;
2235
  
2236
  * ret = impl;
2237

2238
  NdbMem_Free((void*)tableDesc);
2239 2240 2241 2242 2243 2244 2245 2246
  if (version < MAKE_VERSION(5,1,3))
  {
    ;
  } 
  else
  {
    DBUG_ASSERT(impl->m_fragmentCount > 0);
  }
pekka@mysql.com's avatar
pekka@mysql.com committed
2247
  DBUG_RETURN(0);
2248 2249 2250 2251 2252
}

/*****************************************************************
 * Create table and alter table
 */
pekka@mysql.com's avatar
pekka@mysql.com committed
2253 2254 2255
int
NdbDictionaryImpl::createTable(NdbTableImpl &t)
{ 
2256
  DBUG_ENTER("NdbDictionaryImpl::createTable");
2257 2258

  // if the new name has not been set, use the copied name
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
2259 2260
  if (t.m_newExternalName.empty())
    t.m_newExternalName.assign(t.m_externalName);
2261 2262

  // create table
pekka@mysql.com's avatar
pekka@mysql.com committed
2263
  if (m_receiver.createTable(m_ndb, t) != 0)
2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277
    DBUG_RETURN(-1);
  Uint32* data = (Uint32*)m_receiver.m_buffer.get_data();
  t.m_id = data[0];
  t.m_version = data[1];

  // update table def from DICT - by-pass cache
  NdbTableImpl* t2 =
    m_receiver.getTable(t.m_internalName, m_ndb.usingFullyQualifiedNames());

  // check if we got back same table
  if (t2 == NULL) {
    DBUG_PRINT("info", ("table %s dropped by another thread", 
                        t.m_internalName.c_str()));
    m_error.code = 283;
2278 2279
    DBUG_RETURN(-1);
  }
2280 2281 2282 2283 2284
  if (t.m_id != t2->m_id || t.m_version != t2->m_version) {
    DBUG_PRINT("info", ("table %s re-created by another thread",
                        t.m_internalName.c_str()));
    m_error.code = 283;
    delete t2;
2285
    DBUG_RETURN(-1);
pekka@mysql.com's avatar
pekka@mysql.com committed
2286
  }
2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306

  // auto-increment - use "t" because initial value is not in DICT
  {
    bool autoIncrement = false;
    Uint64 initialValue = 0;
    for (Uint32 i = 0; i < t.m_columns.size(); i++) {
      const NdbColumnImpl* c = t.m_columns[i];
      assert(c != NULL);
      if (c->m_autoIncrement) {
        if (autoIncrement) {
          m_error.code = 4335;
          delete t2;
          DBUG_RETURN(-1);
        }
        autoIncrement = true;
        initialValue = c->m_autoIncrementInitialValue;
      }
    }
    if (autoIncrement) {
      // XXX unlikely race condition - t.m_id may no longer be same table
2307 2308 2309 2310 2311
      // the tuple id range is not used on input
      Ndb::TupleIdRange range;
      if (m_ndb.setTupleIdInNdb(&t, range, initialValue, false) == -1) {
        assert(m_ndb.theError.code != 0);
        m_error.code = m_ndb.theError.code;
2312 2313 2314 2315 2316 2317 2318
        delete t2;
        DBUG_RETURN(-1);
      }
    }
  }

  // blob tables - use "t2" to get values set by kernel
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
2319
  if (t2->m_noOfBlobs != 0 && createBlobTables(t, *t2) != 0) {
pekka@mysql.com's avatar
pekka@mysql.com committed
2320
    int save_code = m_error.code;
2321
    (void)dropTableGlobal(*t2);
2322 2323
    m_error.code = save_code;
    delete t2;
2324
    DBUG_RETURN(-1);
pekka@mysql.com's avatar
pekka@mysql.com committed
2325
  }
2326 2327 2328

  // not entered in cache
  delete t2;
2329
  DBUG_RETURN(0);
pekka@mysql.com's avatar
pekka@mysql.com committed
2330 2331 2332
}

int
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
2333
NdbDictionaryImpl::createBlobTables(NdbTableImpl& orig, NdbTableImpl &t)
pekka@mysql.com's avatar
pekka@mysql.com committed
2334
{
2335
  DBUG_ENTER("NdbDictionaryImpl::createBlobTables");
pekka@mysql.com's avatar
pekka@mysql.com committed
2336 2337
  for (unsigned i = 0; i < t.m_columns.size(); i++) {
    NdbColumnImpl & c = *t.m_columns[i];
pekka@mysql.com's avatar
pekka@mysql.com committed
2338
    if (! c.getBlobType() || c.getPartSize() == 0)
pekka@mysql.com's avatar
pekka@mysql.com committed
2339 2340 2341
      continue;
    NdbTableImpl bt;
    NdbBlob::getBlobTable(bt, &t, &c);
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
2342 2343 2344 2345
    NdbDictionary::Column::StorageType 
      d = NdbDictionary::Column::StorageTypeDisk;
    if (orig.m_columns[i]->getStorageType() == d)
      bt.getColumn("DATA")->setStorageType(d);
2346
    if (createTable(bt) != 0) {
2347 2348
      DBUG_RETURN(-1);
    }
pekka@mysql.com's avatar
pekka@mysql.com committed
2349
  }
2350
  DBUG_RETURN(0); 
mskold@mysql.com's avatar
mskold@mysql.com committed
2351 2352
}

2353 2354 2355 2356
int 
NdbDictInterface::createTable(Ndb & ndb,
			      NdbTableImpl & impl)
{
2357 2358
  DBUG_ENTER("NdbDictInterface::createTable");
  DBUG_RETURN(createOrAlterTable(ndb, impl, false));
2359 2360 2361 2362
}

int NdbDictionaryImpl::alterTable(NdbTableImpl &impl)
{
2363
  BaseString internalName(impl.m_internalName);
2364
  const char * originalInternalName = internalName.c_str();
2365 2366

  DBUG_ENTER("NdbDictionaryImpl::alterTable");
2367
  Ndb_local_table_info * local = 0;
2368
  if((local= get_local_table_info(originalInternalName)) == 0)
2369
  {
2370
    m_error.code = 709;
2371
    DBUG_RETURN(-1);
2372
  }
2373

2374
  // Alter the table
2375 2376 2377
  int ret = alterTableGlobal(*local->m_table_impl, impl);
  if(ret == 0)
  {
2378
    m_globalHash->lock();
2379
    m_globalHash->release(local->m_table_impl, 1);
2380 2381
    m_globalHash->unlock();
    m_localHash.drop(originalInternalName);
2382
  }
mskold@mysql.com's avatar
mskold@mysql.com committed
2383
  DBUG_RETURN(ret);
2384 2385
}

2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398
int NdbDictionaryImpl::alterTableGlobal(NdbTableImpl &old_impl,
                                        NdbTableImpl &impl)
{
  DBUG_ENTER("NdbDictionaryImpl::alterTableGlobal");
  // Alter the table
  int ret = m_receiver.alterTable(m_ndb, impl);
  old_impl.m_status = NdbDictionary::Object::Invalid;
  if(ret == 0){
    DBUG_RETURN(ret);
  }
  ERR_RETURN(getNdbError(), ret);
}

2399 2400 2401 2402
int 
NdbDictInterface::alterTable(Ndb & ndb,
			      NdbTableImpl & impl)
{
2403 2404
  DBUG_ENTER("NdbDictInterface::alterTable");
  DBUG_RETURN(createOrAlterTable(ndb, impl, true));
2405 2406 2407 2408 2409 2410 2411
}

int 
NdbDictInterface::createOrAlterTable(Ndb & ndb,
				     NdbTableImpl & impl,
				     bool alter)
{
2412
  unsigned i, err;
2413 2414
  char *ts_names[MAX_NDB_PARTITIONS];
  DBUG_ENTER("NdbDictInterface::createOrAlterTable");
2415 2416 2417

  impl.computeAggregates();

2418
  if((unsigned)impl.getNoOfPrimaryKeys() > NDB_MAX_NO_OF_ATTRIBUTES_IN_KEY){
2419
    m_error.code= 4317;
2420
    DBUG_RETURN(-1);
2421 2422 2423
  }
  unsigned sz = impl.m_columns.size();
  if (sz > NDB_MAX_ATTRIBUTES_IN_TABLE){
2424
    m_error.code= 4318;
2425
    DBUG_RETURN(-1);
2426
  }
2427 2428 2429 2430
  
  // Check if any changes for alter table
  
  // Name change
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
2431
  if (!impl.m_newExternalName.empty()) {
2432 2433 2434 2435
    if (alter)
    {
      AlterTableReq::setNameFlag(impl.m_changeMask, true);
    }
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
2436
    impl.m_externalName.assign(impl.m_newExternalName);
2437
    impl.m_newExternalName.clear();
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
2438
  }
2439
  // Definition change (frm)
2440 2441
  if (!impl.m_newFrm.empty())
  {
2442 2443 2444 2445 2446 2447 2448
    if (alter)
    {
      AlterTableReq::setFrmFlag(impl.m_changeMask, true);
    }
    impl.m_frm.assign(impl.m_newFrm.get_data(), impl.m_newFrm.length());
    impl.m_newFrm.clear();
  }
2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497
  // Change FragmentData (fragment identity, state, tablespace id)
  if (!impl.m_new_fd.empty())
  {
    if (alter)
    {
      AlterTableReq::setFragDataFlag(impl.m_changeMask, true);
    }
    impl.m_fd.assign(impl.m_new_fd.get_data(), impl.m_new_fd.length());
    impl.m_new_fd.clear();
  }
  // Change Tablespace Name Data
  if (!impl.m_new_ts_name.empty())
  {
    if (alter)
    {
      AlterTableReq::setTsNameFlag(impl.m_changeMask, true);
    }
    impl.m_ts_name.assign(impl.m_new_ts_name.get_data(),
                          impl.m_new_ts_name.length());
    impl.m_new_ts_name.clear();
  }
  // Change Range/List Data
  if (!impl.m_new_range.empty())
  {
    if (alter)
    {
      AlterTableReq::setRangeListFlag(impl.m_changeMask, true);
    }
    impl.m_range.assign(impl.m_new_range.get_data(),
                          impl.m_new_range.length());
    impl.m_new_range.clear();
  }
  // Change Tablespace Data
  if (!impl.m_new_ts.empty())
  {
    if (alter)
    {
      AlterTableReq::setTsFlag(impl.m_changeMask, true);
    }
    impl.m_ts.assign(impl.m_new_ts.get_data(),
                     impl.m_new_ts.length());
    impl.m_new_ts.clear();
  }


  /*
     TODO RONM: Here I need to insert checks for fragment array and
     range or list array
  */
2498
  
2499 2500 2501
  //validate();
  //aggregate();

2502 2503
  const BaseString internalName(
    ndb.internalize_table_name(impl.m_externalName.c_str()));
2504
  impl.m_internalName.assign(internalName);
2505
  impl.updateMysqlName();
2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516
  DictTabInfo::Table *tmpTab;

  tmpTab = (DictTabInfo::Table*)NdbMem_Allocate(sizeof(DictTabInfo::Table));
  if (!tmpTab)
  {
    m_error.code = 4000;
    DBUG_RETURN(-1);
  }
  tmpTab->init();
  BaseString::snprintf(tmpTab->TableName,
	   sizeof(tmpTab->TableName),
2517
	   internalName.c_str());
2518

2519
  Uint32 distKeys= 0;
2520
  for(i = 0; i<sz; i++) {
2521
    const NdbColumnImpl * col = impl.m_columns[i];
2522 2523 2524 2525
    if (col == NULL) {
      m_error.code = 4272;
      NdbMem_Free((void*)tmpTab);
      DBUG_RETURN(-1);
2526 2527
    }
    if (col->m_distributionKey)
2528
    {
2529
      distKeys++;
2530
    }
2531
  }
2532 2533 2534 2535
  if (distKeys == impl.m_noOfKeys)
    distKeys= 0;
  impl.m_noOfDistributionKeys= distKeys;

2536 2537 2538

  // Check max length of frm data
  if (impl.m_frm.length() > MAX_FRM_DATA_SIZE){
2539
    m_error.code= 1229;
2540
    NdbMem_Free((void*)tmpTab);
2541
    DBUG_RETURN(-1);
2542
  }
2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564
  /*
    TODO RONM: This needs to change to dynamic arrays instead
    Frm Data, FragmentData, TablespaceData, RangeListData, TsNameData
  */
  tmpTab->FrmLen = impl.m_frm.length();
  memcpy(tmpTab->FrmData, impl.m_frm.get_data(), impl.m_frm.length());

  tmpTab->FragmentDataLen = impl.m_fd.length();
  memcpy(tmpTab->FragmentData, impl.m_fd.get_data(), impl.m_fd.length());

  tmpTab->TablespaceDataLen = impl.m_ts.length();
  memcpy(tmpTab->TablespaceData, impl.m_ts.get_data(), impl.m_ts.length());

  tmpTab->RangeListDataLen = impl.m_range.length();
  memcpy(tmpTab->RangeListData, impl.m_range.get_data(),
         impl.m_range.length());

  memcpy(ts_names, impl.m_ts_name.get_data(),
         impl.m_ts_name.length());

  tmpTab->FragmentCount= impl.m_fragmentCount;
  tmpTab->TableLoggedFlag = impl.m_logging;
knielsen@ymer.(none)'s avatar
knielsen@ymer.(none) committed
2565
  tmpTab->TableTemporaryFlag = impl.m_temporary;
2566 2567 2568 2569 2570 2571 2572 2573
  tmpTab->RowGCIFlag = impl.m_row_gci;
  tmpTab->RowChecksumFlag = impl.m_row_checksum;
  tmpTab->TableKValue = impl.m_kvalue;
  tmpTab->MinLoadFactor = impl.m_minLoadFactor;
  tmpTab->MaxLoadFactor = impl.m_maxLoadFactor;
  tmpTab->TableType = DictTabInfo::UserTable;
  tmpTab->PrimaryTableId = impl.m_primaryTableId;
  tmpTab->NoOfAttributes = sz;
2574 2575 2576 2577
  tmpTab->MaxRowsHigh = (Uint32)(impl.m_max_rows >> 32);
  tmpTab->MaxRowsLow = (Uint32)(impl.m_max_rows & 0xFFFFFFFF);
  tmpTab->MinRowsHigh = (Uint32)(impl.m_min_rows >> 32);
  tmpTab->MinRowsLow = (Uint32)(impl.m_min_rows & 0xFFFFFFFF);
2578
  tmpTab->DefaultNoPartFlag = impl.m_default_no_part_flag;
2579
  tmpTab->LinearHashFlag = impl.m_linear_flag;
2580
  tmpTab->SingleUserMode = impl.m_single_user_mode;
2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614

  if (impl.m_ts_name.length())
  {
    char **ts_name_ptr= (char**)ts_names;
    i= 0;
    do
    {
      NdbTablespaceImpl tmp;
      if (*ts_name_ptr)
      {
        if(get_filegroup(tmp, NdbDictionary::Object::Tablespace, 
                         (const char*)*ts_name_ptr) == 0)
        {
          tmpTab->TablespaceData[2*i] = tmp.m_id;
          tmpTab->TablespaceData[2*i + 1] = tmp.m_version;
        }
        else
        { 
          NdbMem_Free((void*)tmpTab);
          DBUG_RETURN(-1);
        }
      }
      else
      {
        /*
          No tablespace used, set tablespace id to NULL
        */
        tmpTab->TablespaceData[2*i] = RNIL;
        tmpTab->TablespaceData[2*i + 1] = 0;
      }
      ts_name_ptr++;
    } while (++i < tmpTab->FragmentCount);
    tmpTab->TablespaceDataLen= 4*i;
  }
2615

2616 2617 2618 2619 2620 2621
  tmpTab->FragmentType = getKernelConstant(impl.m_fragmentType,
 					   fragmentTypeMapping,
					   DictTabInfo::AllNodesSmallTable);
  tmpTab->TableVersion = rand();

  const char *tablespace_name= impl.m_tablespace_name.c_str();
2622 2623 2624
loop:
  if(impl.m_tablespace_id != ~(Uint32)0)
  {
2625 2626
    tmpTab->TablespaceId = impl.m_tablespace_id;
    tmpTab->TablespaceVersion = impl.m_tablespace_version;
2627 2628 2629 2630 2631 2632 2633
  }
  else if(strlen(tablespace_name))
  {
    NdbTablespaceImpl tmp;
    if(get_filegroup(tmp, NdbDictionary::Object::Tablespace, 
		     tablespace_name) == 0)
    {
2634 2635
      tmpTab->TablespaceId = tmp.m_id;
      tmpTab->TablespaceVersion = tmp.m_version;
2636 2637 2638 2639
    }
    else 
    {
      // error set by get filegroup
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
2640 2641 2642
      if (m_error.code == 723)
	m_error.code = 755;
      
2643 2644
      NdbMem_Free((void*)tmpTab);
      DBUG_RETURN(-1);
2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659
    }
  } 
  else
  {
    for(i = 0; i<sz; i++)
    {
      if(impl.m_columns[i]->m_storageType == NDB_STORAGETYPE_DISK)
      {
	tablespace_name = "DEFAULT-TS";
	goto loop;
      }
    }
  }
  
  UtilBufferWriter w(m_buffer);
2660 2661
  SimpleProperties::UnpackStatus s;
  s = SimpleProperties::pack(w, 
2662
			     tmpTab,
2663 2664 2665 2666 2667 2668
			     DictTabInfo::TableMapping, 
			     DictTabInfo::TableMappingSize, true);
  
  if(s != SimpleProperties::Eof){
    abort();
  }
2669
  NdbMem_Free((void*)tmpTab);
2670
  
2671 2672
  DBUG_PRINT("info",("impl.m_noOfDistributionKeys: %d impl.m_noOfKeys: %d distKeys: %d",
		     impl.m_noOfDistributionKeys, impl.m_noOfKeys, distKeys));
2673 2674 2675
  if (distKeys == impl.m_noOfKeys)
    distKeys= 0;
  impl.m_noOfDistributionKeys= distKeys;
joreland@mysql.com's avatar
joreland@mysql.com committed
2676
  
2677
  for(i = 0; i<sz; i++){
2678 2679 2680 2681
    const NdbColumnImpl * col = impl.m_columns[i];
    if(col == 0)
      continue;
    
2682 2683
    DBUG_PRINT("info",("column: %s(%d) col->m_distributionKey: %d",
		       col->m_name.c_str(), i, col->m_distributionKey));
2684
    DictTabInfo::Attribute tmpAttr; tmpAttr.init();
2685
    BaseString::snprintf(tmpAttr.AttributeName, sizeof(tmpAttr.AttributeName), 
2686
	     col->m_name.c_str());
2687
    tmpAttr.AttributeId = col->m_attrId;
joreland@mysql.com's avatar
joreland@mysql.com committed
2688
    tmpAttr.AttributeKeyFlag = col->m_pk;
2689
    tmpAttr.AttributeNullableFlag = col->m_nullable;
2690
    tmpAttr.AttributeDKey = distKeys ? col->m_distributionKey : 0;
2691

2692
    tmpAttr.AttributeExtType = (Uint32)col->m_type;
pekka@mysql.com's avatar
pekka@mysql.com committed
2693
    tmpAttr.AttributeExtPrecision = ((unsigned)col->m_precision & 0xFFFF);
2694 2695
    tmpAttr.AttributeExtScale = col->m_scale;
    tmpAttr.AttributeExtLength = col->m_length;
2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708
    if(col->m_storageType == NDB_STORAGETYPE_DISK)
      tmpAttr.AttributeArrayType = NDB_ARRAYTYPE_FIXED;
    else
      tmpAttr.AttributeArrayType = col->m_arrayType;

    if(col->m_pk)
      tmpAttr.AttributeStorageType = NDB_STORAGETYPE_MEMORY;      
    else
      tmpAttr.AttributeStorageType = col->m_storageType;

    if(col->getBlobType())
      tmpAttr.AttributeStorageType = NDB_STORAGETYPE_MEMORY;      
    
pekka@mysql.com's avatar
pekka@mysql.com committed
2709 2710
    // check type and compute attribute size and array size
    if (! tmpAttr.translateExtType()) {
2711
      m_error.code= 703;
pekka@mysql.com's avatar
pekka@mysql.com committed
2712 2713
      DBUG_RETURN(-1);
    }
pekka@mysql.com's avatar
pekka@mysql.com committed
2714 2715
    // charset is defined exactly for char types
    if (col->getCharType() != (col->m_cs != NULL)) {
2716
      m_error.code= 703;
2717
      DBUG_RETURN(-1);
pekka@mysql.com's avatar
pekka@mysql.com committed
2718 2719
    }
    // primary key type check
2720 2721 2722 2723
    if (col->m_pk && 
        (err = NdbSqlUtil::check_column_for_pk(col->m_type, col->m_cs)))
    {
      m_error.code= err;
2724
      DBUG_RETURN(-1);
pekka@mysql.com's avatar
pekka@mysql.com committed
2725
    }
2726
    // distribution key not supported for Char attribute
2727
    if (distKeys && col->m_distributionKey && col->m_cs != NULL) {
2728 2729 2730 2731 2732 2733 2734 2735
      // we can allow this for non-var char where strxfrm does nothing
      if (col->m_type == NdbDictionary::Column::Char &&
          (col->m_cs->state & MY_CS_BINSORT))
        ;
      else {
        m_error.code= 745;
        DBUG_RETURN(-1);
      }
2736
    }
pekka@mysql.com's avatar
pekka@mysql.com committed
2737 2738 2739 2740
    // charset in upper half of precision
    if (col->getCharType()) {
      tmpAttr.AttributeExtPrecision |= (col->m_cs->number << 16);
    }
2741 2742

    tmpAttr.AttributeAutoIncrement = col->m_autoIncrement;
2743
    BaseString::snprintf(tmpAttr.AttributeDefaultValue, 
2744 2745 2746 2747 2748
	     sizeof(tmpAttr.AttributeDefaultValue),
	     col->m_defaultValue.c_str());
    s = SimpleProperties::pack(w, 
			       &tmpAttr,
			       DictTabInfo::AttributeMapping, 
joreland@mysql.com's avatar
joreland@mysql.com committed
2749
			       DictTabInfo::AttributeMappingSize, true);
2750 2751 2752
    w.add(DictTabInfo::AttributeEnd, 1);
  }

2753
  int ret;
2754
  
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
2755
  LinearSectionPtr ptr[1];
2756 2757
  ptr[0].p = (Uint32*)m_buffer.get_data();
  ptr[0].sz = m_buffer.length() / 4;
2758 2759 2760 2761 2762 2763 2764
  NdbApiSignal tSignal(m_reference);
  tSignal.theReceiversBlockNumber = DBDICT;
  if (alter) {
    tSignal.theVerId_signalNumber   = GSN_ALTER_TABLE_REQ;
    tSignal.theLength = AlterTableReq::SignalLength;

    AlterTableReq * req = CAST_PTR(AlterTableReq, tSignal.getDataPtrSend());
2765 2766 2767 2768
    
    req->senderRef = m_reference;
    req->senderData = 0;
    req->changeMask = impl.m_changeMask;
2769
    req->tableId = impl.m_id;
2770
    req->tableVersion = impl.m_version;;
2771 2772 2773 2774 2775

    int errCodes[] = { AlterTableRef::NotMaster, AlterTableRef::Busy, 0 };
    ret = dictSignal(&tSignal, ptr, 1,
		     0, // master
		     WAIT_ALTER_TAB_REQ,
2776
		     DICT_WAITFOR_TIMEOUT, 100,
2777
		     errCodes);
2778
    
2779 2780
    if(m_error.code == AlterTableRef::InvalidTableVersion) {
      // Clear caches and try again
2781
      DBUG_RETURN(INCOMPATIBLE_VERSION);
2782 2783
    }
  } else {
2784 2785
    tSignal.theVerId_signalNumber   = GSN_CREATE_TABLE_REQ;
    tSignal.theLength = CreateTableReq::SignalLength;
2786 2787 2788 2789 2790 2791 2792 2793

    CreateTableReq * req = CAST_PTR(CreateTableReq, tSignal.getDataPtrSend());
    req->senderRef = m_reference;
    req->senderData = 0;
    int errCodes[] = { CreateTableRef::Busy, CreateTableRef::NotMaster, 0 };
    ret = dictSignal(&tSignal, ptr, 1,
		     0, // master node
		     WAIT_CREATE_INDX_REQ,
2794
		     DICT_WAITFOR_TIMEOUT, 100,
2795 2796 2797
		     errCodes);
  }
  
2798
  DBUG_RETURN(ret);
2799 2800 2801 2802 2803 2804
}

void
NdbDictInterface::execCREATE_TABLE_CONF(NdbApiSignal * signal,
					LinearSectionPtr ptr[3])
{
2805 2806
  const CreateTableConf* const conf=
    CAST_CONSTPTR(CreateTableConf, signal->getDataPtr());
2807 2808 2809 2810
  m_buffer.grow(4 * 2); // 2 words
  Uint32* data = (Uint32*)m_buffer.get_data();
  data[0] = conf->tableId;
  data[1] = conf->tableVersion;
2811 2812 2813 2814
  m_waiter.signal(NO_WAIT);  
}

void
2815
NdbDictInterface::execCREATE_TABLE_REF(NdbApiSignal * sig,
2816 2817
				       LinearSectionPtr ptr[3])
{
2818
  const CreateTableRef* ref = CAST_CONSTPTR(CreateTableRef, sig->getDataPtr());
2819
  m_error.code= ref->errorCode;
2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831
  m_masterNodeId = ref->masterNodeId;
  m_waiter.signal(NO_WAIT);  
}

void
NdbDictInterface::execALTER_TABLE_CONF(NdbApiSignal * signal,
                                       LinearSectionPtr ptr[3])
{
  m_waiter.signal(NO_WAIT);
}

void
2832
NdbDictInterface::execALTER_TABLE_REF(NdbApiSignal * sig,
2833 2834
				      LinearSectionPtr ptr[3])
{
2835
  const AlterTableRef * ref = CAST_CONSTPTR(AlterTableRef, sig->getDataPtr());
2836
  m_error.code= ref->errorCode;
2837 2838 2839 2840 2841 2842 2843 2844 2845 2846
  m_masterNodeId = ref->masterNodeId;
  m_waiter.signal(NO_WAIT);
}

/*****************************************************************
 * Drop table
 */
int
NdbDictionaryImpl::dropTable(const char * name)
{
2847 2848
  DBUG_ENTER("NdbDictionaryImpl::dropTable");
  DBUG_PRINT("enter",("name: %s", name));
2849
  ASSERT_NOT_MYSQLD;
2850 2851
  NdbTableImpl * tab = getTable(name);
  if(tab == 0){
2852
    DBUG_RETURN(-1);
2853 2854 2855 2856 2857
  }
  int ret = dropTable(* tab);
  // If table stored in cache is incompatible with the one in the kernel
  // we must clear the cache and try again
  if (ret == INCOMPATIBLE_VERSION) {
2858 2859 2860
    const BaseString internalTableName(m_ndb.internalize_table_name(name));
    DBUG_PRINT("info",("INCOMPATIBLE_VERSION internal_name: %s", internalTableName.c_str()));
    m_localHash.drop(internalTableName.c_str());
2861
    m_globalHash->lock();
2862
    m_globalHash->release(tab, 1);
2863
    m_globalHash->unlock();
2864
    DBUG_RETURN(dropTable(name));
2865
  }
2866
  
2867
  DBUG_RETURN(ret);
2868 2869 2870 2871 2872
}

int
NdbDictionaryImpl::dropTable(NdbTableImpl & impl)
{
joreland@mysql.com's avatar
joreland@mysql.com committed
2873
  int res;
2874 2875 2876 2877 2878
  const char * name = impl.getName();
  if(impl.m_status == NdbDictionary::Object::New){
    return dropTable(name);
  }

2879 2880
  if (impl.m_indexType != NdbDictionary::Object::TypeUndefined)
  {
2881
    m_receiver.m_error.code= 1228;
2882 2883 2884 2885
    return -1;
  }

  List list;
2886
  if ((res = listIndexes(list, impl.m_id)) == -1){
2887
    return -1;
joreland@mysql.com's avatar
joreland@mysql.com committed
2888
  }
2889 2890
  for (unsigned i = 0; i < list.count; i++) {
    const List::Element& element = list.elements[i];
joreland@mysql.com's avatar
joreland@mysql.com committed
2891 2892
    if ((res = dropIndex(element.name, name)) == -1)
    {
2893
      return -1;
joreland@mysql.com's avatar
joreland@mysql.com committed
2894
    }
2895
  }
joreland@mysql.com's avatar
joreland@mysql.com committed
2896
  
pekka@mysql.com's avatar
pekka@mysql.com committed
2897
  if (impl.m_noOfBlobs != 0) {
joreland@mysql.com's avatar
joreland@mysql.com committed
2898
    if (dropBlobTables(impl) != 0){
pekka@mysql.com's avatar
pekka@mysql.com committed
2899
      return -1;
joreland@mysql.com's avatar
joreland@mysql.com committed
2900
    }
pekka@mysql.com's avatar
pekka@mysql.com committed
2901
  }
joreland@mysql.com's avatar
joreland@mysql.com committed
2902
  
2903
  int ret = m_receiver.dropTable(impl);  
2904
  if(ret == 0 || m_error.code == 709 || m_error.code == 723){
2905
    const char * internalTableName = impl.m_internalName.c_str();
2906

joreland@mysql.com's avatar
joreland@mysql.com committed
2907
    
2908 2909
    m_localHash.drop(internalTableName);
    m_globalHash->lock();
2910
    m_globalHash->release(&impl, 1);
2911
    m_globalHash->unlock();
joreland@mysql.com's avatar
joreland@mysql.com committed
2912 2913

    return 0;
2914 2915 2916 2917 2918
  }
  
  return ret;
}

2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961
int
NdbDictionaryImpl::dropTableGlobal(NdbTableImpl & impl)
{
  int res;
  DBUG_ENTER("NdbDictionaryImpl::dropTableGlobal");
  DBUG_ASSERT(impl.m_status != NdbDictionary::Object::New);
  DBUG_ASSERT(impl.m_indexType == NdbDictionary::Object::TypeUndefined);

  List list;
  if ((res = listIndexes(list, impl.m_id)) == -1){
    ERR_RETURN(getNdbError(), -1);
  }
  for (unsigned i = 0; i < list.count; i++) {
    const List::Element& element = list.elements[i];
    NdbIndexImpl *idx= getIndexGlobal(element.name, impl);
    if (idx == NULL)
    {
      ERR_RETURN(getNdbError(), -1);
    }
    if ((res = dropIndexGlobal(*idx)) == -1)
    {
      releaseIndexGlobal(*idx, 1);
      ERR_RETURN(getNdbError(), -1);
    }
    releaseIndexGlobal(*idx, 1);
  }
  
  if (impl.m_noOfBlobs != 0) {
    if (dropBlobTables(impl) != 0){
      ERR_RETURN(getNdbError(), -1);
    }
  }
  
  int ret = m_receiver.dropTable(impl);  
  impl.m_status = NdbDictionary::Object::Invalid;
  if(ret == 0 || m_error.code == 709 || m_error.code == 723)
  {
    DBUG_RETURN(0);
  }
  
  ERR_RETURN(getNdbError(), ret);
}

pekka@mysql.com's avatar
pekka@mysql.com committed
2962 2963 2964
int
NdbDictionaryImpl::dropBlobTables(NdbTableImpl & t)
{
2965
  DBUG_ENTER("NdbDictionaryImpl::dropBlobTables");
pekka@mysql.com's avatar
pekka@mysql.com committed
2966 2967
  for (unsigned i = 0; i < t.m_columns.size(); i++) {
    NdbColumnImpl & c = *t.m_columns[i];
pekka@mysql.com's avatar
pekka@mysql.com committed
2968
    if (! c.getBlobType() || c.getPartSize() == 0)
pekka@mysql.com's avatar
pekka@mysql.com committed
2969
      continue;
2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981
    NdbTableImpl* bt = c.m_blobTable;
    if (bt == NULL) {
      DBUG_PRINT("info", ("col %s: blob table pointer is NULL",
                          c.m_name.c_str()));
      continue; // "force" mode on
    }
    // drop directly - by-pass cache
    int ret = m_receiver.dropTable(*c.m_blobTable);
    if (ret != 0) {
      DBUG_PRINT("info", ("col %s: blob table %s: error %d",
                 c.m_name.c_str(), bt->m_internalName.c_str(), m_error.code));
      if (! (ret == 709 || ret == 723)) // "force" mode on
2982
        ERR_RETURN(getNdbError(), -1);
pekka@mysql.com's avatar
pekka@mysql.com committed
2983
    }
2984
    // leave c.m_blobTable defined
pekka@mysql.com's avatar
pekka@mysql.com committed
2985
  }
2986
  DBUG_RETURN(0);
pekka@mysql.com's avatar
pekka@mysql.com committed
2987 2988
}

2989 2990 2991 2992 2993 2994 2995 2996
int
NdbDictInterface::dropTable(const NdbTableImpl & impl)
{
  NdbApiSignal tSignal(m_reference);
  tSignal.theReceiversBlockNumber = DBDICT;
  tSignal.theVerId_signalNumber   = GSN_DROP_TABLE_REQ;
  tSignal.theLength = DropTableReq::SignalLength;
  
2997
  DropTableReq * req = CAST_PTR(DropTableReq, tSignal.getDataPtrSend());
2998 2999
  req->senderRef = m_reference;
  req->senderData = 0;
3000
  req->tableId = impl.m_id;
3001 3002
  req->tableVersion = impl.m_version;

3003 3004 3005 3006 3007 3008 3009
  int errCodes[] =
    { DropTableRef::NoDropTableRecordAvailable,
      DropTableRef::NotMaster,
      DropTableRef::Busy, 0 };
  int r = dictSignal(&tSignal, 0, 0,
		     0, // master
		     WAIT_DROP_TAB_REQ, 
3010
		     DICT_WAITFOR_TIMEOUT, 100,
3011
		     errCodes);
3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022
  if(m_error.code == DropTableRef::InvalidTableVersion) {
    // Clear caches and try again
    return INCOMPATIBLE_VERSION;
  }
  return r;
}

void
NdbDictInterface::execDROP_TABLE_CONF(NdbApiSignal * signal,
				       LinearSectionPtr ptr[3])
{
3023
  DBUG_ENTER("NdbDictInterface::execDROP_TABLE_CONF");
3024 3025 3026
  //DropTableConf* const conf = CAST_CONSTPTR(DropTableConf, signal->getDataPtr());

  m_waiter.signal(NO_WAIT);  
3027
  DBUG_VOID_RETURN;
3028 3029 3030 3031 3032 3033
}

void
NdbDictInterface::execDROP_TABLE_REF(NdbApiSignal * signal,
				      LinearSectionPtr ptr[3])
{
3034
  DBUG_ENTER("NdbDictInterface::execDROP_TABLE_REF");
3035
  const DropTableRef* ref = CAST_CONSTPTR(DropTableRef, signal->getDataPtr());
3036
  m_error.code= ref->errorCode;
3037 3038
  m_masterNodeId = ref->masterNodeId;
  m_waiter.signal(NO_WAIT);  
3039
  DBUG_VOID_RETURN;
3040 3041 3042
}

int
3043
NdbDictionaryImpl::invalidateObject(NdbTableImpl & impl)
3044 3045
{
  const char * internalTableName = impl.m_internalName.c_str();
3046 3047
  DBUG_ENTER("NdbDictionaryImpl::invalidateObject");
  DBUG_PRINT("enter", ("internal_name: %s", internalTableName));
3048

3049
  m_localHash.drop(internalTableName);
3050
  m_globalHash->lock();
3051
  m_globalHash->release(&impl, 1);
3052
  m_globalHash->unlock();
3053
  DBUG_RETURN(0);
3054 3055 3056
}

int
3057
NdbDictionaryImpl::removeCachedObject(NdbTableImpl & impl)
3058 3059
{
  const char * internalTableName = impl.m_internalName.c_str();
3060 3061
  DBUG_ENTER("NdbDictionaryImpl::removeCachedObject");
  DBUG_PRINT("enter", ("internal_name: %s", internalTableName));
3062 3063

  m_localHash.drop(internalTableName);  
3064
  m_globalHash->lock();
3065
  m_globalHash->release(&impl);
3066
  m_globalHash->unlock();
3067
  DBUG_RETURN(0);
3068 3069
}

3070 3071
int
NdbDictInterface::create_index_obj_from_table(NdbIndexImpl** dst,
3072
					      NdbTableImpl* tab,
3073 3074 3075
					      const NdbTableImpl* prim)
{
  DBUG_ENTER("NdbDictInterface::create_index_obj_from_table");
3076
  NdbIndexImpl *idx = new NdbIndexImpl();
3077 3078
  idx->m_version = tab->m_version;
  idx->m_status = tab->m_status;
3079
  idx->m_id = tab->m_id;
3080
  idx->m_externalName.assign(tab->getName());
3081
  idx->m_tableName.assign(prim->m_externalName);
3082
  NdbDictionary::Object::Type type = idx->m_type = tab->m_indexType;
3083
  idx->m_logging = tab->m_logging;
knielsen@ymer.(none)'s avatar
knielsen@ymer.(none) committed
3084
  idx->m_temporary = tab->m_temporary;
3085
  // skip last attribute (NDB$PK or NDB$TNODE)
3086 3087 3088 3089 3090 3091

  const Uint32 distKeys = prim->m_noOfDistributionKeys;
  Uint32 keyCount = (distKeys ? distKeys : prim->m_noOfKeys);

  unsigned i;
  for(i = 0; i+1<tab->m_columns.size(); i++){
3092 3093
    NdbColumnImpl* org = tab->m_columns[i];

3094 3095
    NdbColumnImpl* col = new NdbColumnImpl;
    // Copy column definition
3096
    *col = * org;
3097
    idx->m_columns.push_back(col);
3098

3099 3100 3101
    /**
     * reverse map
     */
3102 3103
    const NdbColumnImpl* primCol = prim->getColumn(col->getName());
    int key_id = primCol->getColumnNo();
3104 3105 3106 3107
    int fill = -1;
    idx->m_key_ids.fill(key_id, fill);
    idx->m_key_ids[key_id] = i;
    col->m_keyInfoPos = key_id;
3108

3109
    if(type == NdbDictionary::Object::OrderedIndex && 
3110 3111
       (primCol->m_distributionKey ||
	(distKeys == 0 && primCol->getPrimaryKey())))
3112
    {
3113 3114
      keyCount--;
      org->m_distributionKey = 1;
3115
    }
3116
  }
3117 3118 3119 3120 3121 3122 3123 3124 3125 3126

  if(keyCount == 0)
  {
    tab->m_noOfDistributionKeys = (distKeys ? distKeys : prim->m_noOfKeys);
  }
  else 
  {
    for(i = 0; i+1<tab->m_columns.size(); i++)
      tab->m_columns[i]->m_distributionKey = 0;
  }
3127

jonas@perch.ndb.mysql.com's avatar
ndb -  
jonas@perch.ndb.mysql.com committed
3128 3129 3130
  idx->m_table_id = prim->getObjectId();
  idx->m_table_version = prim->getObjectVersion();
  
3131
  * dst = idx;
3132 3133
  DBUG_PRINT("exit", ("m_id: %d  m_version: %d", idx->m_id, idx->m_version));
  DBUG_RETURN(0);
3134 3135 3136 3137 3138 3139 3140 3141
}

/*****************************************************************
 * Create index
 */
int
NdbDictionaryImpl::createIndex(NdbIndexImpl &ix)
{
3142
  ASSERT_NOT_MYSQLD;
3143 3144 3145 3146 3147 3148 3149 3150 3151
  NdbTableImpl* tab = getTable(ix.getTable());
  if(tab == 0){
    m_error.code = 4249;
    return -1;
  }
  
  return m_receiver.createIndex(m_ndb, ix, * tab);
}

3152 3153 3154 3155 3156 3157
int
NdbDictionaryImpl::createIndex(NdbIndexImpl &ix, NdbTableImpl &tab)
{
  return m_receiver.createIndex(m_ndb, ix, tab);
}

3158 3159
int 
NdbDictInterface::createIndex(Ndb & ndb,
3160
			      const NdbIndexImpl & impl, 
3161 3162 3163 3164
			      const NdbTableImpl & table)
{
  //validate();
  //aggregate();
3165
  unsigned i, err;
3166 3167 3168 3169 3170 3171
  UtilBufferWriter w(m_buffer);
  const size_t len = strlen(impl.m_externalName.c_str()) + 1;
  if(len > MAX_TAB_NAME_SIZE) {
    m_error.code = 4241;
    return -1;
  }
3172 3173 3174
  const BaseString internalName(
    ndb.internalize_index_name(&table, impl.getName()));
  w.add(DictTabInfo::TableName, internalName.c_str());
3175
  w.add(DictTabInfo::TableLoggedFlag, impl.m_logging);
knielsen@ymer.(none)'s avatar
knielsen@ymer.(none) committed
3176
  w.add(DictTabInfo::TableTemporaryFlag, impl.m_temporary);
3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198

  NdbApiSignal tSignal(m_reference);
  tSignal.theReceiversBlockNumber = DBDICT;
  tSignal.theVerId_signalNumber   = GSN_CREATE_INDX_REQ;
  tSignal.theLength = CreateIndxReq::SignalLength;
  
  CreateIndxReq * const req = CAST_PTR(CreateIndxReq, tSignal.getDataPtrSend());
  
  req->setUserRef(m_reference);
  req->setConnectionPtr(0);
  req->setRequestType(CreateIndxReq::RT_USER);
  
  Uint32 it = getKernelConstant(impl.m_type,
				indexTypeMapping,
				DictTabInfo::UndefTableType);
  
  if(it == DictTabInfo::UndefTableType){
    m_error.code = 4250;
    return -1;
  }
  req->setIndexType((DictTabInfo::TableType) it);
  
3199
  req->setTableId(table.m_id);
3200 3201 3202
  req->setOnline(true);
  AttributeList attributeList;
  attributeList.sz = impl.m_columns.size();
3203
  for(i = 0; i<attributeList.sz; i++){
3204 3205 3206 3207 3208 3209
    const NdbColumnImpl* col = 
      table.getColumn(impl.m_columns[i]->m_name.c_str());
    if(col == 0){
      m_error.code = 4247;
      return -1;
    }
3210
    // Copy column definition  XXX must be wrong, overwrites
3211 3212
    *impl.m_columns[i] = *col;

pekka@mysql.com's avatar
pekka@mysql.com committed
3213 3214
    // index key type check
    if (it == DictTabInfo::UniqueHashIndex &&
3215 3216
        (err = NdbSqlUtil::check_column_for_hash_index(col->m_type, col->m_cs))
        ||
pekka@mysql.com's avatar
pekka@mysql.com committed
3217
        it == DictTabInfo::OrderedIndex &&
3218 3219 3220
        (err = NdbSqlUtil::check_column_for_ordered_index(col->m_type, col->m_cs)))
    {
      m_error.code = err;
pekka@mysql.com's avatar
pekka@mysql.com committed
3221 3222
      return -1;
    }
3223 3224
    // API uses external column number to talk to DICT
    attributeList.id[i] = col->m_column_no;
3225
  }
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
3226
  LinearSectionPtr ptr[2];
3227 3228 3229 3230 3231
  ptr[0].p = (Uint32*)&attributeList;
  ptr[0].sz = 1 + attributeList.sz;
  ptr[1].p = (Uint32*)m_buffer.get_data();
  ptr[1].sz = m_buffer.length() >> 2;                //BUG?

3232 3233 3234
  int errCodes[] = { CreateIndxRef::Busy, CreateIndxRef::NotMaster, 0 };
  return dictSignal(&tSignal, ptr, 2,
		    0, // master
3235
		    WAIT_CREATE_INDX_REQ,
3236
		    DICT_WAITFOR_TIMEOUT, 100,
3237
		    errCodes);
3238 3239 3240 3241 3242 3243 3244 3245 3246 3247
}

void
NdbDictInterface::execCREATE_INDX_CONF(NdbApiSignal * signal,
				       LinearSectionPtr ptr[3])
{
  m_waiter.signal(NO_WAIT);  
}

void
3248
NdbDictInterface::execCREATE_INDX_REF(NdbApiSignal * sig,
3249 3250
				      LinearSectionPtr ptr[3])
{
3251
  const CreateIndxRef* ref = CAST_CONSTPTR(CreateIndxRef, sig->getDataPtr());
3252
  m_error.code = ref->getErrorCode();
3253
  if(m_error.code == ref->NotMaster)
3254
    m_masterNodeId= ref->masterNodeId;
3255 3256 3257 3258 3259 3260 3261 3262 3263 3264
  m_waiter.signal(NO_WAIT);  
}

/*****************************************************************
 * Drop index
 */
int
NdbDictionaryImpl::dropIndex(const char * indexName, 
			     const char * tableName)
{
3265
  ASSERT_NOT_MYSQLD;
3266 3267 3268 3269 3270 3271 3272 3273 3274
  NdbIndexImpl * idx = getIndex(indexName, tableName);
  if (idx == 0) {
    m_error.code = 4243;
    return -1;
  }
  int ret = dropIndex(*idx, tableName);
  // If index stored in cache is incompatible with the one in the kernel
  // we must clear the cache and try again
  if (ret == INCOMPATIBLE_VERSION) {
3275
    const BaseString internalIndexName((tableName)
3276
      ?
3277
      m_ndb.internalize_index_name(getTable(tableName), indexName)
3278
      :
3279 3280 3281
      m_ndb.internalize_table_name(indexName)); // Index is also a table

    m_localHash.drop(internalIndexName.c_str());
3282
    m_globalHash->lock();
3283
    m_globalHash->release(idx->m_table, 1);
3284
    m_globalHash->unlock();
3285 3286
    return dropIndex(indexName, tableName);
  }
3287

3288 3289 3290 3291 3292 3293 3294
  return ret;
}

int
NdbDictionaryImpl::dropIndex(NdbIndexImpl & impl, const char * tableName)
{
  const char * indexName = impl.getName();
3295
  if (tableName || m_ndb.usingFullyQualifiedNames()) {
3296 3297 3298 3299 3300 3301 3302
    NdbTableImpl * timpl = impl.m_table;
    
    if (timpl == 0) {
      m_error.code = 709;
      return -1;
    }

3303
    const BaseString internalIndexName((tableName)
3304
      ?
3305
      m_ndb.internalize_index_name(getTable(tableName), indexName)
3306
      :
3307
      m_ndb.internalize_table_name(indexName)); // Index is also a table
3308 3309 3310 3311

    if(impl.m_status == NdbDictionary::Object::New){
      return dropIndex(indexName, tableName);
    }
3312

3313 3314 3315
    int ret= dropIndexGlobal(impl);
    if (ret == 0)
    {
3316
      m_globalHash->lock();
3317
      m_globalHash->release(impl.m_table, 1);
3318
      m_globalHash->unlock();
3319
      m_localHash.drop(internalIndexName.c_str());
3320 3321 3322 3323 3324 3325 3326 3327
    }
    return ret;
  }

  m_error.code = 4243;
  return -1;
}

3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340
int
NdbDictionaryImpl::dropIndexGlobal(NdbIndexImpl & impl)
{
  DBUG_ENTER("NdbDictionaryImpl::dropIndexGlobal");
  int ret = m_receiver.dropIndex(impl, *impl.m_table);
  impl.m_status = NdbDictionary::Object::Invalid;
  if(ret == 0)
  {
    DBUG_RETURN(0);
  }
  ERR_RETURN(getNdbError(), ret);
}

3341 3342 3343 3344
int
NdbDictInterface::dropIndex(const NdbIndexImpl & impl, 
			    const NdbTableImpl & timpl)
{
3345 3346 3347
  DBUG_ENTER("NdbDictInterface::dropIndex");
  DBUG_PRINT("enter", ("indexId: %d  indexVersion: %d",
                       timpl.m_id, timpl.m_version));
3348 3349 3350 3351 3352 3353 3354 3355 3356 3357
  NdbApiSignal tSignal(m_reference);
  tSignal.theReceiversBlockNumber = DBDICT;
  tSignal.theVerId_signalNumber   = GSN_DROP_INDX_REQ;
  tSignal.theLength = DropIndxReq::SignalLength;

  DropIndxReq * const req = CAST_PTR(DropIndxReq, tSignal.getDataPtrSend());
  req->setUserRef(m_reference);
  req->setConnectionPtr(0);
  req->setRequestType(DropIndxReq::RT_USER);
  req->setTableId(~0);  // DICT overwrites
3358
  req->setIndexId(timpl.m_id);
3359 3360
  req->setIndexVersion(timpl.m_version);

3361 3362 3363
  int errCodes[] = { DropIndxRef::Busy, DropIndxRef::NotMaster, 0 };
  int r = dictSignal(&tSignal, 0, 0,
		     0, // master
3364
		     WAIT_DROP_INDX_REQ,
3365
		     DICT_WAITFOR_TIMEOUT, 100,
3366
		     errCodes);
3367 3368
  if(m_error.code == DropIndxRef::InvalidIndexVersion) {
    // Clear caches and try again
3369
    ERR_RETURN(m_error, INCOMPATIBLE_VERSION);
3370
  }
3371
  ERR_RETURN(m_error, r);
3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384
}

void
NdbDictInterface::execDROP_INDX_CONF(NdbApiSignal * signal,
				       LinearSectionPtr ptr[3])
{
  m_waiter.signal(NO_WAIT);  
}

void
NdbDictInterface::execDROP_INDX_REF(NdbApiSignal * signal,
				      LinearSectionPtr ptr[3])
{
3385
  const DropIndxRef* ref = CAST_CONSTPTR(DropIndxRef, signal->getDataPtr());
3386
  m_error.code = ref->getErrorCode();
3387
  if(m_error.code == ref->NotMaster)
3388
    m_masterNodeId= ref->masterNodeId;
3389 3390 3391 3392 3393 3394 3395 3396 3397 3398
  m_waiter.signal(NO_WAIT);  
}

/*****************************************************************
 * Create event
 */

int
NdbDictionaryImpl::createEvent(NdbEventImpl & evnt)
{
3399
  DBUG_ENTER("NdbDictionaryImpl::createEvent");
3400
  int i;
3401 3402 3403 3404 3405 3406 3407
  NdbTableImpl* tab= evnt.m_tableImpl;
  if (tab == 0)
  {
    tab= getTable(evnt.getTableName());
    if(tab == 0){
      DBUG_PRINT("info",("NdbDictionaryImpl::createEvent: table not found: %s",
			 evnt.getTableName()));
3408
      ERR_RETURN(getNdbError(), -1);
3409
    }
3410
    evnt.setTable(tab);
3411 3412
  }

3413
  DBUG_PRINT("info",("Table: id: %d version: %d", tab->m_id, tab->m_version));
3414

3415 3416 3417 3418
  NdbTableImpl &table = *evnt.m_tableImpl;

  int attributeList_sz = evnt.m_attrIds.size();

3419
  for (i = 0; i < attributeList_sz; i++) {
3420 3421 3422 3423 3424
    NdbColumnImpl *col_impl = table.getColumn(evnt.m_attrIds[i]);
    if (col_impl) {
      evnt.m_facade->addColumn(*(col_impl->m_facade));
    } else {
      ndbout_c("Attr id %u in table %s not found", evnt.m_attrIds[i],
3425 3426
	       evnt.getTableName());
      m_error.code= 4713;
3427
      ERR_RETURN(getNdbError(), -1);
3428 3429 3430 3431 3432 3433
    }
  }

  evnt.m_attrIds.clear();

  attributeList_sz = evnt.m_columns.size();
3434 3435

  DBUG_PRINT("info",("Event on tableId=%d, tableVersion=%d, event name %s, no of columns %d",
3436
		     table.m_id, table.m_version,
3437 3438 3439
		     evnt.m_name.c_str(),
		     evnt.m_columns.size()));

3440 3441 3442
  int pk_count = 0;
  evnt.m_attrListBitmask.clear();

3443
  for(i = 0; i<attributeList_sz; i++){
3444 3445 3446
    const NdbColumnImpl* col = 
      table.getColumn(evnt.m_columns[i]->m_name.c_str());
    if(col == 0){
3447
      m_error.code= 4247;
3448
      ERR_RETURN(getNdbError(), -1);
3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460
    }
    // Copy column definition
    *evnt.m_columns[i] = *col;
    
    if(col->m_pk){
      pk_count++;
    }
    
    evnt.m_attrListBitmask.set(col->m_attrId);
  }
  
  // Sort index attributes according to primary table (using insertion sort)
3461
  for(i = 1; i < attributeList_sz; i++) {
3462 3463 3464 3465 3466 3467 3468 3469 3470
    NdbColumnImpl* temp = evnt.m_columns[i];
    unsigned int j = i;
    while((j > 0) && (evnt.m_columns[j - 1]->m_attrId > temp->m_attrId)) {
      evnt.m_columns[j] = evnt.m_columns[j - 1];
      j--;
    }
    evnt.m_columns[j] = temp;
  }
  // Check for illegal duplicate attributes
3471
  for(i = 1; i<attributeList_sz; i++) {
3472
    if (evnt.m_columns[i-1]->m_attrId == evnt.m_columns[i]->m_attrId) {
3473
      m_error.code= 4258;
3474
      ERR_RETURN(getNdbError(), -1);
3475 3476 3477 3478 3479 3480 3481 3482 3483 3484
    }
  }
  
#ifdef EVENT_DEBUG
  char buf[128] = {0};
  evnt.m_attrListBitmask.getText(buf);
  ndbout_c("createEvent: mask = %s", buf);
#endif

  // NdbDictInterface m_receiver;
3485
  if (m_receiver.createEvent(m_ndb, evnt, 0 /* getFlag unset */) != 0)
3486
    ERR_RETURN(getNdbError(), -1);
3487 3488 3489 3490 3491 3492

  // Create blob events
  if (evnt.m_mergeEvents && createBlobEvents(evnt) != 0) {
    int save_code = m_error.code;
    (void)dropEvent(evnt.m_name.c_str());
    m_error.code = save_code;
3493
    ERR_RETURN(getNdbError(), -1);
3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512
  }
  DBUG_RETURN(0);
}

int
NdbDictionaryImpl::createBlobEvents(NdbEventImpl& evnt)
{
  DBUG_ENTER("NdbDictionaryImpl::createBlobEvents");
  NdbTableImpl& t = *evnt.m_tableImpl;
  Uint32 n = t.m_noOfBlobs;
  Uint32 i;
  for (i = 0; i < evnt.m_columns.size() && n > 0; i++) {
    NdbColumnImpl & c = *evnt.m_columns[i];
    if (! c.getBlobType() || c.getPartSize() == 0)
      continue;
    n--;
    NdbEventImpl blob_evnt;
    NdbBlob::getBlobEvent(blob_evnt, &evnt, &c);
    if (createEvent(blob_evnt) != 0)
3513
      ERR_RETURN(getNdbError(), -1);
3514 3515
  }
  DBUG_RETURN(0);
3516 3517 3518 3519 3520 3521 3522
}

int
NdbDictInterface::createEvent(class Ndb & ndb,
			      NdbEventImpl & evnt,
			      int getFlag)
{
3523 3524 3525
  DBUG_ENTER("NdbDictInterface::createEvent");
  DBUG_PRINT("enter",("getFlag=%d",getFlag));

3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542
  NdbApiSignal tSignal(m_reference);
  tSignal.theReceiversBlockNumber = DBDICT;
  tSignal.theVerId_signalNumber   = GSN_CREATE_EVNT_REQ;
  if (getFlag)
    tSignal.theLength = CreateEvntReq::SignalLengthGet;
  else
    tSignal.theLength = CreateEvntReq::SignalLengthCreate;

  CreateEvntReq * const req = CAST_PTR(CreateEvntReq, tSignal.getDataPtrSend());
  
  req->setUserRef(m_reference);
  req->setUserData(0);

  if (getFlag) {
    // getting event from Dictionary
    req->setRequestType(CreateEvntReq::RT_USER_GET);
  } else {
3543
    DBUG_PRINT("info",("tableId: %u tableVersion: %u",
3544 3545
		       evnt.m_tableImpl->m_id, 
                       evnt.m_tableImpl->m_version));
3546 3547
    // creating event in Dictionary
    req->setRequestType(CreateEvntReq::RT_USER_CREATE);
3548 3549
    req->setTableId(evnt.m_tableImpl->m_id);
    req->setTableVersion(evnt.m_tableImpl->m_version);
3550 3551
    req->setAttrListBitmask(evnt.m_attrListBitmask);
    req->setEventType(evnt.mi_type);
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
3552 3553 3554 3555 3556
    req->clearFlags();
    if (evnt.m_rep & NdbDictionary::Event::ER_ALL)
      req->setReportAll();
    if (evnt.m_rep & NdbDictionary::Event::ER_SUBSCRIBE)
      req->setReportSubscribe();
3557 3558 3559 3560
  }

  UtilBufferWriter w(m_buffer);

3561
  const size_t len = strlen(evnt.m_name.c_str()) + 1;
3562
  if(len > MAX_TAB_NAME_SIZE) {
3563
    m_error.code= 4241;
3564
    ERR_RETURN(getNdbError(), -1);
3565 3566
  }

3567
  w.add(SimpleProperties::StringValue, evnt.m_name.c_str());
3568 3569

  if (getFlag == 0)
3570 3571 3572
  {
    const BaseString internal_tabname(
      ndb.internalize_table_name(evnt.m_tableName.c_str()));
3573
    w.add(SimpleProperties::StringValue,
3574 3575
	 internal_tabname.c_str());
  }
3576

msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
3577
  LinearSectionPtr ptr[1];
3578 3579 3580
  ptr[0].p = (Uint32*)m_buffer.get_data();
  ptr[0].sz = (m_buffer.length()+3) >> 2;

3581 3582 3583
  int ret = dictSignal(&tSignal,ptr, 1,
		       0, // master
		       WAIT_CREATE_INDX_REQ,
3584
		       DICT_WAITFOR_TIMEOUT, 100,
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
3585
		       0, -1);
3586 3587

  if (ret) {
3588
    ERR_RETURN(getNdbError(), ret);
3589
  }
3590
  
3591 3592 3593 3594 3595 3596 3597 3598
  char *dataPtr = (char *)m_buffer.get_data();
  unsigned int lenCreateEvntConf = *((unsigned int *)dataPtr);
  dataPtr += sizeof(lenCreateEvntConf);
  CreateEvntConf const * evntConf = (CreateEvntConf *)dataPtr;
  dataPtr += lenCreateEvntConf;
  
  //  NdbEventImpl *evntImpl = (NdbEventImpl *)evntConf->getUserData();

3599 3600 3601 3602 3603
  evnt.m_eventId = evntConf->getEventId();
  evnt.m_eventKey = evntConf->getEventKey();
  evnt.m_table_id = evntConf->getTableId();
  evnt.m_table_version = evntConf->getTableVersion();

3604 3605 3606 3607 3608
  if (getFlag) {
    evnt.m_attrListBitmask = evntConf->getAttrListBitmask();
    evnt.mi_type           = evntConf->getEventType();
    evnt.setTable(dataPtr);
  } else {
3609
    if ((Uint32) evnt.m_tableImpl->m_id         != evntConf->getTableId() ||
3610
	evnt.m_tableImpl->m_version    != evntConf->getTableVersion() ||
3611 3612 3613
	//evnt.m_attrListBitmask != evntConf->getAttrListBitmask() ||
	evnt.mi_type           != evntConf->getEventType()) {
      ndbout_c("ERROR*************");
3614
      ERR_RETURN(getNdbError(), 1);
3615 3616 3617
    }
  }

3618
  DBUG_RETURN(0);
3619 3620 3621
}

int
3622
NdbDictionaryImpl::executeSubscribeEvent(NdbEventOperationImpl & ev_op)
3623 3624
{
  // NdbDictInterface m_receiver;
3625
  return m_receiver.executeSubscribeEvent(m_ndb, ev_op);
3626 3627 3628 3629
}

int
NdbDictInterface::executeSubscribeEvent(class Ndb & ndb,
3630
					NdbEventOperationImpl & ev_op)
3631
{
3632
  DBUG_ENTER("NdbDictInterface::executeSubscribeEvent");
3633 3634 3635 3636 3637
  NdbApiSignal tSignal(m_reference);
  tSignal.theReceiversBlockNumber = DBDICT;
  tSignal.theVerId_signalNumber   = GSN_SUB_START_REQ;
  tSignal.theLength = SubStartReq::SignalLength2;
  
3638
  SubStartReq * req = CAST_PTR(SubStartReq, tSignal.getDataPtrSend());
3639

3640 3641 3642 3643 3644
  req->subscriptionId   = ev_op.m_eventImpl->m_eventId;
  req->subscriptionKey  = ev_op.m_eventImpl->m_eventKey;
  req->part             = SubscriptionData::TableData;
  req->subscriberData   = ev_op.m_oid;
  req->subscriberRef    = m_reference;
3645

3646 3647 3648
  DBUG_PRINT("info",("GSN_SUB_START_REQ subscriptionId=%d,subscriptionKey=%d,"
		     "subscriberData=%d",req->subscriptionId,
		     req->subscriptionKey,req->subscriberData));
3649

3650
  DBUG_RETURN(dictSignal(&tSignal,NULL,0,
3651
			 0 /*use masternode id*/,
3652
			 WAIT_CREATE_INDX_REQ /*WAIT_CREATE_EVNT_REQ*/,
3653
			 -1, 100,
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
3654
			 0, -1));
3655 3656 3657
}

int
3658
NdbDictionaryImpl::stopSubscribeEvent(NdbEventOperationImpl & ev_op)
3659 3660
{
  // NdbDictInterface m_receiver;
3661
  return m_receiver.stopSubscribeEvent(m_ndb, ev_op);
3662 3663 3664 3665
}

int
NdbDictInterface::stopSubscribeEvent(class Ndb & ndb,
3666
				     NdbEventOperationImpl & ev_op)
3667
{
3668
  DBUG_ENTER("NdbDictInterface::stopSubscribeEvent");
3669 3670 3671 3672 3673 3674 3675

  NdbApiSignal tSignal(m_reference);
  //  tSignal.theReceiversBlockNumber = SUMA;
  tSignal.theReceiversBlockNumber = DBDICT;
  tSignal.theVerId_signalNumber   = GSN_SUB_STOP_REQ;
  tSignal.theLength = SubStopReq::SignalLength;
  
3676
  SubStopReq * req = CAST_PTR(SubStopReq, tSignal.getDataPtrSend());
3677

3678 3679 3680 3681 3682
  req->subscriptionId  = ev_op.m_eventImpl->m_eventId;
  req->subscriptionKey = ev_op.m_eventImpl->m_eventKey;
  req->subscriberData  = ev_op.m_oid;
  req->part            = (Uint32) SubscriptionData::TableData;
  req->subscriberRef   = m_reference;
3683

3684 3685 3686
  DBUG_PRINT("info",("GSN_SUB_STOP_REQ subscriptionId=%d,subscriptionKey=%d,"
		     "subscriberData=%d",req->subscriptionId,
		     req->subscriptionKey,req->subscriberData));
3687

3688
  DBUG_RETURN(dictSignal(&tSignal,NULL,0,
3689
			 0 /*use masternode id*/,
3690
			 WAIT_CREATE_INDX_REQ /*WAIT_SUB_STOP__REQ*/,
3691
			 -1, 100,
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
3692
			 0, -1));
3693 3694 3695
}

NdbEventImpl * 
3696
NdbDictionaryImpl::getEvent(const char * eventName, NdbTableImpl* tab)
3697
{
3698 3699
  DBUG_ENTER("NdbDictionaryImpl::getEvent");
  DBUG_PRINT("enter",("eventName= %s", eventName));
3700

3701
  NdbEventImpl *ev =  new NdbEventImpl();
3702
  if (ev == NULL) {
3703
    DBUG_RETURN(NULL);
3704 3705 3706 3707 3708 3709 3710 3711
  }

  ev->setName(eventName);

  int ret = m_receiver.createEvent(m_ndb, *ev, 1 /* getFlag set */);

  if (ret) {
    delete ev;
3712
    DBUG_RETURN(NULL);
3713 3714
  }

3715 3716 3717 3718 3719 3720
  // We only have the table name with internal name
  DBUG_PRINT("info",("table %s", ev->getTableName()));
  if (tab == NULL)
  {
    tab= fetchGlobalTableImplRef(InitTable(this, ev->getTableName()));
    if (tab == 0)
3721 3722 3723 3724 3725
    {
      DBUG_PRINT("error",("unable to find table %s", ev->getTableName()));
      delete ev;
      DBUG_RETURN(NULL);
    }
3726
    if ((tab->m_status != NdbDictionary::Object::Retrieved) ||
3727
        ((Uint32) tab->m_id != ev->m_table_id) ||
3728
        (table_version_major(tab->m_version) !=
3729
         table_version_major(ev->m_table_version)))
3730
    {
3731 3732 3733 3734
      DBUG_PRINT("info", ("mismatch on verison in cache"));
      releaseTableGlobal(*tab, 1);
      tab= fetchGlobalTableImplRef(InitTable(this, ev->getTableName()));
      if (tab == 0)
3735 3736 3737 3738 3739 3740
      {
        DBUG_PRINT("error",("unable to find table %s", ev->getTableName()));
        delete ev;
        DBUG_RETURN(NULL);
      }
    }
3741 3742
    ev->setTable(tab);
    releaseTableGlobal(*tab, 0);
3743
  }
3744 3745 3746
  else
    ev->setTable(tab);
  tab = 0;
3747

3748
  ev->setTable(m_ndb.externalizeTableName(ev->getTableName()));  
3749 3750 3751
  // get the columns from the attrListBitmask
  NdbTableImpl &table = *ev->m_tableImpl;
  AttributeMask & mask = ev->m_attrListBitmask;
3752
  unsigned attributeList_sz = mask.count();
3753

3754 3755
  DBUG_PRINT("info",("Table: id: %d version: %d", 
                     table.m_id, table.m_version));
3756

3757
  if ((Uint32) table.m_id != ev->m_table_id ||
3758 3759 3760 3761 3762 3763 3764
      table_version_major(table.m_version) !=
      table_version_major(ev->m_table_version))
  {
    m_error.code = 241;
    delete ev;
    DBUG_RETURN(NULL);
  }
3765
#ifndef DBUG_OFF
3766 3767
  char buf[128] = {0};
  mask.getText(buf);
3768 3769
  DBUG_PRINT("info",("attributeList_sz= %d, mask= %s", 
                     attributeList_sz, buf));
3770 3771
#endif

3772
  
3773
  if ( attributeList_sz > (uint) table.getNoOfColumns() )
3774
  {
3775
    m_error.code = 241;
3776 3777 3778 3779
    DBUG_PRINT("error",("Invalid version, too many columns"));
    delete ev;
    DBUG_RETURN(NULL);
  }
3780

3781 3782
  assert( (int)attributeList_sz <= table.getNoOfColumns() );
  for(unsigned id= 0; ev->m_columns.size() < attributeList_sz; id++) {
3783
    if ( id >= (uint) table.getNoOfColumns())
3784
    {
3785
      m_error.code = 241;
3786
      DBUG_PRINT("error",("Invalid version, column %d out of range", id));
3787
      delete ev;
3788
      DBUG_RETURN(NULL);
3789
    }
3790 3791 3792 3793 3794
    if (!mask.get(id))
      continue;

    const NdbColumnImpl* col = table.getColumn(id);
    DBUG_PRINT("info",("column %d %s", id, col->getName()));
3795 3796 3797 3798 3799
    NdbColumnImpl* new_col = new NdbColumnImpl;
    // Copy column definition
    *new_col = *col;
    ev->m_columns.push_back(new_col);
  }
3800
  DBUG_RETURN(ev);
3801 3802
}

3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822
// ev is main event and has been retrieved previously
NdbEventImpl *
NdbDictionaryImpl::getBlobEvent(const NdbEventImpl& ev, uint col_no)
{
  DBUG_ENTER("NdbDictionaryImpl::getBlobEvent");
  DBUG_PRINT("enter", ("ev=%s col=%u", ev.m_name.c_str(), col_no));

  NdbTableImpl* tab = ev.m_tableImpl;
  assert(tab != NULL && col_no < tab->m_columns.size());
  NdbColumnImpl* col = tab->m_columns[col_no];
  assert(col != NULL && col->getBlobType() && col->getPartSize() != 0);
  NdbTableImpl* blob_tab = col->m_blobTable;
  assert(blob_tab != NULL);
  char bename[MAX_TAB_NAME_SIZE];
  NdbBlob::getBlobEventName(bename, &ev, col);

  NdbEventImpl* blob_ev = getEvent(bename, blob_tab);
  DBUG_RETURN(blob_ev);
}

3823 3824 3825 3826
void
NdbDictInterface::execCREATE_EVNT_CONF(NdbApiSignal * signal,
				       LinearSectionPtr ptr[3])
{
3827 3828
  DBUG_ENTER("NdbDictInterface::execCREATE_EVNT_CONF");

3829 3830 3831 3832 3833 3834 3835 3836 3837
  m_buffer.clear();
  unsigned int len = signal->getLength() << 2;
  m_buffer.append((char *)&len, sizeof(len));
  m_buffer.append(signal->getDataPtr(), len);

  if (signal->m_noOfSections > 0) {
    m_buffer.append((char *)ptr[0].p, strlen((char *)ptr[0].p)+1);
  }

3838 3839 3840 3841 3842 3843
  const CreateEvntConf * const createEvntConf=
    CAST_CONSTPTR(CreateEvntConf, signal->getDataPtr());

  Uint32 subscriptionId = createEvntConf->getEventId();
  Uint32 subscriptionKey = createEvntConf->getEventKey();

3844 3845
  DBUG_PRINT("info",("nodeid=%d,subscriptionId=%d,subscriptionKey=%d",
		     refToNode(signal->theSendersBlockRef),
3846
		     subscriptionId,subscriptionKey));
3847
  m_waiter.signal(NO_WAIT);
3848
  DBUG_VOID_RETURN;
3849 3850 3851 3852 3853 3854
}

void
NdbDictInterface::execCREATE_EVNT_REF(NdbApiSignal * signal,
				      LinearSectionPtr ptr[3])
{
3855
  DBUG_ENTER("NdbDictInterface::execCREATE_EVNT_REF");
3856

3857 3858 3859 3860 3861
  const CreateEvntRef* const ref=
    CAST_CONSTPTR(CreateEvntRef, signal->getDataPtr());
  m_error.code= ref->getErrorCode();
  DBUG_PRINT("error",("error=%d,line=%d,node=%d",ref->getErrorCode(),
		      ref->getErrorLine(),ref->getErrorNode()));
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
3862 3863
  if (m_error.code == CreateEvntRef::NotMaster)
    m_masterNodeId = ref->getMasterNode();
3864 3865
  m_waiter.signal(NO_WAIT);
  DBUG_VOID_RETURN;
3866 3867 3868 3869 3870 3871
}

void
NdbDictInterface::execSUB_STOP_CONF(NdbApiSignal * signal,
				      LinearSectionPtr ptr[3])
{
3872 3873 3874
  DBUG_ENTER("NdbDictInterface::execSUB_STOP_CONF");
  const SubStopConf * const subStopConf=
    CAST_CONSTPTR(SubStopConf, signal->getDataPtr());
3875

3876 3877 3878
  Uint32 subscriptionId = subStopConf->subscriptionId;
  Uint32 subscriptionKey = subStopConf->subscriptionKey;
  Uint32 subscriberData = subStopConf->subscriberData;
3879

3880 3881
  DBUG_PRINT("info",("subscriptionId=%d,subscriptionKey=%d,subscriberData=%d",
		     subscriptionId,subscriptionKey,subscriberData));
3882
  m_waiter.signal(NO_WAIT);
3883
  DBUG_VOID_RETURN;
3884 3885 3886 3887 3888 3889
}

void
NdbDictInterface::execSUB_STOP_REF(NdbApiSignal * signal,
				     LinearSectionPtr ptr[3])
{
3890
  DBUG_ENTER("NdbDictInterface::execSUB_STOP_REF");
3891 3892
  const SubStopRef * const subStopRef=
    CAST_CONSTPTR(SubStopRef, signal->getDataPtr());
3893

3894 3895 3896 3897
  Uint32 subscriptionId = subStopRef->subscriptionId;
  Uint32 subscriptionKey = subStopRef->subscriptionKey;
  Uint32 subscriberData = subStopRef->subscriberData;
  m_error.code= subStopRef->errorCode;
3898

3899 3900
  DBUG_PRINT("error",("subscriptionId=%d,subscriptionKey=%d,subscriberData=%d,error=%d",
		      subscriptionId,subscriptionKey,subscriberData,m_error.code));
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
3901 3902
  if (m_error.code == SubStopRef::NotMaster)
    m_masterNodeId = subStopRef->m_masterNodeId;
3903
  m_waiter.signal(NO_WAIT);
3904
  DBUG_VOID_RETURN;
3905 3906 3907 3908 3909 3910
}

void
NdbDictInterface::execSUB_START_CONF(NdbApiSignal * signal,
				     LinearSectionPtr ptr[3])
{
3911 3912 3913
  DBUG_ENTER("NdbDictInterface::execSUB_START_CONF");
  const SubStartConf * const subStartConf=
    CAST_CONSTPTR(SubStartConf, signal->getDataPtr());
3914

3915 3916
  Uint32 subscriptionId = subStartConf->subscriptionId;
  Uint32 subscriptionKey = subStartConf->subscriptionKey;
3917
  SubscriptionData::Part part = 
3918 3919
    (SubscriptionData::Part)subStartConf->part;
  Uint32 subscriberData = subStartConf->subscriberData;
3920 3921 3922

  switch(part) {
  case SubscriptionData::MetaData: {
3923 3924
    DBUG_PRINT("error",("SubscriptionData::MetaData"));
    m_error.code= 1;
3925 3926 3927
    break;
  }
  case SubscriptionData::TableData: {
3928
    DBUG_PRINT("info",("SubscriptionData::TableData"));
3929 3930 3931
    break;
  }
  default: {
3932 3933
    DBUG_PRINT("error",("wrong data"));
    m_error.code= 2;
3934 3935 3936
    break;
  }
  }
3937 3938
  DBUG_PRINT("info",("subscriptionId=%d,subscriptionKey=%d,subscriberData=%d",
		     subscriptionId,subscriptionKey,subscriberData));
3939
  m_waiter.signal(NO_WAIT);
3940
  DBUG_VOID_RETURN;
3941 3942 3943 3944 3945 3946
}

void
NdbDictInterface::execSUB_START_REF(NdbApiSignal * signal,
				    LinearSectionPtr ptr[3])
{
3947 3948 3949 3950
  DBUG_ENTER("NdbDictInterface::execSUB_START_REF");
  const SubStartRef * const subStartRef=
    CAST_CONSTPTR(SubStartRef, signal->getDataPtr());
  m_error.code= subStartRef->errorCode;
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
3951 3952
  if (m_error.code == SubStartRef::NotMaster)
    m_masterNodeId = subStartRef->m_masterNodeId;
3953 3954
  m_waiter.signal(NO_WAIT);
  DBUG_VOID_RETURN;
3955 3956 3957 3958 3959 3960 3961 3962
}

/*****************************************************************
 * Drop event
 */
int 
NdbDictionaryImpl::dropEvent(const char * eventName)
{
3963 3964
  DBUG_ENTER("NdbDictionaryImpl::dropEvent");
  DBUG_PRINT("info", ("name=%s", eventName));
3965

3966 3967 3968 3969 3970
  NdbEventImpl *evnt = getEvent(eventName); // allocated
  if (evnt == NULL) {
    if (m_error.code != 723 && // no such table
        m_error.code != 241)   // invalid table
      DBUG_RETURN(-1);
3971
    DBUG_PRINT("info", ("no table err=%d, drop by name alone", m_error.code));
3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002
    evnt = new NdbEventImpl();
    evnt->setName(eventName);
  }
  int ret = dropEvent(*evnt);
  delete evnt;  
  DBUG_RETURN(ret);
}

int
NdbDictionaryImpl::dropEvent(const NdbEventImpl& evnt)
{
  if (dropBlobEvents(evnt) != 0)
    return -1;
  if (m_receiver.dropEvent(evnt) != 0)
    return -1;
  return 0;
}

int
NdbDictionaryImpl::dropBlobEvents(const NdbEventImpl& evnt)
{
  DBUG_ENTER("NdbDictionaryImpl::dropBlobEvents");
  if (evnt.m_tableImpl != 0) {
    const NdbTableImpl& t = *evnt.m_tableImpl;
    Uint32 n = t.m_noOfBlobs;
    Uint32 i;
    for (i = 0; i < evnt.m_columns.size() && n > 0; i++) {
      const NdbColumnImpl& c = *evnt.m_columns[i];
      if (! c.getBlobType() || c.getPartSize() == 0)
        continue;
      n--;
4003 4004 4005 4006
      NdbEventImpl* blob_evnt = getBlobEvent(evnt, i);
      if (blob_evnt == NULL)
        continue;
      (void)dropEvent(*blob_evnt);
4007
      delete blob_evnt;
4008 4009
    }
  } else {
4010 4011
    // loop over MAX_ATTRIBUTES_IN_TABLE ...
    Uint32 i;
4012 4013 4014
    DBUG_PRINT("info", ("missing table definition, looping over "
                        "MAX_ATTRIBUTES_IN_TABLE(%d)",
                        MAX_ATTRIBUTES_IN_TABLE));
4015 4016 4017 4018 4019 4020 4021 4022 4023
    for (i = 0; i < MAX_ATTRIBUTES_IN_TABLE; i++) {
      char bename[MAX_TAB_NAME_SIZE];
      // XXX should get name from NdbBlob
      sprintf(bename, "NDB$BLOBEVENT_%s_%u", evnt.getName(), i);
      NdbEventImpl* bevnt = new NdbEventImpl();
      bevnt->setName(bename);
      (void)m_receiver.dropEvent(*bevnt);
      delete bevnt;
    }
4024 4025
  }
  DBUG_RETURN(0);
4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042
}

int
NdbDictInterface::dropEvent(const NdbEventImpl &evnt)
{
  NdbApiSignal tSignal(m_reference);
  tSignal.theReceiversBlockNumber = DBDICT;
  tSignal.theVerId_signalNumber   = GSN_DROP_EVNT_REQ;
  tSignal.theLength = DropEvntReq::SignalLength;
  
  DropEvntReq * const req = CAST_PTR(DropEvntReq, tSignal.getDataPtrSend());

  req->setUserRef(m_reference);
  req->setUserData(0);

  UtilBufferWriter w(m_buffer);

4043
  w.add(SimpleProperties::StringValue, evnt.m_name.c_str());
4044 4045 4046 4047 4048

  LinearSectionPtr ptr[1];
  ptr[0].p = (Uint32*)m_buffer.get_data();
  ptr[0].sz = (m_buffer.length()+3) >> 2;

4049 4050 4051 4052
  return dictSignal(&tSignal,ptr, 1,
		    0 /*use masternode id*/,
		    WAIT_CREATE_INDX_REQ,
		    -1, 100,
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
4053
		    0, -1);
4054
}
4055

4056 4057 4058 4059
void
NdbDictInterface::execDROP_EVNT_CONF(NdbApiSignal * signal,
				     LinearSectionPtr ptr[3])
{
4060
  DBUG_ENTER("NdbDictInterface::execDROP_EVNT_CONF");
4061
  m_waiter.signal(NO_WAIT);  
4062
  DBUG_VOID_RETURN;
4063 4064 4065 4066 4067 4068
}

void
NdbDictInterface::execDROP_EVNT_REF(NdbApiSignal * signal,
				    LinearSectionPtr ptr[3])
{
4069 4070 4071 4072
  DBUG_ENTER("NdbDictInterface::execDROP_EVNT_REF");
  const DropEvntRef* const ref=
    CAST_CONSTPTR(DropEvntRef, signal->getDataPtr());
  m_error.code= ref->getErrorCode();
4073

4074 4075
  DBUG_PRINT("info",("ErrorCode=%u Errorline=%u ErrorNode=%u",
	     ref->getErrorCode(), ref->getErrorLine(), ref->getErrorNode()));
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
4076 4077
  if (m_error.code == DropEvntRef::NotMaster)
    m_masterNodeId = ref->getMasterNode();
4078 4079
  m_waiter.signal(NO_WAIT);
  DBUG_VOID_RETURN;
4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091
}

/*****************************************************************
 * List objects or indexes
 */
int
NdbDictionaryImpl::listObjects(List& list, NdbDictionary::Object::Type type)
{
  ListTablesReq req;
  req.requestData = 0;
  req.setTableType(getKernelConstant(type, objectTypeMapping, 0));
  req.setListNames(true);
4092
  return m_receiver.listObjects(list, req.requestData, m_ndb.usingFullyQualifiedNames());
4093 4094 4095
}

int
joreland@mysql.com's avatar
joreland@mysql.com committed
4096
NdbDictionaryImpl::listIndexes(List& list, Uint32 indexId)
4097
{
4098
  ListTablesReq req;
4099
  req.requestData = 0;
joreland@mysql.com's avatar
joreland@mysql.com committed
4100
  req.setTableId(indexId);
4101 4102
  req.setListNames(true);
  req.setListIndexes(true);
4103
  return m_receiver.listObjects(list, req.requestData, m_ndb.usingFullyQualifiedNames());
4104 4105 4106 4107
}

int
NdbDictInterface::listObjects(NdbDictionary::Dictionary::List& list,
4108
			      Uint32 requestData, bool fullyQualifiedNames)
4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143
{
  NdbApiSignal tSignal(m_reference);
  ListTablesReq* const req = CAST_PTR(ListTablesReq, tSignal.getDataPtrSend());
  req->senderRef = m_reference;
  req->senderData = 0;
  req->requestData = requestData;
  tSignal.theReceiversBlockNumber = DBDICT;
  tSignal.theVerId_signalNumber = GSN_LIST_TABLES_REQ;
  tSignal.theLength = ListTablesReq::SignalLength;
  if (listObjects(&tSignal) != 0)
    return -1;
  // count
  const Uint32* data = (const Uint32*)m_buffer.get_data();
  const unsigned length = m_buffer.length() / 4;
  list.count = 0;
  bool ok = true;
  unsigned pos, count;
  pos = count = 0;
  while (pos < length) {
    // table id - name length - name
    pos++;
    if (pos >= length) {
      ok = false;
      break;
    }
    Uint32 n = (data[pos++] + 3) >> 2;
    pos += n;
    if (pos > length) {
      ok = false;
      break;
    }
    count++;
  }
  if (! ok) {
    // bad signal data
4144
    m_error.code= 4213;
4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159
    return -1;
  }
  list.count = count;
  list.elements = new NdbDictionary::Dictionary::List::Element[count];
  pos = count = 0;
  while (pos < length) {
    NdbDictionary::Dictionary::List::Element& element = list.elements[count];
    Uint32 d = data[pos++];
    element.id = ListTablesConf::getTableId(d);
    element.type = (NdbDictionary::Object::Type)
      getApiConstant(ListTablesConf::getTableType(d), objectTypeMapping, 0);
    element.state = (NdbDictionary::Object::State)
      getApiConstant(ListTablesConf::getTableState(d), objectStateMapping, 0);
    element.store = (NdbDictionary::Object::Store)
      getApiConstant(ListTablesConf::getTableStore(d), objectStoreMapping, 0);
knielsen@ymer.(none)'s avatar
knielsen@ymer.(none) committed
4160
    element.temp = ListTablesConf::getTableTemp(d);
4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171
    // table or index name
    Uint32 n = (data[pos++] + 3) >> 2;
    BaseString databaseName;
    BaseString schemaName;
    BaseString objectName;
    if ((element.type == NdbDictionary::Object::UniqueHashIndex) ||
	(element.type == NdbDictionary::Object::OrderedIndex)) {
      char * indexName = new char[n << 2];
      memcpy(indexName, &data[pos], n << 2);
      databaseName = Ndb::getDatabaseFromInternalName(indexName);
      schemaName = Ndb::getSchemaFromInternalName(indexName);
4172
      objectName = BaseString(Ndb::externalizeIndexName(indexName, fullyQualifiedNames));
4173 4174 4175 4176 4177 4178 4179
      delete [] indexName;
    } else if ((element.type == NdbDictionary::Object::SystemTable) || 
	       (element.type == NdbDictionary::Object::UserTable)) {
      char * tableName = new char[n << 2];
      memcpy(tableName, &data[pos], n << 2);
      databaseName = Ndb::getDatabaseFromInternalName(tableName);
      schemaName = Ndb::getSchemaFromInternalName(tableName);
4180
      objectName = BaseString(Ndb::externalizeTableName(tableName, fullyQualifiedNames));
4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207
      delete [] tableName;
    }
    else {
      char * otherName = new char[n << 2];
      memcpy(otherName, &data[pos], n << 2);
      objectName = BaseString(otherName);
      delete [] otherName;
    }
    element.database = new char[databaseName.length() + 1]; 
    strcpy(element.database, databaseName.c_str());
    element.schema = new char[schemaName.length() + 1]; 
    strcpy(element.schema, schemaName.c_str());
    element.name = new char[objectName.length() + 1]; 
    strcpy(element.name, objectName.c_str());
    pos += n;
    count++;
  }
  return 0;
}

int
NdbDictInterface::listObjects(NdbApiSignal* signal)
{
  const Uint32 RETRIES = 100;
  for (Uint32 i = 0; i < RETRIES; i++) {
    m_buffer.clear();
    // begin protected
mronstrom@mysql.com[mikron]'s avatar
mronstrom@mysql.com[mikron] committed
4208 4209 4210 4211 4212 4213 4214
    /*
      The PollGuard has an implicit call of unlock_and_signal through the
      ~PollGuard method. This method is called implicitly by the compiler
      in all places where the object is out of context due to a return,
      break, continue or simply end of statement block
    */
    PollGuard poll_guard(m_transporter, &m_waiter, refToBlock(m_reference));
4215 4216
    Uint16 aNodeId = m_transporter->get_an_alive_node();
    if (aNodeId == 0) {
4217
      m_error.code= 4009;
4218 4219 4220 4221 4222
      return -1;
    }
    if (m_transporter->sendSignal(signal, aNodeId) != 0) {
      continue;
    }
4223
    m_error.code= 0;
4224
    int ret_val= poll_guard.wait_n_unlock(DICT_WAITFOR_TIMEOUT,
mronstrom@mysql.com[mikron]'s avatar
mronstrom@mysql.com[mikron] committed
4225
                                          aNodeId, WAIT_LIST_TABLES_CONF);
4226
    // end protected
mronstrom@mysql.com[mikron]'s avatar
mronstrom@mysql.com[mikron] committed
4227
    if (ret_val == 0 && m_error.code == 0)
4228
      return 0;
mronstrom@mysql.com[mikron]'s avatar
mronstrom@mysql.com[mikron] committed
4229
    if (ret_val == -2) //WAIT_NODE_FAILURE
4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247
      continue;
    return -1;
  }
  return -1;
}

void
NdbDictInterface::execLIST_TABLES_CONF(NdbApiSignal* signal,
				       LinearSectionPtr ptr[3])
{
  const unsigned off = ListTablesConf::HeaderLength;
  const unsigned len = (signal->getLength() - off);
  m_buffer.append(signal->getDataPtr() + off, len << 2);
  if (signal->getLength() < ListTablesConf::SignalLength) {
    // last signal has less than full length
    m_waiter.signal(NO_WAIT);
  }
}
4248

4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283
int
NdbDictionaryImpl::forceGCPWait()
{
  return m_receiver.forceGCPWait();
}

int
NdbDictInterface::forceGCPWait()
{
  NdbApiSignal tSignal(m_reference);
  WaitGCPReq* const req = CAST_PTR(WaitGCPReq, tSignal.getDataPtrSend());
  req->senderRef = m_reference;
  req->senderData = 0;
  req->requestType = WaitGCPReq::CompleteForceStart;
  tSignal.theReceiversBlockNumber = DBDIH;
  tSignal.theVerId_signalNumber = GSN_WAIT_GCP_REQ;
  tSignal.theLength = WaitGCPReq::SignalLength;

  const Uint32 RETRIES = 100;
  for (Uint32 i = 0; i < RETRIES; i++)
  {
    m_transporter->lock_mutex();
    Uint16 aNodeId = m_transporter->get_an_alive_node();
    if (aNodeId == 0) {
      m_error.code= 4009;
      m_transporter->unlock_mutex();
      return -1;
    }
    if (m_transporter->sendSignal(&tSignal, aNodeId) != 0) {
      m_transporter->unlock_mutex();
      continue;
    }
    m_error.code= 0;
    m_waiter.m_node = aNodeId;
    m_waiter.m_state = WAIT_LIST_TABLES_CONF;
4284
    m_waiter.wait(DICT_WAITFOR_TIMEOUT);
4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304
    m_transporter->unlock_mutex();    
    return 0;
  }
  return -1;
}

void
NdbDictInterface::execWAIT_GCP_CONF(NdbApiSignal* signal,
				    LinearSectionPtr ptr[3])
{
  m_waiter.signal(NO_WAIT);
}

void
NdbDictInterface::execWAIT_GCP_REF(NdbApiSignal* signal,
				    LinearSectionPtr ptr[3])
{
  m_waiter.signal(NO_WAIT);
}

4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328
NdbFilegroupImpl::NdbFilegroupImpl(NdbDictionary::Object::Type t)
  : NdbDictObjectImpl(t)
{
  m_extent_size = 0;
  m_undo_buffer_size = 0;
  m_logfile_group_id = ~0;
  m_logfile_group_version = ~0;
}

NdbTablespaceImpl::NdbTablespaceImpl() : 
  NdbDictionary::Tablespace(* this), 
  NdbFilegroupImpl(NdbDictionary::Object::Tablespace), m_facade(this)
{
}

NdbTablespaceImpl::NdbTablespaceImpl(NdbDictionary::Tablespace & f) : 
  NdbDictionary::Tablespace(* this), 
  NdbFilegroupImpl(NdbDictionary::Object::Tablespace), m_facade(&f)
{
}

NdbTablespaceImpl::~NdbTablespaceImpl(){
}

4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346
void
NdbTablespaceImpl::assign(const NdbTablespaceImpl& org)
{
  m_id = org.m_id;
  m_version = org.m_version;
  m_status = org.m_status;
  m_type = org.m_type;

  m_name.assign(org.m_name);
  m_grow_spec = org.m_grow_spec;
  m_extent_size = org.m_extent_size;
  m_undo_free_words = org.m_undo_free_words;
  m_logfile_group_id = org.m_logfile_group_id;
  m_logfile_group_version = org.m_logfile_group_version;
  m_logfile_group_name.assign(org.m_logfile_group_name);
  m_undo_free_words = org.m_undo_free_words;
}

4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361
NdbLogfileGroupImpl::NdbLogfileGroupImpl() : 
  NdbDictionary::LogfileGroup(* this), 
  NdbFilegroupImpl(NdbDictionary::Object::LogfileGroup), m_facade(this)
{
}

NdbLogfileGroupImpl::NdbLogfileGroupImpl(NdbDictionary::LogfileGroup & f) : 
  NdbDictionary::LogfileGroup(* this), 
  NdbFilegroupImpl(NdbDictionary::Object::LogfileGroup), m_facade(&f)
{
}

NdbLogfileGroupImpl::~NdbLogfileGroupImpl(){
}

4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379
void
NdbLogfileGroupImpl::assign(const NdbLogfileGroupImpl& org)
{
  m_id = org.m_id;
  m_version = org.m_version;
  m_status = org.m_status;
  m_type = org.m_type;

  m_name.assign(org.m_name);
  m_grow_spec = org.m_grow_spec;
  m_extent_size = org.m_extent_size;
  m_undo_free_words = org.m_undo_free_words;
  m_logfile_group_id = org.m_logfile_group_id;
  m_logfile_group_version = org.m_logfile_group_version;
  m_logfile_group_name.assign(org.m_logfile_group_name);
  m_undo_free_words = org.m_undo_free_words;
}

4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403
NdbFileImpl::NdbFileImpl(NdbDictionary::Object::Type t)
  : NdbDictObjectImpl(t)
{
  m_size = 0;
  m_free = 0;
  m_filegroup_id = ~0;
  m_filegroup_version = ~0;
}

NdbDatafileImpl::NdbDatafileImpl() : 
  NdbDictionary::Datafile(* this), 
  NdbFileImpl(NdbDictionary::Object::Datafile), m_facade(this)
{
}

NdbDatafileImpl::NdbDatafileImpl(NdbDictionary::Datafile & f) : 
  NdbDictionary::Datafile(* this), 
  NdbFileImpl(NdbDictionary::Object::Datafile), m_facade(&f)
{
}

NdbDatafileImpl::~NdbDatafileImpl(){
}

4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419
void
NdbDatafileImpl::assign(const NdbDatafileImpl& org)
{
  m_id = org.m_id;
  m_version = org.m_version;
  m_status = org.m_status;
  m_type = org.m_type;

  m_size = org.m_size;
  m_free = org.m_free;
  m_filegroup_id = org.m_filegroup_id;
  m_filegroup_version = org.m_filegroup_version;
  m_path.assign(org.m_path);
  m_filegroup_name.assign(org.m_filegroup_name);
}

4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434
NdbUndofileImpl::NdbUndofileImpl() : 
  NdbDictionary::Undofile(* this), 
  NdbFileImpl(NdbDictionary::Object::Undofile), m_facade(this)
{
}

NdbUndofileImpl::NdbUndofileImpl(NdbDictionary::Undofile & f) : 
  NdbDictionary::Undofile(* this), 
  NdbFileImpl(NdbDictionary::Object::Undofile), m_facade(&f)
{
}

NdbUndofileImpl::~NdbUndofileImpl(){
}

4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450
void
NdbUndofileImpl::assign(const NdbUndofileImpl& org)
{
  m_id = org.m_id;
  m_version = org.m_version;
  m_status = org.m_status;
  m_type = org.m_type;

  m_size = org.m_size;
  m_free = org.m_free;
  m_filegroup_id = org.m_filegroup_id;
  m_filegroup_version = org.m_filegroup_version;
  m_path.assign(org.m_path);
  m_filegroup_name.assign(org.m_filegroup_name);
}

4451
int 
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
4452 4453 4454 4455 4456
NdbDictionaryImpl::createDatafile(const NdbDatafileImpl & file, 
				  bool force,
				  NdbDictObjectImpl* obj)
  
{
4457 4458 4459 4460 4461
  DBUG_ENTER("NdbDictionaryImpl::createDatafile");
  NdbFilegroupImpl tmp(NdbDictionary::Object::Tablespace);
  if(file.m_filegroup_version != ~(Uint32)0){
    tmp.m_id = file.m_filegroup_id;
    tmp.m_version = file.m_filegroup_version;
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
4462
    DBUG_RETURN(m_receiver.create_file(file, tmp, force, obj));
4463 4464 4465 4466 4467
  }
  
  
  if(m_receiver.get_filegroup(tmp, NdbDictionary::Object::Tablespace,
			      file.m_filegroup_name.c_str()) == 0){
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
4468
    DBUG_RETURN(m_receiver.create_file(file, tmp, force, obj));
4469 4470 4471 4472 4473 4474 4475 4476 4477 4478
  }
  DBUG_RETURN(-1); 
}

int
NdbDictionaryImpl::dropDatafile(const NdbDatafileImpl & file){
  return m_receiver.drop_file(file);
}

int
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
4479 4480 4481 4482
NdbDictionaryImpl::createUndofile(const NdbUndofileImpl & file, 
				  bool force,
				  NdbDictObjectImpl* obj)
{
4483 4484 4485 4486 4487
  DBUG_ENTER("NdbDictionaryImpl::createUndofile");
  NdbFilegroupImpl tmp(NdbDictionary::Object::LogfileGroup);
  if(file.m_filegroup_version != ~(Uint32)0){
    tmp.m_id = file.m_filegroup_id;
    tmp.m_version = file.m_filegroup_version;
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
4488
    DBUG_RETURN(m_receiver.create_file(file, tmp, force, obj));
4489 4490 4491 4492 4493
  }
  
  
  if(m_receiver.get_filegroup(tmp, NdbDictionary::Object::LogfileGroup,
			      file.m_filegroup_name.c_str()) == 0){
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
4494
    DBUG_RETURN(m_receiver.create_file(file, tmp, force, obj));
4495 4496 4497 4498 4499 4500
  }
  DBUG_PRINT("info", ("Failed to find filegroup"));
  DBUG_RETURN(-1);
}

int
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
4501 4502
NdbDictionaryImpl::dropUndofile(const NdbUndofileImpl & file)
{
4503 4504 4505 4506
  return m_receiver.drop_file(file);
}

int
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
4507 4508 4509 4510
NdbDictionaryImpl::createTablespace(const NdbTablespaceImpl & fg,
				    NdbDictObjectImpl* obj)
{
  return m_receiver.create_filegroup(fg, obj);
4511 4512 4513
}

int
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
4514 4515
NdbDictionaryImpl::dropTablespace(const NdbTablespaceImpl & fg)
{
4516 4517 4518 4519
  return m_receiver.drop_filegroup(fg);
}

int
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
4520 4521 4522 4523
NdbDictionaryImpl::createLogfileGroup(const NdbLogfileGroupImpl & fg,
				      NdbDictObjectImpl* obj)
{
  return m_receiver.create_filegroup(fg, obj);
4524 4525 4526
}

int
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
4527 4528
NdbDictionaryImpl::dropLogfileGroup(const NdbLogfileGroupImpl & fg)
{
4529 4530 4531 4532 4533 4534
  return m_receiver.drop_filegroup(fg);
}

int
NdbDictInterface::create_file(const NdbFileImpl & file,
			      const NdbFilegroupImpl & group,
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
4535 4536 4537
			      bool overwrite,
			      NdbDictObjectImpl* obj)
{
4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574
  DBUG_ENTER("NdbDictInterface::create_file"); 
  UtilBufferWriter w(m_buffer);
  DictFilegroupInfo::File f; f.init();
  snprintf(f.FileName, sizeof(f.FileName), file.m_path.c_str());
  f.FileType = file.m_type;
  f.FilegroupId = group.m_id;
  f.FilegroupVersion = group.m_version;
  f.FileSizeHi = (file.m_size >> 32);
  f.FileSizeLo = (file.m_size & 0xFFFFFFFF);
  
  SimpleProperties::UnpackStatus s;
  s = SimpleProperties::pack(w, 
			     &f,
			     DictFilegroupInfo::FileMapping, 
			     DictFilegroupInfo::FileMappingSize, true);
  
  if(s != SimpleProperties::Eof){
    abort();
  }
  
  NdbApiSignal tSignal(m_reference);
  tSignal.theReceiversBlockNumber = DBDICT;
  tSignal.theVerId_signalNumber = GSN_CREATE_FILE_REQ;
  tSignal.theLength = CreateFileReq::SignalLength;
  
  CreateFileReq* req = CAST_PTR(CreateFileReq, tSignal.getDataPtrSend());
  req->senderRef = m_reference;
  req->senderData = 0;
  req->objType = file.m_type;
  req->requestInfo = 0;
  if (overwrite)
    req->requestInfo |= CreateFileReq::ForceCreateFile;
  
  LinearSectionPtr ptr[3];
  ptr[0].p = (Uint32*)m_buffer.get_data();
  ptr[0].sz = m_buffer.length() / 4;

jonas@perch.ndb.mysql.com's avatar
ndb -  
jonas@perch.ndb.mysql.com committed
4575
  int err[] = { CreateFileRef::Busy, CreateFileRef::NotMaster, 0};
4576 4577 4578 4579
  /*
    Send signal without time-out since creating files can take a very long
    time if the file is very big.
  */
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593
  int ret = dictSignal(&tSignal, ptr, 1,
		       0, // master
		       WAIT_CREATE_INDX_REQ,
		       -1, 100,
		       err);

  if (ret == 0 && obj)
  {
    Uint32* data = (Uint32*)m_buffer.get_data();
    obj->m_id = data[0];
    obj->m_version = data[1];
  }

  DBUG_RETURN(ret);
4594 4595 4596 4597
}

void
NdbDictInterface::execCREATE_FILE_CONF(NdbApiSignal * signal,
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
4598
				       LinearSectionPtr ptr[3])
4599
{
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
4600 4601 4602 4603 4604 4605 4606
  const CreateFileConf* conf=
    CAST_CONSTPTR(CreateFileConf, signal->getDataPtr());
  m_buffer.grow(4 * 2); // 2 words
  Uint32* data = (Uint32*)m_buffer.get_data();
  data[0] = conf->fileId;
  data[1] = conf->fileVersion;
  
4607 4608 4609 4610 4611
  m_waiter.signal(NO_WAIT);  
}

void
NdbDictInterface::execCREATE_FILE_REF(NdbApiSignal * signal,
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
4612
				      LinearSectionPtr ptr[3])
4613 4614 4615 4616 4617 4618 4619 4620 4621
{
  const CreateFileRef* ref = 
    CAST_CONSTPTR(CreateFileRef, signal->getDataPtr());
  m_error.code = ref->errorCode;
  m_masterNodeId = ref->masterNodeId;
  m_waiter.signal(NO_WAIT);  
}

int
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
4622 4623
NdbDictInterface::drop_file(const NdbFileImpl & file)
{
4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635
  DBUG_ENTER("NdbDictInterface::drop_file");
  NdbApiSignal tSignal(m_reference);
  tSignal.theReceiversBlockNumber = DBDICT;
  tSignal.theVerId_signalNumber = GSN_DROP_FILE_REQ;
  tSignal.theLength = DropFileReq::SignalLength;
  
  DropFileReq* req = CAST_PTR(DropFileReq, tSignal.getDataPtrSend());
  req->senderRef = m_reference;
  req->senderData = 0;
  req->file_id = file.m_id;
  req->file_version = file.m_version;

jonas@perch.ndb.mysql.com's avatar
ndb -  
jonas@perch.ndb.mysql.com committed
4636
  int err[] = { DropFileRef::Busy, DropFileRef::NotMaster, 0};
4637 4638 4639
  DBUG_RETURN(dictSignal(&tSignal, 0, 0,
	                 0, // master
		         WAIT_CREATE_INDX_REQ,
4640
		         DICT_WAITFOR_TIMEOUT, 100,
4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662
		         err));
}

void
NdbDictInterface::execDROP_FILE_CONF(NdbApiSignal * signal,
					    LinearSectionPtr ptr[3])
{
  m_waiter.signal(NO_WAIT);  
}

void
NdbDictInterface::execDROP_FILE_REF(NdbApiSignal * signal,
					   LinearSectionPtr ptr[3])
{
  const DropFileRef* ref = 
    CAST_CONSTPTR(DropFileRef, signal->getDataPtr());
  m_error.code = ref->errorCode;
  m_masterNodeId = ref->masterNodeId;
  m_waiter.signal(NO_WAIT);  
}

int
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
4663 4664 4665
NdbDictInterface::create_filegroup(const NdbFilegroupImpl & group,
				   NdbDictObjectImpl* obj)
{
4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733
  DBUG_ENTER("NdbDictInterface::create_filegroup");
  UtilBufferWriter w(m_buffer);
  DictFilegroupInfo::Filegroup fg; fg.init();
  snprintf(fg.FilegroupName, sizeof(fg.FilegroupName), group.m_name.c_str());
  switch(group.m_type){
  case NdbDictionary::Object::Tablespace:
  {
    fg.FilegroupType = DictTabInfo::Tablespace;
    //fg.TS_DataGrow = group.m_grow_spec;
    fg.TS_ExtentSize = group.m_extent_size;

    if(group.m_logfile_group_version != ~(Uint32)0)
    {
      fg.TS_LogfileGroupId = group.m_logfile_group_id;
      fg.TS_LogfileGroupVersion = group.m_logfile_group_version;
    }
    else 
    {
      NdbLogfileGroupImpl tmp;
      if(get_filegroup(tmp, NdbDictionary::Object::LogfileGroup, 
		       group.m_logfile_group_name.c_str()) == 0)
      {
	fg.TS_LogfileGroupId = tmp.m_id;
	fg.TS_LogfileGroupVersion = tmp.m_version;
      }
      else // error set by get filegroup
      {
	DBUG_RETURN(-1);
      }
    }
  }
  break;
  case NdbDictionary::Object::LogfileGroup:
    fg.LF_UndoBufferSize = group.m_undo_buffer_size;
    fg.FilegroupType = DictTabInfo::LogfileGroup;
    //fg.LF_UndoGrow = group.m_grow_spec;
    break;
  default:
    abort();
    DBUG_RETURN(-1);
  };
  
  SimpleProperties::UnpackStatus s;
  s = SimpleProperties::pack(w, 
			     &fg,
			     DictFilegroupInfo::Mapping, 
			     DictFilegroupInfo::MappingSize, true);
  
  if(s != SimpleProperties::Eof){
    abort();
  }
  
  NdbApiSignal tSignal(m_reference);
  tSignal.theReceiversBlockNumber = DBDICT;
  tSignal.theVerId_signalNumber = GSN_CREATE_FILEGROUP_REQ;
  tSignal.theLength = CreateFilegroupReq::SignalLength;
  
  CreateFilegroupReq* req = 
    CAST_PTR(CreateFilegroupReq, tSignal.getDataPtrSend());
  req->senderRef = m_reference;
  req->senderData = 0;
  req->objType = fg.FilegroupType;
  
  LinearSectionPtr ptr[3];
  ptr[0].p = (Uint32*)m_buffer.get_data();
  ptr[0].sz = m_buffer.length() / 4;

  int err[] = { CreateFilegroupRef::Busy, CreateFilegroupRef::NotMaster, 0};
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747
  int ret = dictSignal(&tSignal, ptr, 1,
		       0, // master
		       WAIT_CREATE_INDX_REQ,
		       DICT_WAITFOR_TIMEOUT, 100,
		       err);
  
  if (ret == 0 && obj)
  {
    Uint32* data = (Uint32*)m_buffer.get_data();
    obj->m_id = data[0];
    obj->m_version = data[1];
  }
  
  DBUG_RETURN(ret);
4748 4749 4750 4751 4752 4753
}

void
NdbDictInterface::execCREATE_FILEGROUP_CONF(NdbApiSignal * signal,
					    LinearSectionPtr ptr[3])
{
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
4754 4755 4756 4757 4758 4759
  const CreateFilegroupConf* conf=
    CAST_CONSTPTR(CreateFilegroupConf, signal->getDataPtr());
  m_buffer.grow(4 * 2); // 2 words
  Uint32* data = (Uint32*)m_buffer.get_data();
  data[0] = conf->filegroupId;
  data[1] = conf->filegroupVersion;
4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774
  m_waiter.signal(NO_WAIT);  
}

void
NdbDictInterface::execCREATE_FILEGROUP_REF(NdbApiSignal * signal,
					   LinearSectionPtr ptr[3])
{
  const CreateFilegroupRef* ref = 
    CAST_CONSTPTR(CreateFilegroupRef, signal->getDataPtr());
  m_error.code = ref->errorCode;
  m_masterNodeId = ref->masterNodeId;
  m_waiter.signal(NO_WAIT);  
}

int
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
4775 4776
NdbDictInterface::drop_filegroup(const NdbFilegroupImpl & group)
{
4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792
  DBUG_ENTER("NdbDictInterface::drop_filegroup");
  NdbApiSignal tSignal(m_reference);
  tSignal.theReceiversBlockNumber = DBDICT;
  tSignal.theVerId_signalNumber = GSN_DROP_FILEGROUP_REQ;
  tSignal.theLength = DropFilegroupReq::SignalLength;
  
  DropFilegroupReq* req = CAST_PTR(DropFilegroupReq, tSignal.getDataPtrSend());
  req->senderRef = m_reference;
  req->senderData = 0;
  req->filegroup_id = group.m_id;
  req->filegroup_version = group.m_version;
  
  int err[] = { DropFilegroupRef::Busy, DropFilegroupRef::NotMaster, 0};
  DBUG_RETURN(dictSignal(&tSignal, 0, 0,
                         0, // master
		         WAIT_CREATE_INDX_REQ,
4793
		         DICT_WAITFOR_TIMEOUT, 100,
4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819
		         err));
}

void
NdbDictInterface::execDROP_FILEGROUP_CONF(NdbApiSignal * signal,
					    LinearSectionPtr ptr[3])
{
  m_waiter.signal(NO_WAIT);  
}

void
NdbDictInterface::execDROP_FILEGROUP_REF(NdbApiSignal * signal,
					   LinearSectionPtr ptr[3])
{
  const DropFilegroupRef* ref = 
    CAST_CONSTPTR(DropFilegroupRef, signal->getDataPtr());
  m_error.code = ref->errorCode;
  m_masterNodeId = ref->masterNodeId;
  m_waiter.signal(NO_WAIT);  
}


int
NdbDictInterface::get_filegroup(NdbFilegroupImpl & dst,
				NdbDictionary::Object::Type type,
				const char * name){
4820
  DBUG_ENTER("NdbDictInterface::get_filegroup");
4821 4822
  NdbApiSignal tSignal(m_reference);
  GetTabInfoReq * req = CAST_PTR(GetTabInfoReq, tSignal.getDataPtrSend());
4823

4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837
  size_t strLen = strlen(name) + 1;

  req->senderRef = m_reference;
  req->senderData = 0;
  req->requestType = 
    GetTabInfoReq::RequestByName | GetTabInfoReq::LongSignalConf;
  req->tableNameLen = strLen;
  tSignal.theReceiversBlockNumber = DBDICT;
  tSignal.theVerId_signalNumber   = GSN_GET_TABINFOREQ;
  tSignal.theLength = GetTabInfoReq::SignalLength;

  LinearSectionPtr ptr[1];
  ptr[0].p  = (Uint32*)name;
  ptr[0].sz = (strLen + 3)/4;
jonas@perch.ndb.mysql.com's avatar
ndb -  
jonas@perch.ndb.mysql.com committed
4838 4839 4840 4841 4842 4843 4844 4845
  
#ifndef IGNORE_VALGRIND_WARNINGS
  if (strLen & 3)
  {
    Uint32 pad = 0;
    m_buffer.clear();
    m_buffer.append(name, strLen);
    m_buffer.append(&pad, 4);
jonas@perch.ndb.mysql.com's avatar
ndb -  
jonas@perch.ndb.mysql.com committed
4846
    ptr[0].p = (Uint32*)m_buffer.get_data();
jonas@perch.ndb.mysql.com's avatar
ndb -  
jonas@perch.ndb.mysql.com committed
4847 4848 4849
  }
#endif
  
4850 4851 4852
  int r = dictSignal(&tSignal, ptr, 1,
		     -1, // any node
		     WAIT_GET_TAB_INFO_REQ,
4853
		     DICT_WAITFOR_TIMEOUT, 100);
4854 4855
  if (r)
  {
jonas@perch.ndb.mysql.com's avatar
ndb -  
jonas@perch.ndb.mysql.com committed
4856 4857 4858
    dst.m_id = -1;
    dst.m_version = ~0;
    
4859 4860 4861
    DBUG_PRINT("info", ("get_filegroup failed dictSignal"));
    DBUG_RETURN(-1);
  }
4862 4863 4864

  m_error.code = parseFilegroupInfo(dst,
				    (Uint32*)m_buffer.get_data(),
4865
				    m_buffer.length() / 4);
4866

4867 4868 4869 4870 4871 4872 4873
  if(m_error.code)
  {
    DBUG_PRINT("info", ("get_filegroup failed parseFilegroupInfo %d",
                         m_error.code));
    DBUG_RETURN(m_error.code);
  }

4874 4875 4876 4877 4878 4879 4880 4881 4882
  if(dst.m_type == NdbDictionary::Object::Tablespace)
  {
    NdbDictionary::LogfileGroup tmp;
    get_filegroup(NdbLogfileGroupImpl::getImpl(tmp),
		  NdbDictionary::Object::LogfileGroup,
		  dst.m_logfile_group_id);
    dst.m_logfile_group_name.assign(tmp.getName());
  }
  
4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918
  if(dst.m_type == type)
  {
    DBUG_RETURN(0);
  }
  DBUG_PRINT("info", ("get_filegroup failed no such filegroup"));
  DBUG_RETURN(m_error.code = GetTabInfoRef::TableNotDefined);
}

int
NdbDictInterface::parseFilegroupInfo(NdbFilegroupImpl &dst,
				     const Uint32 * data, Uint32 len)
  
{
  SimplePropertiesLinearReader it(data, len);

  SimpleProperties::UnpackStatus status;
  DictFilegroupInfo::Filegroup fg; fg.init();
  status = SimpleProperties::unpack(it, &fg, 
				    DictFilegroupInfo::Mapping, 
				    DictFilegroupInfo::MappingSize, 
				    true, true);
  
  if(status != SimpleProperties::Eof){
    return CreateFilegroupRef::InvalidFormat;
  }

  dst.m_id = fg.FilegroupId;
  dst.m_version = fg.FilegroupVersion;
  dst.m_type = (NdbDictionary::Object::Type)fg.FilegroupType;
  dst.m_status = NdbDictionary::Object::Retrieved;
  
  dst.m_name.assign(fg.FilegroupName);
  dst.m_extent_size = fg.TS_ExtentSize;
  dst.m_undo_buffer_size = fg.LF_UndoBufferSize;
  dst.m_logfile_group_id = fg.TS_LogfileGroupId;
  dst.m_logfile_group_version = fg.TS_LogfileGroupVersion;
4919 4920 4921
  dst.m_undo_free_words= ((Uint64)fg.LF_UndoFreeWordsHi << 32)
    | (fg.LF_UndoFreeWordsLo);

4922 4923 4924
  return 0;
}

4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944
int
NdbDictInterface::get_filegroup(NdbFilegroupImpl & dst,
				NdbDictionary::Object::Type type,
				Uint32 id){
  DBUG_ENTER("NdbDictInterface::get_filegroup");
  NdbApiSignal tSignal(m_reference);
  GetTabInfoReq * req = CAST_PTR(GetTabInfoReq, tSignal.getDataPtrSend());

  req->senderRef = m_reference;
  req->senderData = 0;
  req->requestType =
    GetTabInfoReq::RequestById | GetTabInfoReq::LongSignalConf;
  req->tableId = id;
  tSignal.theReceiversBlockNumber = DBDICT;
  tSignal.theVerId_signalNumber   = GSN_GET_TABINFOREQ;
  tSignal.theLength = GetTabInfoReq::SignalLength;

  int r = dictSignal(&tSignal, NULL, 1,
		     -1, // any node
		     WAIT_GET_TAB_INFO_REQ,
4945
		     DICT_WAITFOR_TIMEOUT, 100);
4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970
  if (r)
  {
    DBUG_PRINT("info", ("get_filegroup failed dictSignal"));
    DBUG_RETURN(-1);
  }

  m_error.code = parseFilegroupInfo(dst,
				    (Uint32*)m_buffer.get_data(),
				    m_buffer.length() / 4);

  if(m_error.code)
  {
    DBUG_PRINT("info", ("get_filegroup failed parseFilegroupInfo %d",
                         m_error.code));
    DBUG_RETURN(m_error.code);
  }

  if(dst.m_type == type)
  {
    DBUG_RETURN(0);
  }
  DBUG_PRINT("info", ("get_filegroup failed no such filegroup"));
  DBUG_RETURN(m_error.code = GetTabInfoRef::TableNotDefined);
}

4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993
int
NdbDictInterface::get_file(NdbFileImpl & dst,
			   NdbDictionary::Object::Type type,
			   int node,
			   const char * name){
  DBUG_ENTER("NdbDictInterface::get_file");
  NdbApiSignal tSignal(m_reference);
  GetTabInfoReq * req = CAST_PTR(GetTabInfoReq, tSignal.getDataPtrSend());

  size_t strLen = strlen(name) + 1;

  req->senderRef = m_reference;
  req->senderData = 0;
  req->requestType =
    GetTabInfoReq::RequestByName | GetTabInfoReq::LongSignalConf;
  req->tableNameLen = strLen;
  tSignal.theReceiversBlockNumber = DBDICT;
  tSignal.theVerId_signalNumber   = GSN_GET_TABINFOREQ;
  tSignal.theLength = GetTabInfoReq::SignalLength;

  LinearSectionPtr ptr[1];
  ptr[0].p  = (Uint32*)name;
  ptr[0].sz = (strLen + 3)/4;
jonas@perch.ndb.mysql.com's avatar
ndb -  
jonas@perch.ndb.mysql.com committed
4994 4995 4996 4997 4998 4999 5000 5001
  
#ifndef IGNORE_VALGRIND_WARNINGS
  if (strLen & 3)
  {
    Uint32 pad = 0;
    m_buffer.clear();
    m_buffer.append(name, strLen);
    m_buffer.append(&pad, 4);
jonas@perch.ndb.mysql.com's avatar
ndb -  
jonas@perch.ndb.mysql.com committed
5002
    ptr[0].p = (Uint32*)m_buffer.get_data();
jonas@perch.ndb.mysql.com's avatar
ndb -  
jonas@perch.ndb.mysql.com committed
5003 5004 5005
  }
#endif
  
5006 5007 5008
  int r = dictSignal(&tSignal, ptr, 1,
		     node,
		     WAIT_GET_TAB_INFO_REQ,
5009
		     DICT_WAITFOR_TIMEOUT, 100);
5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026
  if (r)
  {
    DBUG_PRINT("info", ("get_file failed dictSignal"));
    DBUG_RETURN(-1);
  }

  m_error.code = parseFileInfo(dst,
			       (Uint32*)m_buffer.get_data(),
			       m_buffer.length() / 4);

  if(m_error.code)
  {
    DBUG_PRINT("info", ("get_file failed parseFileInfo %d",
                         m_error.code));
    DBUG_RETURN(m_error.code);
  }

5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046
  if(dst.m_type == NdbDictionary::Object::Undofile)
  {
    NdbDictionary::LogfileGroup tmp;
    get_filegroup(NdbLogfileGroupImpl::getImpl(tmp),
		  NdbDictionary::Object::LogfileGroup,
		  dst.m_filegroup_id);
    dst.m_filegroup_name.assign(tmp.getName());
  }
  else if(dst.m_type == NdbDictionary::Object::Datafile)
  {
    NdbDictionary::Tablespace tmp;
    get_filegroup(NdbTablespaceImpl::getImpl(tmp),
		  NdbDictionary::Object::Tablespace,
		  dst.m_filegroup_id);
    dst.m_filegroup_name.assign(tmp.getName());
    dst.m_free *= tmp.getExtentSize();
  }
  else
    dst.m_filegroup_name.assign("Not Yet Implemented");
  
5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072
  if(dst.m_type == type)
  {
    DBUG_RETURN(0);
  }
  DBUG_PRINT("info", ("get_file failed no such file"));
  DBUG_RETURN(m_error.code = GetTabInfoRef::TableNotDefined);
}

int
NdbDictInterface::parseFileInfo(NdbFileImpl &dst,
				const Uint32 * data, Uint32 len)
{
  SimplePropertiesLinearReader it(data, len);

  SimpleProperties::UnpackStatus status;
  DictFilegroupInfo::File f; f.init();
  status = SimpleProperties::unpack(it, &f,
				    DictFilegroupInfo::FileMapping,
				    DictFilegroupInfo::FileMappingSize,
				    true, true);

  if(status != SimpleProperties::Eof){
    return CreateFilegroupRef::InvalidFormat;
  }

  dst.m_type= (NdbDictionary::Object::Type)f.FileType;
jonas@perch.ndb.mysql.com's avatar
ndb -  
jonas@perch.ndb.mysql.com committed
5073 5074
  dst.m_id= f.FileId;
  dst.m_version = f.FileVersion;
5075 5076 5077

  dst.m_size= ((Uint64)f.FileSizeHi << 32) | (f.FileSizeLo);
  dst.m_path.assign(f.FileName);
5078

5079 5080
  dst.m_filegroup_id= f.FilegroupId;
  dst.m_filegroup_version= f.FilegroupVersion;
5081
  dst.m_free=  f.FileFreeExtents;
5082 5083 5084
  return 0;
}

5085
template class Vector<int>;
5086
template class Vector<Uint16>;
5087 5088
template class Vector<Uint32>;
template class Vector<Vector<Uint32> >;
5089 5090
template class Vector<NdbTableImpl*>;
template class Vector<NdbColumnImpl*>;
5091

5092
const NdbDictionary::Column * NdbDictionary::Column::FRAGMENT = 0;
5093 5094
const NdbDictionary::Column * NdbDictionary::Column::FRAGMENT_FIXED_MEMORY = 0;
const NdbDictionary::Column * NdbDictionary::Column::FRAGMENT_VARSIZED_MEMORY = 0;
5095 5096 5097 5098 5099 5100 5101 5102
const NdbDictionary::Column * NdbDictionary::Column::ROW_COUNT = 0;
const NdbDictionary::Column * NdbDictionary::Column::COMMIT_COUNT = 0;
const NdbDictionary::Column * NdbDictionary::Column::ROW_SIZE = 0;
const NdbDictionary::Column * NdbDictionary::Column::RANGE_NO = 0;
const NdbDictionary::Column * NdbDictionary::Column::DISK_REF = 0;
const NdbDictionary::Column * NdbDictionary::Column::RECORDS_IN_RANGE = 0;
const NdbDictionary::Column * NdbDictionary::Column::ROWID = 0;
const NdbDictionary::Column * NdbDictionary::Column::ROW_GCI = 0;