ndbdictionaryimpl.cpp

来自「MySQL源码文件5.X系列, 可自已编译到服务器」· C++ 代码 · 共 2,432 行 · 第 1/5 页

CPP
2,432
字号
  req->senderRef= m_reference;  req->senderData= 0;  req->requestType=    GetTabInfoReq::RequestByName | GetTabInfoReq::LongSignalConf;  req->tableNameLen= namelen;  tSignal.theReceiversBlockNumber= DBDICT;  tSignal.theVerId_signalNumber= GSN_GET_TABINFOREQ;  tSignal.theLength= GetTabInfoReq::SignalLength;  // Copy name to m_buffer to get a word sized buffer  m_buffer.clear();  m_buffer.grow(namelen_words*4+4);  m_buffer.append(name.c_str(), namelen);#ifndef IGNORE_VALGRIND_WARNINGS  Uint32 pad = 0;  m_buffer.append(&pad, 4);#endif    LinearSectionPtr ptr[1];  ptr[0].p= (Uint32*)m_buffer.get_data();  ptr[0].sz= namelen_words;  return getTable(&tSignal, ptr, 1, fullyQualifiedNames);}NdbTableImpl *NdbDictInterface::getTable(class NdbApiSignal * signal,			   LinearSectionPtr ptr[3],			   Uint32 noOfSections, bool fullyQualifiedNames){  int errCodes[] = {GetTabInfoRef::Busy };  int r = dictSignal(signal,ptr,noOfSections,		     0/*do not use masternode id*/,		     100,		     WAIT_GET_TAB_INFO_REQ,		     WAITFOR_RESPONSE_TIMEOUT,		     errCodes, 1);  if (r) return 0;  NdbTableImpl * rt = 0;  m_error.code= parseTableInfo(&rt, 			       (Uint32*)m_buffer.get_data(), 			       m_buffer.length() / 4, fullyQualifiedNames);  if (rt != 0)    rt->buildColumnHash();  return rt;}voidNdbDictInterface::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);}voidNdbDictInterface::execGET_TABINFO_REF(NdbApiSignal * signal,				      LinearSectionPtr ptr[3]){  const GetTabInfoRef* ref = CAST_CONSTPTR(GetTabInfoRef, signal->getDataPtr());  m_error.code= ref->errorCode;  m_waiter.signal(NO_WAIT);}/***************************************************************** * Pack/Unpack tables */struct ApiKernelMapping {  Int32 kernelConstant;  Int32 apiConstant;};Uint32getApiConstant(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;}Uint32getKernelConstant(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 constApiKernelMapping fragmentTypeMapping[] = {  { DictTabInfo::AllNodesSmallTable,  NdbDictionary::Object::FragAllSmall },  { DictTabInfo::AllNodesMediumTable, NdbDictionary::Object::FragAllMedium },  { DictTabInfo::AllNodesLargeTable,  NdbDictionary::Object::FragAllLarge },  { DictTabInfo::SingleFragment,      NdbDictionary::Object::FragSingle },  { -1, -1 }};static constApiKernelMappingobjectTypeMapping[] = {  { 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 },  { -1, -1 }};static constApiKernelMappingobjectStateMapping[] = {  { DictTabInfo::StateOffline,       NdbDictionary::Object::StateOffline },  { DictTabInfo::StateBuilding,      NdbDictionary::Object::StateBuilding },  { DictTabInfo::StateDropping,      NdbDictionary::Object::StateDropping },  { DictTabInfo::StateOnline,        NdbDictionary::Object::StateOnline },  { DictTabInfo::StateBackup,        NdbDictionary::Object::StateBackup },  { DictTabInfo::StateBroken,        NdbDictionary::Object::StateBroken },   { -1, -1 }};static constApiKernelMappingobjectStoreMapping[] = {  { DictTabInfo::StoreTemporary,     NdbDictionary::Object::StoreTemporary },  { DictTabInfo::StorePermanent,     NdbDictionary::Object::StorePermanent },  { -1, -1 }};static constApiKernelMappingindexTypeMapping[] = {  { DictTabInfo::UniqueHashIndex,    NdbDictionary::Index::UniqueHashIndex },    { DictTabInfo::OrderedIndex,       NdbDictionary::Index::OrderedIndex },  { -1, -1 }};intNdbDictInterface::parseTableInfo(NdbTableImpl ** ret,				 const Uint32 * data, Uint32 len,				 bool fullyQualifiedNames){  DBUG_ENTER("NdbDictInterface::parseTableInfo");  SimplePropertiesLinearReader it(data, len);  DictTabInfo::Table tableDesc; tableDesc.init();  SimpleProperties::UnpackStatus s;  s = SimpleProperties::unpack(it, &tableDesc, 			       DictTabInfo::TableMapping, 			       DictTabInfo::TableMappingSize, 			       true, true);    if(s != SimpleProperties::Break){    DBUG_RETURN(703);  }  const char * internalName = tableDesc.TableName;  const char * externalName = Ndb::externalizeTableName(internalName, fullyQualifiedNames);  NdbTableImpl * impl = new NdbTableImpl();  impl->m_tableId = tableDesc.TableId;  impl->m_version = tableDesc.TableVersion;  impl->m_status = NdbDictionary::Object::Retrieved;  impl->m_internalName.assign(internalName);  impl->m_externalName.assign(externalName);  impl->m_frm.assign(tableDesc.FrmData, tableDesc.FrmLen);    impl->m_fragmentType = (NdbDictionary::Object::FragmentType)    getApiConstant(tableDesc.FragmentType, 		   fragmentTypeMapping, 		   (Uint32)NdbDictionary::Object::FragUndefined);    Uint64 max_rows = ((Uint64)tableDesc.MaxRowsHigh) << 32;  max_rows += tableDesc.MaxRowsLow;  impl->m_max_rows = max_rows;  Uint64 min_rows = ((Uint64)tableDesc.MinRowsHigh) << 32;  min_rows += tableDesc.MinRowsLow;  impl->m_min_rows = min_rows;  impl->m_logging = tableDesc.TableLoggedFlag;  impl->m_kvalue = tableDesc.TableKValue;  impl->m_minLoadFactor = tableDesc.MinLoadFactor;  impl->m_maxLoadFactor = tableDesc.MaxLoadFactor;  impl->m_indexType = (NdbDictionary::Index::Type)    getApiConstant(tableDesc.TableType,		   indexTypeMapping,		   NdbDictionary::Index::Undefined);    if(impl->m_indexType == NdbDictionary::Index::Undefined){  } else {    const char * externalPrimary =       Ndb::externalizeTableName(tableDesc.PrimaryTable, fullyQualifiedNames);    impl->m_primaryTable.assign(externalPrimary);  }    Uint32 keyInfoPos = 0;  Uint32 keyCount = 0;  Uint32 blobCount = 0;  Uint32 distKeys = 0;    Uint32 i;  for(i = 0; i < tableDesc.NoOfAttributes; i++) {    DictTabInfo::Attribute attrDesc; attrDesc.init();    s = SimpleProperties::unpack(it, 				 &attrDesc, 				 DictTabInfo::AttributeMapping, 				 DictTabInfo::AttributeMappingSize, 				 true, true);    if(s != SimpleProperties::Break){      delete impl;      DBUG_RETURN(703);    }        NdbColumnImpl * col = new NdbColumnImpl();    col->m_attrId = attrDesc.AttributeId;    col->setName(attrDesc.AttributeName);    // check type and compute attribute size and array size    if (! attrDesc.translateExtType()) {      delete impl;      DBUG_RETURN(703);    }    col->m_type = (NdbDictionary::Column::Type)attrDesc.AttributeExtType;    col->m_precision = (attrDesc.AttributeExtPrecision & 0xFFFF);    col->m_scale = attrDesc.AttributeExtScale;    col->m_length = attrDesc.AttributeExtLength;    // 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;      DBUG_RETURN(703);    }    if (col->getCharType()) {      col->m_cs = get_charset(cs_number, MYF(0));      if (col->m_cs == NULL) {        delete impl;        DBUG_RETURN(743);      }    }    col->m_attrSize = (1 << attrDesc.AttributeSize) / 8;    col->m_arraySize = attrDesc.AttributeArraySize;    if(attrDesc.AttributeSize == 0)    {      col->m_attrSize = 4;      col->m_arraySize = (attrDesc.AttributeArraySize + 31) >> 5;    }        col->m_pk = attrDesc.AttributeKeyFlag;    col->m_distributionKey = attrDesc.AttributeDKey;    col->m_nullable = attrDesc.AttributeNullableFlag;    col->m_autoIncrement = (attrDesc.AttributeAutoIncrement ? true : false);    col->m_autoIncrementInitialValue = ~0;    col->m_defaultValue.assign(attrDesc.AttributeDefaultValue);    if(attrDesc.AttributeKeyFlag){      col->m_keyInfoPos = keyInfoPos + 1;      keyInfoPos += ((col->m_attrSize * col->m_arraySize + 3) / 4);      keyCount++;            if(attrDesc.AttributeDKey)	distKeys++;    } else {      col->m_keyInfoPos = 0;    }    if (col->getBlobType())      blobCount++;    NdbColumnImpl * null = 0;    impl->m_columns.fill(attrDesc.AttributeId, null);    if(impl->m_columns[attrDesc.AttributeId] != 0){      delete col;      delete impl;      DBUG_RETURN(703);    }    impl->m_columns[attrDesc.AttributeId] = col;    it.next();  }  impl->m_noOfKeys = keyCount;  impl->m_keyLenInWords = keyInfoPos;  impl->m_noOfBlobs = blobCount;  impl->m_noOfDistributionKeys = distKeys;  if(tableDesc.FragmentDataLen > 0)  {    Uint32 replicaCount = tableDesc.FragmentData[0];    Uint32 fragCount = tableDesc.FragmentData[1];    impl->m_replicaCount = replicaCount;    impl->m_fragmentCount = fragCount;    for(i = 0; i<(fragCount*replicaCount); i++)    {      impl->m_fragments.push_back(tableDesc.FragmentData[i+2]);    }    Uint32 topBit = (1 << 31);    for(; topBit && !(fragCount & topBit); ){      topBit >>= 1;    }    impl->m_hashValueMask = topBit - 1;    impl->m_hashpointerValue = fragCount - (impl->m_hashValueMask + 1);  }  else  {    impl->m_fragmentCount = tableDesc.FragmentCount;    impl->m_replicaCount = 0;    impl->m_hashValueMask = 0;    impl->m_hashpointerValue = 0;  }  if(distKeys == 0)  {    for(i = 0; i < tableDesc.NoOfAttributes; i++)    {      if(impl->m_columns[i]->getPrimaryKey())	impl->m_columns[i]->m_distributionKey = true;    }  }    * ret = impl;  DBUG_RETURN(0);}/***************************************************************** * Create table and alter table */intNdbDictionaryImpl::createTable(NdbTableImpl &t){   if (m_receiver.createTable(m_ndb, t) != 0)    return -1;  if (t.m_noOfBlobs == 0)    return 0;  // update table def from DICT  Ndb_local_table_info *info=    get_local_table_info(t.m_internalName,false);  if (info == NULL) {    m_error.code= 709;    return -1;  }  if (createBlobTables(*(info->m_table_impl)) != 0) {    int save_code = m_error.code;    (void)dropTable(t);    m_error.code= save_code;    return -1;  }  return 0;}intNdbDictionaryImpl::createBlobTables(NdbTableImpl &t){  for (unsigned i = 0; i < t.m_columns.size(); i++) {    NdbColumnImpl & c = *t.m_columns[i];    if (! c.getBlobType() || c.getPartSize() == 0)      continue;    NdbTableImpl bt;    NdbBlob::getBlobTable(bt, &t, &c);    if (createTable(bt) != 0)      return -1;    // Save BLOB table handle    Ndb_local_table_info *info=      get_local_table_info(bt.m_internalName, false);    if (info == 0) {      return -1;    }    c.m_blobTable = info->m_table_impl;  }    return 0;}intNdbDictionaryImpl::addBlobTables(NdbTableImpl &t){  unsigned n= t.m_noOfBlobs;  // 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--;    char btname[NdbBlobImpl::BlobTableNameSize];    NdbBlob::getBlobTableName(btname, &t, &c);    // Save BLOB table handle    NdbTableImpl * cachedBlobTable = getTable(btname);    if (cachedBlobTable == 0) {      return -1;    }    c.m_blobTable = cachedBlobTable;  }    return 0;}int NdbDictInterface::createTable(Ndb & ndb,			      NdbTableImpl & impl){  return createOrAlterTable(ndb, impl, false);}int NdbDictionaryImpl::alterTable(NdbTableImpl &impl){  BaseString internalName(impl.m_internalName);  const char * originalInternalName = internalName.c_str();  DBUG_ENTER("NdbDictionaryImpl::alterTable");  Ndb_local_table_info * local = 0;  if((local= get_local_table_info(originalInternalName, false)) == 0)  {    m_error.code = 709;    DBUG_RETURN(-1);  }  // Alter the table  int ret = m_receiver.alterTable(m_ndb, impl);  if(ret == 0){    // Remove cached information and let it be refreshed at next access    m_globalHash->lock();    local->m_table_impl->m_status = NdbDictionary::Object::Invalid;    m_globalHash->drop(local->m_table_impl);    m_globalHash->unlock();    m_localHash.drop(originalInternalName);  }  DBUG_RETURN(ret);}int NdbDictInterface::alterTable(Ndb & ndb,			      NdbTableImpl & impl){  return createOrAlterTable(ndb, impl, true);}int NdbDictInterface::createOrAlterTable(Ndb & ndb,				     NdbTableImpl & impl,				     bool alter){  DBUG_ENTER("NdbDictInterface::createOrAlterTable");  unsigned i, err;  if((unsigned)impl.getNoOfPrimaryKeys() > NDB_MAX_NO_OF_ATTRIBUTES_IN_KEY){    m_error.code= 4317;    DBUG_RETURN(-1);  }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?