#include <mysql.h>#include <NdbApi.hpp>#include <stdio.h>#include <iostream>Include dependency graph for ndbapi_simple_index.cpp:

Go to the source code of this file.
Defines | |
| #define | PRINT_ERROR(code, msg) |
| #define | MYSQLERROR(mysql) |
| #define | APIERROR(error) |
Functions | |
| int | main () |
| #define APIERROR | ( | error | ) |
Value:
{ \
PRINT_ERROR(error.code,error.message); \
exit(-1); }
Definition at line 48 of file ndbapi_simple_index.cpp.
| #define MYSQLERROR | ( | mysql | ) |
Value:
{ \
PRINT_ERROR(mysql_errno(&mysql),mysql_error(&mysql)); \
exit(-1); }
Definition at line 45 of file ndbapi_simple_index.cpp.
| #define PRINT_ERROR | ( | code, | |||
| msg | ) |
Value:
std::cout << "Error in " << __FILE__ << ", line: " << __LINE__ \ << ", code: " << code \ << ", msg: " << msg << "." << std::endl
Definition at line 41 of file ndbapi_simple_index.cpp.
| int main | ( | void | ) |
Definition at line 52 of file ndbapi_simple_index.cpp.
References APIERROR, Ndb::closeTransaction(), NdbTransaction::Commit, Ndb_cluster_connection::connect(), NdbTransaction::execute(), exit, Ndb::getDictionary(), NdbDictionary::Dictionary::getIndex(), NdbTransaction::getNdbError(), NdbDictionary::Dictionary::getNdbError(), Ndb::getNdbError(), NdbTransaction::getNdbIndexOperation(), NdbDictionary::Dictionary::getTable(), Ndb::init(), NdbOperation::LM_Read, mysql, mysql_init(), mysql_query(), mysql_real_connect(), MYSQLERROR, ndb_end(), ndb_init(), NULL, Ndb::startTransaction(), NdbRecAttr::u_32_value(), and Ndb_cluster_connection::wait_until_ready().
00053 { 00054 ndb_init(); 00055 MYSQL mysql; 00056 00057 /************************************************************** 00058 * Connect to mysql server and create table * 00059 **************************************************************/ 00060 { 00061 if ( !mysql_init(&mysql) ) { 00062 std::cout << "mysql_init failed\n"; 00063 exit(-1); 00064 } 00065 if ( !mysql_real_connect(&mysql, "localhost", "root", "", "", 00066 3306, "/tmp/mysql.sock", 0) ) 00067 MYSQLERROR(mysql); 00068 00069 mysql_query(&mysql, "CREATE DATABASE TEST_DB_1"); 00070 if (mysql_query(&mysql, "USE TEST_DB_1") != 0) MYSQLERROR(mysql); 00071 00072 if (mysql_query(&mysql, 00073 "CREATE TABLE" 00074 " MYTABLENAME" 00075 " (ATTR1 INT UNSIGNED," 00076 " ATTR2 INT UNSIGNED NOT NULL," 00077 " PRIMARY KEY USING HASH (ATTR1)," 00078 " UNIQUE MYINDEXNAME USING HASH (ATTR2))" 00079 " ENGINE=NDB")) 00080 MYSQLERROR(mysql); 00081 } 00082 00083 /************************************************************** 00084 * Connect to ndb cluster * 00085 **************************************************************/ 00086 00087 Ndb_cluster_connection *cluster_connection= 00088 new Ndb_cluster_connection(); // Object representing the cluster 00089 00090 if (cluster_connection->connect(5,3,1)) 00091 { 00092 std::cout << "Connect to cluster management server failed.\n"; 00093 exit(-1); 00094 } 00095 00096 if (cluster_connection->wait_until_ready(30,30)) 00097 { 00098 std::cout << "Cluster was not ready within 30 secs.\n"; 00099 exit(-1); 00100 } 00101 00102 Ndb* myNdb = new Ndb( cluster_connection, 00103 "TEST_DB_1" ); // Object representing the database 00104 if (myNdb->init() == -1) { 00105 APIERROR(myNdb->getNdbError()); 00106 exit(-1); 00107 } 00108 00109 const NdbDictionary::Dictionary* myDict= myNdb->getDictionary(); 00110 const NdbDictionary::Table *myTable= myDict->getTable("MYTABLENAME"); 00111 if (myTable == NULL) 00112 APIERROR(myDict->getNdbError()); 00113 const NdbDictionary::Index *myIndex= myDict->getIndex("MYINDEXNAME","MYTABLENAME"); 00114 if (myIndex == NULL) 00115 APIERROR(myDict->getNdbError()); 00116 00117 /************************************************************************** 00118 * Using 5 transactions, insert 10 tuples in table: (0,0),(1,1),...,(9,9) * 00119 **************************************************************************/ 00120 for (int i = 0; i < 5; i++) { 00121 NdbTransaction *myTransaction= myNdb->startTransaction(); 00122 if (myTransaction == NULL) APIERROR(myNdb->getNdbError()); 00123 00124 NdbOperation *myOperation= myTransaction->getNdbOperation(myTable); 00125 if (myOperation == NULL) APIERROR(myTransaction->getNdbError()); 00126 00127 myOperation->insertTuple(); 00128 myOperation->equal("ATTR1", i); 00129 myOperation->setValue("ATTR2", i); 00130 00131 myOperation = myTransaction->getNdbOperation(myTable); 00132 if (myOperation == NULL) APIERROR(myTransaction->getNdbError()); 00133 00134 myOperation->insertTuple(); 00135 myOperation->equal("ATTR1", i+5); 00136 myOperation->setValue("ATTR2", i+5); 00137 00138 if (myTransaction->execute( NdbTransaction::Commit ) == -1) 00139 APIERROR(myTransaction->getNdbError()); 00140 00141 myNdb->closeTransaction(myTransaction); 00142 } 00143 00144 /***************************************** 00145 * Read and print all tuples using index * 00146 *****************************************/ 00147 std::cout << "ATTR1 ATTR2" << std::endl; 00148 00149 for (int i = 0; i < 10; i++) { 00150 NdbTransaction *myTransaction= myNdb->startTransaction(); 00151 if (myTransaction == NULL) APIERROR(myNdb->getNdbError()); 00152 00153 NdbIndexOperation *myIndexOperation= 00154 myTransaction->getNdbIndexOperation(myIndex); 00155 if (myIndexOperation == NULL) APIERROR(myTransaction->getNdbError()); 00156 00157 myIndexOperation->readTuple(NdbOperation::LM_Read); 00158 myIndexOperation->equal("ATTR2", i); 00159 00160 NdbRecAttr *myRecAttr= myIndexOperation->getValue("ATTR1", NULL); 00161 if (myRecAttr == NULL) APIERROR(myTransaction->getNdbError()); 00162 00163 if(myTransaction->execute( NdbTransaction::Commit ) != -1) 00164 printf(" %2d %2d\n", myRecAttr->u_32_value(), i); 00165 00166 myNdb->closeTransaction(myTransaction); 00167 } 00168 00169 /***************************************************************** 00170 * Update the second attribute in half of the tuples (adding 10) * 00171 *****************************************************************/ 00172 for (int i = 0; i < 10; i+=2) { 00173 NdbTransaction *myTransaction= myNdb->startTransaction(); 00174 if (myTransaction == NULL) APIERROR(myNdb->getNdbError()); 00175 00176 NdbIndexOperation *myIndexOperation= 00177 myTransaction->getNdbIndexOperation(myIndex); 00178 if (myIndexOperation == NULL) APIERROR(myTransaction->getNdbError()); 00179 00180 myIndexOperation->updateTuple(); 00181 myIndexOperation->equal( "ATTR2", i ); 00182 myIndexOperation->setValue( "ATTR2", i+10); 00183 00184 if( myTransaction->execute( NdbTransaction::Commit ) == -1 ) 00185 APIERROR(myTransaction->getNdbError()); 00186 00187 myNdb->closeTransaction(myTransaction); 00188 } 00189 00190 /************************************************* 00191 * Delete one tuple (the one with primary key 3) * 00192 *************************************************/ 00193 { 00194 NdbTransaction *myTransaction= myNdb->startTransaction(); 00195 if (myTransaction == NULL) APIERROR(myNdb->getNdbError()); 00196 00197 NdbIndexOperation *myIndexOperation= 00198 myTransaction->getNdbIndexOperation(myIndex); 00199 if (myIndexOperation == NULL) APIERROR(myTransaction->getNdbError()); 00200 00201 myIndexOperation->deleteTuple(); 00202 myIndexOperation->equal( "ATTR2", 3 ); 00203 00204 if (myTransaction->execute(NdbTransaction::Commit) == -1) 00205 APIERROR(myTransaction->getNdbError()); 00206 00207 myNdb->closeTransaction(myTransaction); 00208 } 00209 00210 /***************************** 00211 * Read and print all tuples * 00212 *****************************/ 00213 { 00214 std::cout << "ATTR1 ATTR2" << std::endl; 00215 00216 for (int i = 0; i < 10; i++) { 00217 NdbTransaction *myTransaction= myNdb->startTransaction(); 00218 if (myTransaction == NULL) APIERROR(myNdb->getNdbError()); 00219 00220 NdbOperation *myOperation= myTransaction->getNdbOperation(myTable); 00221 if (myOperation == NULL) APIERROR(myTransaction->getNdbError()); 00222 00223 myOperation->readTuple(NdbOperation::LM_Read); 00224 myOperation->equal("ATTR1", i); 00225 00226 NdbRecAttr *myRecAttr= myOperation->getValue("ATTR2", NULL); 00227 if (myRecAttr == NULL) APIERROR(myTransaction->getNdbError()); 00228 00229 if(myTransaction->execute( NdbTransaction::Commit ) == -1) 00230 if (i == 3) { 00231 std::cout << "Detected that deleted tuple doesn't exist!\n"; 00232 } else { 00233 APIERROR(myTransaction->getNdbError()); 00234 } 00235 00236 if (i != 3) { 00237 printf(" %2d %2d\n", i, myRecAttr->u_32_value()); 00238 } 00239 myNdb->closeTransaction(myTransaction); 00240 } 00241 } 00242 00243 /************** 00244 * Drop table * 00245 **************/ 00246 if (mysql_query(&mysql, "DROP TABLE MYTABLENAME")) 00247 MYSQLERROR(mysql); 00248 00249 delete myNdb; 00250 delete cluster_connection; 00251 00252 ndb_end(0); 00253 return 0; 00254 }
Here is the call graph for this function:

1.4.7

