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

📄 ndbapi_scan.cpp

📁 mysql-5.0.22.tar.gz源码包
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* 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   the Free Software Foundation; either version 2 of the License, or   (at your option) any later version.   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 *//* * ndbapi_scan.cpp:  * Illustrates how to use the scan api in the NDBAPI. * The example shows how to do scan, scan for update and scan for delete * using NdbScanFilter and NdbScanOperation * * Classes and methods used in this example: * *  Ndb_cluster_connection *       connect() *       wait_until_ready() * *  Ndb *       init() *       getDictionary() *       startTransaction() *       closeTransaction() * *  NdbTransaction *       getNdbScanOperation() *       execute() * *  NdbScanOperation *       getValue()  *       readTuples() *       nextResult() *       deleteCurrentTuple() *       updateCurrentTuple() * *  const NdbDictionary::Dictionary *       getTable() * *  const NdbDictionary::Table *       getColumn() * *  const NdbDictionary::Column *       getLength() * *  NdbOperation *       insertTuple() *       equal() *       setValue() * *  NdbScanFilter *       begin() *	 eq() *	 end() * */#include <mysql.h>#include <mysqld_error.h>#include <NdbApi.hpp>// Used for cout#include <iostream>#include <stdio.h>/** * Helper sleep function */static voidmilliSleep(int milliseconds){  struct timeval sleeptime;  sleeptime.tv_sec = milliseconds / 1000;  sleeptime.tv_usec = (milliseconds - (sleeptime.tv_sec * 1000)) * 1000000;  select(0, 0, 0, 0, &sleeptime);}/** * Helper sleep function */#define PRINT_ERROR(code,msg) \  std::cout << "Error in " << __FILE__ << ", line: " << __LINE__ \            << ", code: " << code \            << ", msg: " << msg << "." << std::endl#define MYSQLERROR(mysql) { \  PRINT_ERROR(mysql_errno(&mysql),mysql_error(&mysql)); \  exit(-1); }#define APIERROR(error) { \  PRINT_ERROR(error.code,error.message); \  exit(-1); }struct Car {  /**   * Note memset, so that entire char-fields are cleared   *   as all 20 bytes are significant (as type is char)   */  Car() { memset(this, 0, sizeof(* this)); }    unsigned int reg_no;  char brand[20];  char color[20];};/** * Function to create table */int create_table(MYSQL &mysql) {  while (mysql_query(&mysql, 		  "CREATE TABLE"		  "  GARAGE"		  "    (REG_NO INT UNSIGNED NOT NULL,"		  "     BRAND CHAR(20) NOT NULL,"		  "     COLOR CHAR(20) NOT NULL,"		  "     PRIMARY KEY USING HASH (REG_NO))"		  "  ENGINE=NDB"))  {    if (mysql_errno(&mysql) != ER_TABLE_EXISTS_ERROR)      MYSQLERROR(mysql);    std::cout << "MySQL Cluster already has example table: GARAGE. "	      << "Dropping it..." << std::endl;     /**************     * Drop table *     **************/    if (mysql_query(&mysql, "DROP TABLE GARAGE"))      MYSQLERROR(mysql);  }  return 1;}int populate(Ndb * myNdb){  int i;  Car cars[15];  const NdbDictionary::Dictionary* myDict= myNdb->getDictionary();  const NdbDictionary::Table *myTable= myDict->getTable("GARAGE");  if (myTable == NULL)     APIERROR(myDict->getNdbError());  /**   * Five blue mercedes   */  for (i = 0; i < 5; i++)  {    cars[i].reg_no = i;    sprintf(cars[i].brand, "Mercedes");    sprintf(cars[i].color, "Blue");  }  /**   * Five black bmw   */  for (i = 5; i < 10; i++)  {    cars[i].reg_no = i;    sprintf(cars[i].brand, "BMW");    sprintf(cars[i].color, "Black");  }  /**   * Five pink toyotas   */  for (i = 10; i < 15; i++)  {    cars[i].reg_no = i;    sprintf(cars[i].brand, "Toyota");    sprintf(cars[i].color, "Pink");  }    NdbTransaction* myTrans = myNdb->startTransaction();  if (myTrans == NULL)    APIERROR(myNdb->getNdbError());  for (i = 0; i < 15; i++)   {    NdbOperation* myNdbOperation = myTrans->getNdbOperation(myTable);    if (myNdbOperation == NULL)       APIERROR(myTrans->getNdbError());    myNdbOperation->insertTuple();    myNdbOperation->equal("REG_NO", cars[i].reg_no);    myNdbOperation->setValue("BRAND", cars[i].brand);    myNdbOperation->setValue("COLOR", cars[i].color);  }  int check = myTrans->execute(NdbTransaction::Commit);  myTrans->close();  return check != -1;}int scan_delete(Ndb* myNdb, 		int column,		const char * color)  {    // Scan all records exclusive and delete   // them one by one  int                  retryAttempt = 0;  const int            retryMax = 10;  int deletedRows = 0;  int check;  NdbError              err;  NdbTransaction	*myTrans;  NdbScanOperation	*myScanOp;  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;    }   /**    * 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) != 0)    {      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, column, 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 << 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       {	if (myScanOp->deleteCurrentTuple() != 0)	{	  std::cout << myTrans->getNdbError().message << std::endl;	  myNdb->closeTransaction(myTrans);	  return -1;	}	deletedRows++;		/**	 * nextResult(false) means that the records 	 * cached in the NDBAPI are modified before	 * fetching more rows from NDB.	 */          } while((check = myScanOp->nextResult(false)) == 0);            /**       * Commit when all cached tuple have been marked for deletion       */          if(check != -1)      {	check = myTrans->execute(NdbTransaction::Commit);         }      if(check == -1)      {	/**	 * Create a new transaction, while keeping scan open	 */	check = myTrans->restart();      }      /**       * 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        */    }    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_update(Ndb* myNdb, 		int update_column,		const char * before_color,		const char * after_color)		{    // Scan all records exclusive and update  // them one by one  int                  retryAttempt = 0;  const int            retryMax = 10;  int updatedRows = 0;  int check;  NdbError              err;  NdbTransaction	*myTrans;  NdbScanOperation	*myScanOp;  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

⌨️ 快捷键说明

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