MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hugoLockRecords.cpp
1 /*
2  Copyright (C) 2003-2006 MySQL AB
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 #include <ndb_global.h>
20 
21 #include <NdbOut.hpp>
22 
23 #include <NdbApi.hpp>
24 #include <NdbMain.h>
25 #include <NDBT.hpp>
26 #include <NdbSleep.h>
27 #include <getarg.h>
28 
29 #include <HugoTransactions.hpp>
30 
31 int main(int argc, const char** argv){
32  ndb_init();
33 
34  int _records = 0;
35  int _loops = 1;
36  int _percentVal = 1;
37  int _lockTime = 1000;
38  const char* _tabname = NULL;
39  int _help = 0;
40 
41  struct getargs args[] = {
42  { "loops", 'l', arg_integer, &_loops, "number of times to run this program(0=infinite loop)", "loops" },
43  { "records", 'r', arg_integer, &_records, "Number of records", "recs" },
44  { "locktime", 't', arg_integer, &_lockTime, "Time in ms to hold lock(default=1000)", "ms" },
45  { "percent", 'p', arg_integer, &_percentVal, "Percent of records to lock(default=1%)", "%" },
46  { "usage", '?', arg_flag, &_help, "Print help", "" }
47  };
48  int num_args = sizeof(args) / sizeof(args[0]);
49  int optind = 0;
50  char desc[] =
51  "tabname\n"\
52  "This program will lock p% of the records in the table for x milliseconds\n"\
53  "then it will lock the next 1% and continue to do so until it has locked \n"\
54  "all records in the table\n";
55 
56  if(getarg(args, num_args, argc, argv, &optind) ||
57  argv[optind] == NULL || _records == 0 || _help) {
58  arg_printusage(args, num_args, argv[0], desc);
59  return NDBT_ProgramExit(NDBT_WRONGARGS);
60  }
61  _tabname = argv[optind];
62 
63  // Connect to Ndb
65  if(con.connect(12, 5, 1) != 0)
66  {
67  return NDBT_ProgramExit(NDBT_FAILED);
68  }
69  Ndb MyNdb(&con, "TEST_DB" );
70 
71  if(MyNdb.init() != 0){
72  ERR(MyNdb.getNdbError());
73  return NDBT_ProgramExit(NDBT_FAILED);
74  }
75 
76  while(MyNdb.waitUntilReady() != 0)
77  ndbout << "Waiting for ndb to become ready..." << endl;
78 
79  // Check if table exists in db
80  const NdbDictionary::Table * pTab = NDBT_Table::discoverTableFromDb(&MyNdb, _tabname);
81  if(pTab == NULL){
82  ndbout << " Table " << _tabname << " does not exist!" << endl;
83  return NDBT_ProgramExit(NDBT_WRONGARGS);
84  }
85 
86  HugoTransactions hugoTrans(*pTab);
87  int i = 0;
88  while (i<_loops || _loops==0) {
89  ndbout << i << ": ";
90  if (hugoTrans.lockRecords(&MyNdb, _records, _percentVal, _lockTime) != 0){
91  return NDBT_ProgramExit(NDBT_FAILED);
92  }
93  i++;
94  }
95 
96  return NDBT_ProgramExit(NDBT_OK);
97 }
98