ndbdictionaryimpl.cpp

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

CPP
2,432
字号
    return 0;  }  /**   * Create index impl   */  NdbIndexImpl* idx;  if(NdbDictInterface::create_index_obj_from_table(&idx, tab, prim) == 0){    idx->m_table = tab;    idx->m_externalName.assign(externalName);    idx->m_internalName.assign(internalName);    // TODO Assign idx to tab->m_index    // Don't do it right now since assign can't asign a table with index    // tab->m_index = idx;    return idx;  }  return 0;}intNdbDictInterface::create_index_obj_from_table(NdbIndexImpl** dst,					      NdbTableImpl* tab,					      const NdbTableImpl* prim){  NdbIndexImpl *idx = new NdbIndexImpl();  idx->m_version = tab->m_version;  idx->m_status = tab->m_status;  idx->m_indexId = tab->m_tableId;  idx->m_externalName.assign(tab->getName());  idx->m_tableName.assign(prim->m_externalName);  NdbDictionary::Index::Type type = idx->m_type = tab->m_indexType;  idx->m_logging = tab->m_logging;  // skip last attribute (NDB$PK or NDB$TNODE)  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++){    NdbColumnImpl* org = tab->m_columns[i];    NdbColumnImpl* col = new NdbColumnImpl;    // Copy column definition    *col = * org;    idx->m_columns.push_back(col);    /**     * reverse map     */    const NdbColumnImpl* primCol = prim->getColumn(col->getName());    int key_id = primCol->getColumnNo();    int fill = -1;    idx->m_key_ids.fill(key_id, fill);    idx->m_key_ids[key_id] = i;    col->m_keyInfoPos = key_id;    if(type == NdbDictionary::Index::OrderedIndex &&        (primCol->m_distributionKey ||	(distKeys == 0 && primCol->getPrimaryKey())))    {      keyCount--;      org->m_distributionKey = 1;    }  }  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;  }  * dst = idx;  return 0;}/***************************************************************** * Create index */intNdbDictionaryImpl::createIndex(NdbIndexImpl &ix){  NdbTableImpl* tab = getTable(ix.getTable());  if(tab == 0){    m_error.code = 4249;    return -1;  }    return m_receiver.createIndex(m_ndb, ix, * tab);}int NdbDictInterface::createIndex(Ndb & ndb,			      NdbIndexImpl & impl, 			      const NdbTableImpl & table){  //validate();  //aggregate();  unsigned i, err;  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;  }  const BaseString internalName(    ndb.internalize_index_name(&table, impl.getName()));  impl.m_internalName.assign(internalName);  w.add(DictTabInfo::TableName, internalName.c_str());  w.add(DictTabInfo::TableLoggedFlag, impl.m_logging);  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);    req->setTableId(table.m_tableId);  req->setOnline(true);  AttributeList attributeList;  attributeList.sz = impl.m_columns.size();  for(i = 0; i<attributeList.sz; i++){    const NdbColumnImpl* col =       table.getColumn(impl.m_columns[i]->m_name.c_str());    if(col == 0){      m_error.code = 4247;      return -1;    }    // Copy column definition    *impl.m_columns[i] = *col;    // index key type check    if (it == DictTabInfo::UniqueHashIndex &&        (err = NdbSqlUtil::check_column_for_hash_index(col->m_type, col->m_cs))        ||        it == DictTabInfo::OrderedIndex &&        (err = NdbSqlUtil::check_column_for_ordered_index(col->m_type, col->m_cs)))    {      m_error.code = err;      return -1;    }    attributeList.id[i] = col->m_attrId;  }  LinearSectionPtr ptr[2];  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?  return createIndex(&tSignal, ptr);}intNdbDictInterface::createIndex(NdbApiSignal* signal, 			      LinearSectionPtr ptr[3]){  const int noErrCodes = 2;  int errCodes[noErrCodes] = {CreateIndxRef::Busy, CreateIndxRef::NotMaster};  return dictSignal(signal,ptr,2,		    1 /*use masternode id*/,		    100,		    WAIT_CREATE_INDX_REQ,		    -1,		    errCodes,noErrCodes);}voidNdbDictInterface::execCREATE_INDX_CONF(NdbApiSignal * signal,				       LinearSectionPtr ptr[3]){  //CreateTableConf* const conf = CAST_CONSTPTR(CreateTableConf, signal->getDataPtr());    m_waiter.signal(NO_WAIT);  }voidNdbDictInterface::execCREATE_INDX_REF(NdbApiSignal * signal,				      LinearSectionPtr ptr[3]){  const CreateIndxRef* const ref = CAST_CONSTPTR(CreateIndxRef, signal->getDataPtr());  m_error.code = ref->getErrorCode();  if(m_error.code == ref->NotMaster)    m_masterNodeId= ref->masterNodeId;  m_waiter.signal(NO_WAIT);  }/***************************************************************** * Drop index */intNdbDictionaryImpl::dropIndex(const char * indexName, 			     const char * tableName){  NdbIndexImpl * idx = getIndex(indexName, tableName);  if (idx == 0) {    m_error.code = 4243;    return -1;  }  int ret = dropIndex(*idx);  // 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) {    const BaseString internalIndexName((tableName)      ?      m_ndb.internalize_index_name(getTable(tableName), indexName)      :      m_ndb.internalize_table_name(indexName)); // Index is also a table    m_localHash.drop(internalIndexName.c_str());    m_globalHash->lock();    idx->m_table->m_status = NdbDictionary::Object::Invalid;    m_globalHash->drop(idx->m_table);    m_globalHash->unlock();    return dropIndex(indexName, tableName);  }  return ret;}intNdbDictionaryImpl::dropIndex(NdbIndexImpl & impl){    NdbTableImpl * timpl = impl.m_table;        if (timpl == 0) {      m_error.code = 709;      return -1;    }    int ret = m_receiver.dropIndex(impl, *timpl);    if(ret == 0){      m_localHash.drop(timpl->m_internalName.c_str());      m_globalHash->lock();      timpl->m_status = NdbDictionary::Object::Invalid;      m_globalHash->drop(timpl);      m_globalHash->unlock();    }    return ret;}intNdbDictInterface::dropIndex(const NdbIndexImpl & impl, 			    const NdbTableImpl & timpl){  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  req->setIndexId(timpl.m_tableId);  req->setIndexVersion(timpl.m_version);  return dropIndex(&tSignal, 0);}intNdbDictInterface::dropIndex(NdbApiSignal* signal, LinearSectionPtr ptr[3]){  const int noErrCodes = 2;  int errCodes[noErrCodes] = {DropIndxRef::Busy, DropIndxRef::NotMaster};  int r = dictSignal(signal,NULL,0,		     1/*Use masternode id*/,		     100,		     WAIT_DROP_INDX_REQ,		     WAITFOR_RESPONSE_TIMEOUT,		     errCodes,noErrCodes);  if(m_error.code == DropIndxRef::InvalidIndexVersion) {    // Clear caches and try again    return INCOMPATIBLE_VERSION;  }  return r;}voidNdbDictInterface::execDROP_INDX_CONF(NdbApiSignal * signal,				       LinearSectionPtr ptr[3]){  m_waiter.signal(NO_WAIT);  }voidNdbDictInterface::execDROP_INDX_REF(NdbApiSignal * signal,				      LinearSectionPtr ptr[3]){  const DropIndxRef* const ref = CAST_CONSTPTR(DropIndxRef, signal->getDataPtr());  m_error.code = ref->getErrorCode();  if(m_error.code == ref->NotMaster)    m_masterNodeId= ref->masterNodeId;  m_waiter.signal(NO_WAIT);  }/***************************************************************** * List objects or indexes */intNdbDictionaryImpl::listObjects(List& list, NdbDictionary::Object::Type type){  ListTablesReq req;  req.requestData = 0;  req.setTableType(getKernelConstant(type, objectTypeMapping, 0));  req.setListNames(true);  return m_receiver.listObjects(list, req.requestData, m_ndb.usingFullyQualifiedNames());}intNdbDictionaryImpl::listIndexes(List& list, Uint32 indexId){  ListTablesReq req;  req.requestData = 0;  req.setTableId(indexId);  req.setListNames(true);  req.setListIndexes(true);  return m_receiver.listObjects(list, req.requestData, m_ndb.usingFullyQualifiedNames());}intNdbDictInterface::listObjects(NdbDictionary::Dictionary::List& list,			      Uint32 requestData, bool fullyQualifiedNames){  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    m_error.code= 4213;    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);    // 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);      objectName = BaseString(Ndb::externalizeIndexName(indexName, fullyQualifiedNames));      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);      objectName = BaseString(Ndb::externalizeTableName(tableName, fullyQualifiedNames));      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;}intNdbDictInterface::listObjects(NdbApiSignal* signal){  const Uint32 RETRIES = 100;  for (Uint32 i = 0; i < RETRIES; i++) {    m_buffer.clear();    // begin protected    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(signal, aNodeId) != 0) {      m_transporter->unlock_mutex();      continue;    }    m_error.code= 0;    m_waiter.m_node = aNodeId;    m_waiter.m_state = WAIT_LIST_TABLES_CONF;    m_waiter.wait(WAITFOR_RESPONSE_TIMEOUT);    m_transporter->unlock_mutex();        // end protected    if (m_waiter.m_state == NO_WAIT && m_error.code == 0)      return 0;    if (m_waiter.m_state == WAIT_NODE_FAILURE)      continue;    return -1;  }  return -1;}voidNdbDictInterface::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);  }}template class Vector<int>;template class Vector<Uint16>;template class Vector<Uint32>;template class Vector<Vector<Uint32> >;template class Vector<NdbTableImpl*>;template class Vector<NdbColumnImpl*>;

⌨️ 快捷键说明

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