MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
reorg_tab.cpp
1 /* Copyright (C) 2008 MySQL AB, 2009 Sun Microsystems, Inc.
2  All rights reserved. Use is subject to license terms.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
16 
17 #include <ndb_global.h>
18 
19 #include <NdbOut.hpp>
20 #include <NdbApi.hpp>
21 #include <NDBT.hpp>
22 
23 #include <getarg.h>
24 
25 int main(int argc, const char** argv){
26  ndb_init();
27 
28  int _help = 0;
29  int _p = 0;
30  const char * db = "TEST_DB";
31  const char* _connectstr = NULL;
32 
33  struct getargs args[] = {
34  { "database", 'd', arg_string, &db, "database", 0 },
35  { "connstr", 'c', arg_string, &_connectstr, "Connect string", "cs" },
36  { "partitions", 'p', arg_integer, &_p, "New no of partitions", 0},
37  { "usage", '?', arg_flag, &_help, "Print help", "" }
38  };
39  int num_args = sizeof(args) / sizeof(args[0]);
40  int optind = 0;
41  char desc[] =
42  "tabname\n" \
43  "This program will alter no of partitions of table in Ndb.\n";
44 
45  if(getarg(args, num_args, argc, argv, &optind) || _help){
46  arg_printusage(args, num_args, argv[0], desc);
47  return NDBT_ProgramExit(NDBT_WRONGARGS);
48  }
49 
50  if(argv[optind] == NULL)
51  {
52  arg_printusage(args, num_args, argv[0], desc);
53  return NDBT_ProgramExit(NDBT_WRONGARGS);
54  }
55 
56 
57  // Connect to Ndb
58  Ndb_cluster_connection con(_connectstr);
59  if(con.connect(12, 5, 1) != 0)
60  {
61  return NDBT_ProgramExit(NDBT_FAILED);
62  }
63  Ndb MyNdb(&con, db );
64 
65  if(MyNdb.init() != 0){
66  ERR(MyNdb.getNdbError());
67  return NDBT_ProgramExit(NDBT_FAILED);
68  }
69 
70  while(MyNdb.waitUntilReady() != 0)
71  ndbout << "Waiting for ndb to become ready..." << endl;
72 
73  NdbDictionary::Dictionary* MyDic = MyNdb.getDictionary();
74  for (int i = optind; i<argc; i++)
75  {
76  printf("altering %s/%s...", db, argv[i]);
77  const NdbDictionary::Table* oldTable = MyDic->getTable(argv[i]);
78 
79  if (oldTable == 0)
80  {
81  ndbout << "Failed to retrieve table " << argv[i]
82  << ": " << MyDic->getNdbError() << endl;
83  return NDBT_ProgramExit(NDBT_FAILED);
84  }
85 
86  NdbDictionary::Table newTable = *oldTable;
87  newTable.setFragmentCount(_p);
88 
89  if (MyDic->beginSchemaTrans() != 0)
90  goto err;
91 
92  if (MyDic->prepareHashMap(*oldTable, newTable) != 0)
93  goto err;
94 
95  if (MyDic->alterTable(*oldTable, newTable) != 0)
96  goto err;
97 
98  if (MyDic->endSchemaTrans())
99  goto err;
100 
101  ndbout_c("done");
102  }
103 
104  return NDBT_ProgramExit(NDBT_OK);
105 
106 err:
107  NdbError err = MyDic->getNdbError();
108  if (MyDic->hasSchemaTrans())
109  MyDic->endSchemaTrans(NdbDictionary::Dictionary::SchemaTransAbort);
110 
111  ndbout << "Failed! "
112  << err << endl;
113  return NDBT_ProgramExit(NDBT_FAILED);
114 
115 }