PageRenderTime 45ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/envieidoc/advancedtomato2
C++ | 297 lines | 178 code | 51 blank | 68 comment | 56 complexity | d8338e1fd65ac9d92698911282c8a257 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.cpp: Using synchronous transactions in NDB API
  14. *
  15. * Correct output from this program is:
  16. *
  17. * ATTR1 ATTR2
  18. * 0 10
  19. * 1 1
  20. * 2 12
  21. * Detected that deleted tuple doesn't exist!
  22. * 4 14
  23. * 5 5
  24. * 6 16
  25. * 7 7
  26. * 8 18
  27. * 9 9
  28. *
  29. */
  30. #include <mysql.h>
  31. #include <NdbApi.hpp>
  32. // Used for cout
  33. #include <stdio.h>
  34. #include <iostream>
  35. static void run_application(MYSQL &, Ndb_cluster_connection &);
  36. #define PRINT_ERROR(code,msg) \
  37. std::cout << "Error in " << __FILE__ << ", line: " << __LINE__ \
  38. << ", code: " << code \
  39. << ", msg: " << msg << "." << std::endl
  40. #define MYSQLERROR(mysql) { \
  41. PRINT_ERROR(mysql_errno(&mysql),mysql_error(&mysql)); \
  42. exit(-1); }
  43. #define APIERROR(error) { \
  44. PRINT_ERROR(error.code,error.message); \
  45. exit(-1); }
  46. int main(int argc, char** argv)
  47. {
  48. if (argc != 3)
  49. {
  50. std::cout << "Arguments are <socket mysqld> <connect_string cluster>.\n";
  51. exit(-1);
  52. }
  53. // ndb_init must be called first
  54. ndb_init();
  55. // connect to mysql server and cluster and run application
  56. {
  57. char * mysqld_sock = argv[1];
  58. const char *connectstring = argv[2];
  59. // Object representing the cluster
  60. Ndb_cluster_connection cluster_connection(connectstring);
  61. // Connect to cluster management server (ndb_mgmd)
  62. if (cluster_connection.connect(4 /* retries */,
  63. 5 /* delay between retries */,
  64. 1 /* verbose */))
  65. {
  66. std::cout << "Cluster management server was not ready within 30 secs.\n";
  67. exit(-1);
  68. }
  69. // Optionally connect and wait for the storage nodes (ndbd's)
  70. if (cluster_connection.wait_until_ready(30,0) < 0)
  71. {
  72. std::cout << "Cluster was not ready within 30 secs.\n";
  73. exit(-1);
  74. }
  75. // connect to mysql server
  76. MYSQL mysql;
  77. if ( !mysql_init(&mysql) ) {
  78. std::cout << "mysql_init failed\n";
  79. exit(-1);
  80. }
  81. if ( !mysql_real_connect(&mysql, "localhost", "root", "", "",
  82. 0, mysqld_sock, 0) )
  83. MYSQLERROR(mysql);
  84. // run the application code
  85. run_application(mysql, cluster_connection);
  86. }
  87. ndb_end(0);
  88. return 0;
  89. }
  90. static void create_table(MYSQL &);
  91. static void drop_table(MYSQL &);
  92. static void do_insert(Ndb &);
  93. static void do_update(Ndb &);
  94. static void do_delete(Ndb &);
  95. static void do_read(Ndb &);
  96. static void run_application(MYSQL &mysql,
  97. Ndb_cluster_connection &cluster_connection)
  98. {
  99. /********************************************
  100. * Connect to database via mysql-c *
  101. ********************************************/
  102. mysql_query(&mysql, "CREATE DATABASE TEST_DB_1");
  103. if (mysql_query(&mysql, "USE TEST_DB_1") != 0) MYSQLERROR(mysql);
  104. create_table(mysql);
  105. /********************************************
  106. * Connect to database via NdbApi *
  107. ********************************************/
  108. // Object representing the database
  109. Ndb myNdb( &cluster_connection, "TEST_DB_1" );
  110. if (myNdb.init()) APIERROR(myNdb.getNdbError());
  111. /*
  112. * Do different operations on database
  113. */
  114. do_insert(myNdb);
  115. do_update(myNdb);
  116. do_delete(myNdb);
  117. do_read(myNdb);
  118. drop_table(mysql);
  119. mysql_query(&mysql, "DROP DATABASE TEST_DB_1");
  120. }
  121. /*********************************************************
  122. * Create a table named MYTABLENAME if it does not exist *
  123. *********************************************************/
  124. static void create_table(MYSQL &mysql)
  125. {
  126. if (mysql_query(&mysql,
  127. "CREATE TABLE"
  128. " MYTABLENAME"
  129. " (ATTR1 INT UNSIGNED NOT NULL PRIMARY KEY,"
  130. " ATTR2 INT UNSIGNED NOT NULL)"
  131. " ENGINE=NDB"))
  132. MYSQLERROR(mysql);
  133. }
  134. /***********************************
  135. * Drop a table named MYTABLENAME
  136. ***********************************/
  137. static void drop_table(MYSQL &mysql)
  138. {
  139. if (mysql_query(&mysql,
  140. "DROP TABLE"
  141. " MYTABLENAME"))
  142. MYSQLERROR(mysql);
  143. }
  144. /**************************************************************************
  145. * Using 5 transactions, insert 10 tuples in table: (0,0),(1,1),...,(9,9) *
  146. **************************************************************************/
  147. static void do_insert(Ndb &myNdb)
  148. {
  149. const NdbDictionary::Dictionary* myDict= myNdb.getDictionary();
  150. const NdbDictionary::Table *myTable= myDict->getTable("MYTABLENAME");
  151. if (myTable == NULL)
  152. APIERROR(myDict->getNdbError());
  153. for (int i = 0; i < 5; i++) {
  154. NdbTransaction *myTransaction= myNdb.startTransaction();
  155. if (myTransaction == NULL) APIERROR(myNdb.getNdbError());
  156. NdbOperation *myOperation= myTransaction->getNdbOperation(myTable);
  157. if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
  158. myOperation->insertTuple();
  159. myOperation->equal("ATTR1", i);
  160. myOperation->setValue("ATTR2", i);
  161. myOperation= myTransaction->getNdbOperation(myTable);
  162. if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
  163. myOperation->insertTuple();
  164. myOperation->equal("ATTR1", i+5);
  165. myOperation->setValue("ATTR2", i+5);
  166. if (myTransaction->execute( NdbTransaction::Commit ) == -1)
  167. APIERROR(myTransaction->getNdbError());
  168. myNdb.closeTransaction(myTransaction);
  169. }
  170. }
  171. /*****************************************************************
  172. * Update the second attribute in half of the tuples (adding 10) *
  173. *****************************************************************/
  174. static void do_update(Ndb &myNdb)
  175. {
  176. const NdbDictionary::Dictionary* myDict= myNdb.getDictionary();
  177. const NdbDictionary::Table *myTable= myDict->getTable("MYTABLENAME");
  178. if (myTable == NULL)
  179. APIERROR(myDict->getNdbError());
  180. for (int i = 0; i < 10; i+=2) {
  181. NdbTransaction *myTransaction= myNdb.startTransaction();
  182. if (myTransaction == NULL) APIERROR(myNdb.getNdbError());
  183. NdbOperation *myOperation= myTransaction->getNdbOperation(myTable);
  184. if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
  185. myOperation->updateTuple();
  186. myOperation->equal( "ATTR1", i );
  187. myOperation->setValue( "ATTR2", i+10);
  188. if( myTransaction->execute( NdbTransaction::Commit ) == -1 )
  189. APIERROR(myTransaction->getNdbError());
  190. myNdb.closeTransaction(myTransaction);
  191. }
  192. }
  193. /*************************************************
  194. * Delete one tuple (the one with primary key 3) *
  195. *************************************************/
  196. static void do_delete(Ndb &myNdb)
  197. {
  198. const NdbDictionary::Dictionary* myDict= myNdb.getDictionary();
  199. const NdbDictionary::Table *myTable= myDict->getTable("MYTABLENAME");
  200. if (myTable == NULL)
  201. APIERROR(myDict->getNdbError());
  202. NdbTransaction *myTransaction= myNdb.startTransaction();
  203. if (myTransaction == NULL) APIERROR(myNdb.getNdbError());
  204. NdbOperation *myOperation= myTransaction->getNdbOperation(myTable);
  205. if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
  206. myOperation->deleteTuple();
  207. myOperation->equal( "ATTR1", 3 );
  208. if (myTransaction->execute(NdbTransaction::Commit) == -1)
  209. APIERROR(myTransaction->getNdbError());
  210. myNdb.closeTransaction(myTransaction);
  211. }
  212. /*****************************
  213. * Read and print all tuples *
  214. *****************************/
  215. static void do_read(Ndb &myNdb)
  216. {
  217. const NdbDictionary::Dictionary* myDict= myNdb.getDictionary();
  218. const NdbDictionary::Table *myTable= myDict->getTable("MYTABLENAME");
  219. if (myTable == NULL)
  220. APIERROR(myDict->getNdbError());
  221. std::cout << "ATTR1 ATTR2" << std::endl;
  222. for (int i = 0; i < 10; i++) {
  223. NdbTransaction *myTransaction= myNdb.startTransaction();
  224. if (myTransaction == NULL) APIERROR(myNdb.getNdbError());
  225. NdbOperation *myOperation= myTransaction->getNdbOperation(myTable);
  226. if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
  227. myOperation->readTuple(NdbOperation::LM_Read);
  228. myOperation->equal("ATTR1", i);
  229. NdbRecAttr *myRecAttr= myOperation->getValue("ATTR2", NULL);
  230. if (myRecAttr == NULL) APIERROR(myTransaction->getNdbError());
  231. if(myTransaction->execute( NdbTransaction::Commit ) == -1)
  232. APIERROR(myTransaction->getNdbError());
  233. if (myTransaction->getNdbError().classification == NdbError::NoDataFound)
  234. if (i == 3)
  235. std::cout << "Detected that deleted tuple doesn't exist!" << std::endl;
  236. else
  237. APIERROR(myTransaction->getNdbError());
  238. if (i != 3) {
  239. printf(" %2d %2d\n", i, myRecAttr->u_32_value());
  240. }
  241. myNdb.closeTransaction(myTransaction);
  242. }
  243. }