summaryrefslogtreecommitdiff
path: root/storage/ndb/test/tools/create_index.cpp
blob: 992034a5085e06361ec43306e7dc2c74f28a6eb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* Copyright (c) 2003-2005 MySQL AB
   Use is subject to license terms

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */

#include <ndb_global.h>

#include <NdbOut.hpp>
#include <NdbApi.hpp>
#include <NDBT.hpp>

#include <getarg.h>



int 
main(int argc, const char** argv){
  ndb_init();

  const char* _dbname = "TEST_DB";
  int _help = 0;
  int _ordered = 0, _pk = 1;

  struct getargs args[] = {
    { "database", 'd', arg_string, &_dbname, "dbname", 
      "Name of database table is in"},
    { "ordered", 'o', arg_flag, &_ordered, "Create ordered index", "" },
    { "pk", 'p', arg_flag, &_pk, "Create index on primary key", "" },
    { "usage", '?', arg_flag, &_help, "Print help", "" }
  };

  int num_args = sizeof(args) / sizeof(args[0]);
  int optind = 0;
  char desc[] = 
    "<tabname>+\n"\
    "This program will create one unique hash index named ind_<tabname> "
    " for each table. The index will contain all columns in the table";
  
  if(getarg(args, num_args, argc, argv, &optind) || _help ||
     argv[optind] == NULL){
    arg_printusage(args, num_args, argv[0], desc);
    return NDBT_ProgramExit(NDBT_WRONGARGS);
  }
  
  Ndb_cluster_connection con;
  if(con.connect(12, 5, 1) != 0)
  {
    return NDBT_ProgramExit(NDBT_FAILED);
  }

  Ndb MyNdb(&con, _dbname);
  if(MyNdb.init() != 0){
    ERR(MyNdb.getNdbError());
    return NDBT_ProgramExit(NDBT_FAILED);
  }
  
  while(MyNdb.waitUntilReady() != 0)
    ndbout << "Waiting for ndb to become ready..." << endl;
  
  NdbDictionary::Dictionary * dict = MyNdb.getDictionary();
  
  for(int i = optind; i<argc; i++){
    const NdbDictionary::Table * tab = dict->getTable(argv[i]);
    if(tab == 0){
      g_err << "Unknown table: " << argv[i] << endl;
      continue;
    }
    
    if(tab->getNoOfColumns() > 16){
      g_err << "Table " <<  argv[i] << " has more than 16 columns" << endl;
    }
    
    NdbDictionary::Index ind;
    if(_ordered){
      ind.setType(NdbDictionary::Index::OrderedIndex);
      ind.setLogging(false);
    } else {
      ind.setType(NdbDictionary::Index::UniqueHashIndex);
    }
    char buf[512];
    sprintf(buf, "IND_%s_%s_%c", 
	    argv[i], (_pk ? "PK" : "FULL"), (_ordered ? 'O' : 'U'));
    ind.setName(buf);
    ind.setTable(argv[i]);
    for(int c = 0; c<tab->getNoOfColumns(); c++){
      if(!_pk || tab->getColumn(c)->getPrimaryKey())
	ind.addIndexColumn(tab->getColumn(c)->getName());
    }
    ndbout << "creating index " << buf << " on table " << argv[i] << "...";
    const int res = dict->createIndex(ind);
    if(res != 0)
      ndbout << endl << dict->getNdbError() << endl;
    else
      ndbout << "OK" << endl;
  }  
  
  return NDBT_ProgramExit(NDBT_OK);
}