⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ndbapi_scan.cpp

📁 这是linux下运行的mysql软件包,可用于linux 下安装 php + mysql + apach 的网络配置
💻 CPP
📖 第 1 页 / 共 2 页
字号:
   *  Permanent error (return -1)   */  while (true)  {    if (retryAttempt >= retryMax)    {      std::cout << "ERROR: has retried this operation " << retryAttempt 		<< " times, failing!" << std::endl;      return -1;    }    myTrans = myNdb->startTransaction();    if (myTrans == NULL)     {      const NdbError err = myNdb->getNdbError();      if (err.status == NdbError::TemporaryError)      {	milliSleep(50);	retryAttempt++;	continue;      }      std::cout <<  err.message << std::endl;      return -1;    }   /**    * Get a scan operation.    */    myScanOp = myTrans->getNdbScanOperation(myTable);	    if (myScanOp == NULL)     {      std::cout << myTrans->getNdbError().message << std::endl;      myNdb->closeTransaction(myTrans);      return -1;    }    /**     * Define a result set for the scan.     */     if( myScanOp->readTuples(NdbOperation::LM_Exclusive) )     {      std::cout << myTrans->getNdbError().message << std::endl;      myNdb->closeTransaction(myTrans);      return -1;    }     /**     * Use NdbScanFilter to define a search critera     */     NdbScanFilter filter(myScanOp) ;       if(filter.begin(NdbScanFilter::AND) < 0  ||        filter.cmp(NdbScanFilter::COND_EQ, update_column, before_color) <0||       filter.end() <0)    {      std::cout <<  myTrans->getNdbError().message << std::endl;      myNdb->closeTransaction(myTrans);      return -1;    }            /**     * Start scan    (NoCommit since we are only reading at this stage);     */         if(myTrans->execute(NdbTransaction::NoCommit) != 0)    {            err = myTrans->getNdbError();          if(err.status == NdbError::TemporaryError){	std::cout << myTrans->getNdbError().message << std::endl;	myNdb->closeTransaction(myTrans);	milliSleep(50);	continue;      }      std::cout << myTrans->getNdbError().code << std::endl;      myNdb->closeTransaction(myTrans);      return -1;    }    /**     * start of loop: nextResult(true) means that "parallelism" number of     * rows are fetched from NDB and cached in NDBAPI     */        while((check = myScanOp->nextResult(true)) == 0){      do {	/**	 * Get update operation	 */    	NdbOperation * myUpdateOp = myScanOp->updateCurrentTuple();	if (myUpdateOp == 0)	{	  std::cout << myTrans->getNdbError().message << std::endl;	  myNdb->closeTransaction(myTrans);	  return -1;	}	updatedRows++;	/**	 * do the update	 */    	myUpdateOp->setValue(update_column, after_color);	/**	 * nextResult(false) means that the records 	 * cached in the NDBAPI are modified before	 * fetching more rows from NDB.	 */          } while((check = myScanOp->nextResult(false)) == 0);            /**       * NoCommit when all cached tuple have been updated       */          if(check != -1)      {	check = myTrans->execute(NdbTransaction::NoCommit);         }      /**       * Check for errors       */      err = myTrans->getNdbError();          if(check == -1)      {	if(err.status == NdbError::TemporaryError){	  std::cout << myTrans->getNdbError().message << std::endl;	  myNdb->closeTransaction(myTrans);	  milliSleep(50);	  continue;	}	      }      /**       * End of loop        */    }    /**     * Commit all prepared operations     */    if(myTrans->execute(NdbTransaction::Commit) == -1)    {      if(err.status == NdbError::TemporaryError){	std::cout << myTrans->getNdbError().message << std::endl;	myNdb->closeTransaction(myTrans);	milliSleep(50);	continue;      }	    }    std::cout << myTrans->getNdbError().message << std::endl;    myNdb->closeTransaction(myTrans);    return 0;      }  if(myTrans!=0)   {    std::cout << myTrans->getNdbError().message << std::endl;    myNdb->closeTransaction(myTrans);  }  return -1;}int scan_print(Ndb * myNdb){// Scan all records exclusive and update  // them one by one  int                  retryAttempt = 0;  const int            retryMax = 10;  int fetchedRows = 0;  int check;  NdbError              err;  NdbTransaction	*myTrans;  NdbScanOperation	*myScanOp;  /* Result of reading attribute value, three columns:     REG_NO, BRAND, and COLOR   */  NdbRecAttr *    	myRecAttr[3];     const NdbDictionary::Dictionary* myDict= myNdb->getDictionary();  const NdbDictionary::Table *myTable= myDict->getTable("GARAGE");  if (myTable == NULL)     APIERROR(myDict->getNdbError());  /**   * Loop as long as :   *  retryMax not reached   *  failed operations due to TEMPORARY erros   *   * Exit loop;   *  retyrMax reached   *  Permanent error (return -1)   */  while (true)  {    if (retryAttempt >= retryMax)    {      std::cout << "ERROR: has retried this operation " << retryAttempt 		<< " times, failing!" << std::endl;      return -1;    }    myTrans = myNdb->startTransaction();    if (myTrans == NULL)     {      const NdbError err = myNdb->getNdbError();      if (err.status == NdbError::TemporaryError)      {	milliSleep(50);	retryAttempt++;	continue;      }     std::cout << err.message << std::endl;      return -1;    }    /*     * Define a scan operation.      * NDBAPI.     */    myScanOp = myTrans->getNdbScanOperation(myTable);	    if (myScanOp == NULL)     {      std::cout << myTrans->getNdbError().message << std::endl;      myNdb->closeTransaction(myTrans);      return -1;    }    /**     * Read without locks, without being placed in lock queue     */    if( myScanOp->readTuples(NdbOperation::LM_CommittedRead) == -1)    {      std::cout << myTrans->getNdbError().message << std::endl;      myNdb->closeTransaction(myTrans);      return -1;    }     /**     * Define storage for fetched attributes.     * E.g., the resulting attributes of executing     * myOp->getValue("REG_NO") is placed in myRecAttr[0].     * No data exists in myRecAttr until transaction has commited!     */    myRecAttr[0] = myScanOp->getValue("REG_NO");    myRecAttr[1] = myScanOp->getValue("BRAND");    myRecAttr[2] = myScanOp->getValue("COLOR");    if(myRecAttr[0] ==NULL || myRecAttr[1] == NULL || myRecAttr[2]==NULL)     {	std::cout << myTrans->getNdbError().message << std::endl;	myNdb->closeTransaction(myTrans);	return -1;    }    /**     * Start scan   (NoCommit since we are only reading at this stage);     */         if(myTrans->execute(NdbTransaction::NoCommit) != 0){            err = myTrans->getNdbError();          if(err.status == NdbError::TemporaryError){	std::cout << myTrans->getNdbError().message << std::endl;	myNdb->closeTransaction(myTrans);	milliSleep(50);	continue;      }      std::cout << err.code << std::endl;      std::cout << myTrans->getNdbError().code << std::endl;      myNdb->closeTransaction(myTrans);      return -1;    }        /**     * start of loop: nextResult(true) means that "parallelism" number of     * rows are fetched from NDB and cached in NDBAPI     */        while((check = myScanOp->nextResult(true)) == 0){      do {		fetchedRows++;	/**	 * print  REG_NO unsigned int	 */	std::cout << myRecAttr[0]->u_32_value() << "\t";	/**	 * print  BRAND character string	 */	std::cout << myRecAttr[1]->aRef() << "\t";	/**	 * print  COLOR character string	 */	std::cout << myRecAttr[2]->aRef() << std::endl;	/**	 * nextResult(false) means that the records 	 * cached in the NDBAPI are modified before	 * fetching more rows from NDB.	 */          } while((check = myScanOp->nextResult(false)) == 0);    }        myNdb->closeTransaction(myTrans);    return 1;  }  return -1;}int main(){  ndb_init();  MYSQL mysql;  /**************************************************************   * Connect to mysql server and create table                   *   **************************************************************/  {    if ( !mysql_init(&mysql) ) {      std::cout << "mysql_init failed\n";      exit(-1);    }    if ( !mysql_real_connect(&mysql, "localhost", "root", "", "",			     3306, "/tmp/mysql.sock", 0) )      MYSQLERROR(mysql);    mysql_query(&mysql, "CREATE DATABASE TEST_DB");    if (mysql_query(&mysql, "USE TEST_DB") != 0) MYSQLERROR(mysql);    create_table(mysql);  }  /**************************************************************   * Connect to ndb cluster                                     *   **************************************************************/  Ndb_cluster_connection cluster_connection;  if (cluster_connection.connect(4, 5, 1))  {    std::cout << "Unable to connect to cluster within 30 secs." << std::endl;    exit(-1);  }  // Optionally connect and wait for the storage nodes (ndbd's)  if (cluster_connection.wait_until_ready(30,0) < 0)  {    std::cout << "Cluster was not ready within 30 secs.\n";    exit(-1);  }  Ndb myNdb(&cluster_connection,"TEST_DB");  if (myNdb.init(1024) == -1) {      // Set max 1024  parallel transactions    APIERROR(myNdb.getNdbError());    exit(-1);  }  /*******************************************   * Check table definition                  *   *******************************************/  int column_color;  {    const NdbDictionary::Dictionary* myDict= myNdb.getDictionary();    const NdbDictionary::Table *t= myDict->getTable("GARAGE");    Car car;    if (t->getColumn("COLOR")->getLength() != sizeof(car.color) ||	t->getColumn("BRAND")->getLength() != sizeof(car.brand))    {      std::cout << "Wrong table definition" << std::endl;      exit(-1);    }    column_color= t->getColumn("COLOR")->getColumnNo();  }  if(populate(&myNdb) > 0)    std::cout << "populate: Success!" << std::endl;    if(scan_print(&myNdb) > 0)    std::cout << "scan_print: Success!" << std::endl  << std::endl;    std::cout << "Going to delete all pink cars!" << std::endl;    {    /**     * Note! color needs to be of exact the same size as column defined     */    Car tmp;    sprintf(tmp.color, "Pink");    if(scan_delete(&myNdb, column_color, tmp.color) > 0)      std::cout << "scan_delete: Success!" << std::endl  << std::endl;  }  if(scan_print(&myNdb) > 0)    std::cout << "scan_print: Success!" << std::endl  << std::endl;    {    /**     * Note! color1 & 2 need to be of exact the same size as column defined     */    Car tmp1, tmp2;    sprintf(tmp1.color, "Blue");    sprintf(tmp2.color, "Black");    std::cout << "Going to update all " << tmp1.color 	      << " cars to " << tmp2.color << " cars!" << std::endl;    if(scan_update(&myNdb, column_color, tmp1.color, tmp2.color) > 0)       std::cout << "scan_update: Success!" << std::endl  << std::endl;  }  if(scan_print(&myNdb) > 0)    std::cout << "scan_print: Success!" << std::endl  << std::endl;  return 0;}

⌨️ 快捷键说明

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