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