MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hugoLoad.cpp
1 /*
2  Copyright (C) 2003-2006 MySQL AB, 2008, 2009 Sun Microsystems, Inc.
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 
20 #include <NdbOut.hpp>
21 #include <NdbApi.hpp>
22 #include <NdbSleep.h>
23 #include <NDBT.hpp>
24 #include <HugoTransactions.hpp>
25 #include <getarg.h>
26 
27 
28 int main(int argc, const char** argv){
29  ndb_init();
30 
31  int _records = 0;
32  int _help = 0;
33  int _batch = 512;
34  int _loops = 0;
35  int _rand = 0;
36  int _onetrans = 0;
37  int _abort = 0;
38  const char* db = 0;
39 
40  struct getargs args[] = {
41  { "records", 'r', arg_integer, &_records, "Number of records", "recs" },
42  { "batch", 'b', arg_integer, &_batch, "Number of operations in each transaction", "batch" },
43  { "loops", 'l', arg_integer, &_loops, "Number of loops", "" },
44  { "database", 'd', arg_string, &db, "Database", "" },
45  { "usage", '?', arg_flag, &_help, "Print help", "" },
46  { "rnd-rows", 0, arg_flag, &_rand, "Rand number of records", "recs" },
47  { "one-trans", 0, arg_flag, &_onetrans, "Insert as 1 trans", "" },
48  { "abort", 0, arg_integer, &_abort, "Abort probability", "" }
49  };
50  int num_args = sizeof(args) / sizeof(args[0]);
51  int optind = 0;
52  char desc[] =
53  "tabname\n"\
54  "This program will load one table in Ndb with calculated data. \n"\
55  "This means that it is possible to check the validity of the data \n"\
56  "at a later time. The last column in each table is used as an update \n"\
57  "counter, it's initialised to zero and should be incremented for each \n"\
58  "update of the record. \n";
59 
60  if(getarg(args, num_args, argc, argv, &optind) ||
61  argv[optind] == NULL || _records == 0 || _help) {
62  arg_printusage(args, num_args, argv[0], desc);
63  return NDBT_ProgramExit(NDBT_WRONGARGS);
64  }
65 
66 
67  // Connect to Ndb
69  if(con.connect(12, 5, 1) != 0)
70  {
71  return NDBT_ProgramExit(NDBT_FAILED);
72  }
73 
74  if (con.wait_until_ready(30,0) < 0)
75  {
76  ndbout << "Cluster nodes not ready in 30 seconds." << endl;
77  return NDBT_ProgramExit(NDBT_FAILED);
78  }
79 
80  Ndb MyNdb( &con, db ? db : "TEST_DB" );
81 
82  if(MyNdb.init() != 0){
83  ERR(MyNdb.getNdbError());
84  return NDBT_ProgramExit(NDBT_FAILED);
85  }
86 
87  for(int i = optind; i<argc; i++)
88  {
89  const char* _tabname = argv[i];
90  // Check if table exists in db
91  const NdbDictionary::Table* pTab =
92  NDBT_Table::discoverTableFromDb(&MyNdb, _tabname);
93  if(pTab == NULL){
94  ndbout << " Table " << _tabname << " does not exist!" << endl;
95  return NDBT_ProgramExit(NDBT_WRONGARGS);
96  }
97 
98  HugoTransactions hugoTrans(*pTab);
99 loop:
100  int rows = (_rand ? rand() % _records : _records);
101  int abort = (rand() % 100) < _abort ? 1 : 0;
102  if (abort)
103  ndbout << "load+abort" << endl;
104  if (hugoTrans.loadTable(&MyNdb,
105  rows,
106  _batch,
107  true, 0, _onetrans, _loops, abort) != 0){
108  return NDBT_ProgramExit(NDBT_FAILED);
109  }
110 
111  if(_loops > 0)
112  {
113  ndbout << "clearing..." << endl;
114  hugoTrans.clearTable(&MyNdb);
115  //hugoTrans.pkDelRecords(&MyNdb, _records);
116  _loops--;
117  goto loop;
118  }
119  }
120 
121  return NDBT_ProgramExit(NDBT_OK);
122 }