PageRenderTime 25ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/release/src/router/mysql/storage/ndb/ndbapi-examples/ndbapi_simple_index/main.cpp

https://gitlab.com/envieidoc/tomato
C++ | 273 lines | 159 code | 48 blank | 66 comment | 61 complexity | 03aa69255953b5143167ac63045e8778 MD5 | raw file
  1. /* Copyright (c) 2003, 2005-2007 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
  12. //
  13. // ndbapi_simple_index.cpp: Using secondary indexes in NDB API
  14. //
  15. // Correct output from this program is:
  16. //
  17. // ATTR1 ATTR2
  18. // 0 0
  19. // 1 1
  20. // 2 2
  21. // 3 3
  22. // 4 4
  23. // 5 5
  24. // 6 6
  25. // 7 7
  26. // 8 8
  27. // 9 9
  28. // ATTR1 ATTR2
  29. // 0 10
  30. // 1 1
  31. // 2 12
  32. // Detected that deleted tuple doesn't exist!
  33. // 4 14
  34. // 5 5
  35. // 6 16
  36. // 7 7
  37. // 8 18
  38. // 9 9
  39. #include <mysql.h>
  40. #include <NdbApi.hpp>
  41. // Used for cout
  42. #include <stdio.h>
  43. #include <iostream>
  44. #define PRINT_ERROR(code,msg) \
  45. std::cout << "Error in " << __FILE__ << ", line: " << __LINE__ \
  46. << ", code: " << code \
  47. << ", msg: " << msg << "." << std::endl
  48. #define MYSQLERROR(mysql) { \
  49. PRINT_ERROR(mysql_errno(&mysql),mysql_error(&mysql)); \
  50. exit(-1); }
  51. #define APIERROR(error) { \
  52. PRINT_ERROR(error.code,error.message); \
  53. exit(-1); }
  54. int main(int argc, char** argv)
  55. {
  56. if (argc != 3)
  57. {
  58. std::cout << "Arguments are <socket mysqld> <connect_string cluster>.\n";
  59. exit(-1);
  60. }
  61. char * mysqld_sock = argv[1];
  62. const char *connectstring = argv[2];
  63. ndb_init();
  64. MYSQL mysql;
  65. /**************************************************************
  66. * Connect to mysql server and create table *
  67. **************************************************************/
  68. {
  69. if ( !mysql_init(&mysql) ) {
  70. std::cout << "mysql_init failed\n";
  71. exit(-1);
  72. }
  73. if ( !mysql_real_connect(&mysql, "localhost", "root", "", "",
  74. 0, mysqld_sock, 0) )
  75. MYSQLERROR(mysql);
  76. mysql_query(&mysql, "CREATE DATABASE TEST_DB_1");
  77. if (mysql_query(&mysql, "USE TEST_DB_1") != 0) MYSQLERROR(mysql);
  78. if (mysql_query(&mysql,
  79. "CREATE TABLE"
  80. " MYTABLENAME"
  81. " (ATTR1 INT UNSIGNED,"
  82. " ATTR2 INT UNSIGNED NOT NULL,"
  83. " PRIMARY KEY USING HASH (ATTR1),"
  84. " UNIQUE MYINDEXNAME USING HASH (ATTR2))"
  85. " ENGINE=NDB"))
  86. MYSQLERROR(mysql);
  87. }
  88. /**************************************************************
  89. * Connect to ndb cluster *
  90. **************************************************************/
  91. Ndb_cluster_connection *cluster_connection=
  92. new Ndb_cluster_connection(connectstring); // Object representing the cluster
  93. if (cluster_connection->connect(5,3,1))
  94. {
  95. std::cout << "Connect to cluster management server failed.\n";
  96. exit(-1);
  97. }
  98. if (cluster_connection->wait_until_ready(30,30))
  99. {
  100. std::cout << "Cluster was not ready within 30 secs.\n";
  101. exit(-1);
  102. }
  103. Ndb* myNdb = new Ndb( cluster_connection,
  104. "TEST_DB_1" ); // Object representing the database
  105. if (myNdb->init() == -1) {
  106. APIERROR(myNdb->getNdbError());
  107. exit(-1);
  108. }
  109. const NdbDictionary::Dictionary* myDict= myNdb->getDictionary();
  110. const NdbDictionary::Table *myTable= myDict->getTable("MYTABLENAME");
  111. if (myTable == NULL)
  112. APIERROR(myDict->getNdbError());
  113. const NdbDictionary::Index *myIndex= myDict->getIndex("MYINDEXNAME$unique","MYTABLENAME");
  114. if (myIndex == NULL)
  115. APIERROR(myDict->getNdbError());
  116. /**************************************************************************
  117. * Using 5 transactions, insert 10 tuples in table: (0,0),(1,1),...,(9,9) *
  118. **************************************************************************/
  119. for (int i = 0; i < 5; i++) {
  120. NdbTransaction *myTransaction= myNdb->startTransaction();
  121. if (myTransaction == NULL) APIERROR(myNdb->getNdbError());
  122. NdbOperation *myOperation= myTransaction->getNdbOperation(myTable);
  123. if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
  124. myOperation->insertTuple();
  125. myOperation->equal("ATTR1", i);
  126. myOperation->setValue("ATTR2", i);
  127. myOperation = myTransaction->getNdbOperation(myTable);
  128. if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
  129. myOperation->insertTuple();
  130. myOperation->equal("ATTR1", i+5);
  131. myOperation->setValue("ATTR2", i+5);
  132. if (myTransaction->execute( NdbTransaction::Commit ) == -1)
  133. APIERROR(myTransaction->getNdbError());
  134. myNdb->closeTransaction(myTransaction);
  135. }
  136. /*****************************************
  137. * Read and print all tuples using index *
  138. *****************************************/
  139. std::cout << "ATTR1 ATTR2" << std::endl;
  140. for (int i = 0; i < 10; i++) {
  141. NdbTransaction *myTransaction= myNdb->startTransaction();
  142. if (myTransaction == NULL) APIERROR(myNdb->getNdbError());
  143. NdbIndexOperation *myIndexOperation=
  144. myTransaction->getNdbIndexOperation(myIndex);
  145. if (myIndexOperation == NULL) APIERROR(myTransaction->getNdbError());
  146. myIndexOperation->readTuple(NdbOperation::LM_Read);
  147. myIndexOperation->equal("ATTR2", i);
  148. NdbRecAttr *myRecAttr= myIndexOperation->getValue("ATTR1", NULL);
  149. if (myRecAttr == NULL) APIERROR(myTransaction->getNdbError());
  150. if(myTransaction->execute( NdbTransaction::Commit,
  151. NdbOperation::AbortOnError ) != -1)
  152. printf(" %2d %2d\n", myRecAttr->u_32_value(), i);
  153. myNdb->closeTransaction(myTransaction);
  154. }
  155. /*****************************************************************
  156. * Update the second attribute in half of the tuples (adding 10) *
  157. *****************************************************************/
  158. for (int i = 0; i < 10; i+=2) {
  159. NdbTransaction *myTransaction= myNdb->startTransaction();
  160. if (myTransaction == NULL) APIERROR(myNdb->getNdbError());
  161. NdbIndexOperation *myIndexOperation=
  162. myTransaction->getNdbIndexOperation(myIndex);
  163. if (myIndexOperation == NULL) APIERROR(myTransaction->getNdbError());
  164. myIndexOperation->updateTuple();
  165. myIndexOperation->equal( "ATTR2", i );
  166. myIndexOperation->setValue( "ATTR2", i+10);
  167. if( myTransaction->execute( NdbTransaction::Commit ) == -1 )
  168. APIERROR(myTransaction->getNdbError());
  169. myNdb->closeTransaction(myTransaction);
  170. }
  171. /*************************************************
  172. * Delete one tuple (the one with primary key 3) *
  173. *************************************************/
  174. {
  175. NdbTransaction *myTransaction= myNdb->startTransaction();
  176. if (myTransaction == NULL) APIERROR(myNdb->getNdbError());
  177. NdbIndexOperation *myIndexOperation=
  178. myTransaction->getNdbIndexOperation(myIndex);
  179. if (myIndexOperation == NULL) APIERROR(myTransaction->getNdbError());
  180. myIndexOperation->deleteTuple();
  181. myIndexOperation->equal( "ATTR2", 3 );
  182. if (myTransaction->execute(NdbTransaction::Commit) == -1)
  183. APIERROR(myTransaction->getNdbError());
  184. myNdb->closeTransaction(myTransaction);
  185. }
  186. /*****************************
  187. * Read and print all tuples *
  188. *****************************/
  189. {
  190. std::cout << "ATTR1 ATTR2" << std::endl;
  191. for (int i = 0; i < 10; i++) {
  192. NdbTransaction *myTransaction= myNdb->startTransaction();
  193. if (myTransaction == NULL) APIERROR(myNdb->getNdbError());
  194. NdbOperation *myOperation= myTransaction->getNdbOperation(myTable);
  195. if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
  196. myOperation->readTuple(NdbOperation::LM_Read);
  197. myOperation->equal("ATTR1", i);
  198. NdbRecAttr *myRecAttr= myOperation->getValue("ATTR2", NULL);
  199. if (myRecAttr == NULL) APIERROR(myTransaction->getNdbError());
  200. if(myTransaction->execute( NdbTransaction::Commit,
  201. NdbOperation::AbortOnError ) == -1)
  202. if (i == 3) {
  203. std::cout << "Detected that deleted tuple doesn't exist!\n";
  204. } else {
  205. APIERROR(myTransaction->getNdbError());
  206. }
  207. if (i != 3) {
  208. printf(" %2d %2d\n", i, myRecAttr->u_32_value());
  209. }
  210. myNdb->closeTransaction(myTransaction);
  211. }
  212. }
  213. /**************
  214. * Drop table *
  215. **************/
  216. if (mysql_query(&mysql, "DROP TABLE MYTABLENAME"))
  217. MYSQLERROR(mysql);
  218. delete myNdb;
  219. delete cluster_connection;
  220. ndb_end(0);
  221. return 0;
  222. }