MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ConfigFactory.hpp
1 /*
2  Copyright 2009 Sun Microsystems, Inc.
3 
4  All rights reserved. Use is subject to license terms.
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; version 2 of the License.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #ifndef _CONFIGFACTORY_HPP
21 #define _CONFIGFACTORY_HPP
22 
23 #include <util/Properties.hpp>
24 
26 {
27 
28  static Uint32 get_ndbt_base_port(void)
29  {
30  Uint32 port = 0;
31  const char* base_port_str = getenv("NDBT_BASE_PORT");
32  if (base_port_str)
33  port = atoi(base_port_str);
34  if (!port)
35  port = 11000; // default
36  return port;
37  }
38 
39 
40  static Properties create(unsigned mgmds = 1,
41  unsigned ndbds = 1,
42  unsigned mysqlds = 1)
43  {
44  Uint32 base_port = get_ndbt_base_port();
46  assert(mgmds >= 1 && ndbds >= 1 && mysqlds >= 1);
47  for (unsigned n = 1; n <= ndbds + mgmds + mysqlds; n++)
48  {
49  const char* node;
50  Properties node_settings;
51 
52  node_settings.put("NodeId", n);
53 
54  if (n <= mgmds)
55  {
56  node = "ndb_mgmd";
57  node_settings.put("HostName", "localhost");
58  node_settings.put("PortNumber", base_port + n);
59  } else if (n <= mgmds + ndbds)
60  {
61  node = "ndbd";
62  if (ndbds == 1)
63  node_settings.put("NoOfReplicas", 1);
64 
65  } else if (n <= mgmds + ndbds + mysqlds)
66  {
67  node = "mysqld";
68  } else
69  abort();
70 
71  config.put(node, n, &node_settings);
72  }
73  return config;
74  }
75 
76  static bool
77  put(Properties& config, const char* section, Uint32 section_no,
78  const char* key, Uint32 value)
79  {
80  Properties* p;
81  if (!config.getCopy(section, section_no, &p))
82  return false;
83  if (!p->put(key, value))
84  return false;
85  if (!config.put(section, section_no, p, true))
86  return false;
87  return true;
88  }
89 
90  static bool
91  write_config_ini(Properties& config, const char* path)
92  {
93  FILE* config_file = fopen(path, "w");
94  if (config_file == NULL)
95  return false;
96 
97  Properties::Iterator it(&config);
98 
99  while (const char* name = it.next())
100  {
101  BaseString section_name(name);
102  fprintf(config_file, "[%s]\n",
103  section_name.substr(0, section_name.lastIndexOf('_')).c_str());
104 
105  const Properties* p;
106  if (!config.get(name, &p))
107  return false;
108 
109  Properties::Iterator it2(p);
110 
111  while (const char* name2 = it2.next())
112  {
113 
114  PropertiesType type;
115  if (!p->getTypeOf(name2, &type))
116  return false;
117 
118  switch (type) {
119  case PropertiesType_Uint32:
120  {
121  Uint32 value;
122  if (!p->get(name2, &value))
123  return false;
124  fprintf(config_file, "%s=%u\n", name2, value);
125  break;
126  }
127 
128  case PropertiesType_char:
129  {
130  const char* value;
131  if (!p->get(name2, &value))
132  return false;
133  fprintf(config_file, "%s=%s\n", name2, value);
134  break;
135  }
136 
137  default:
138  abort();
139  break;
140  }
141  }
142  fprintf(config_file, "\n");
143  }
144 
145  fclose(config_file);
146  return true;
147  }
148 
149  static bool
150  create_directories(const char* path, Properties & config)
151  {
152  Properties::Iterator it(&config);
153 
154  while (const char* name = it.next())
155  {
156  BaseString dir;
157  dir.assfmt("%s/%s", path, name);
158  printf("Creating %s...\n", dir.c_str());
159  if (!NdbDir::create(dir.c_str()))
160  return false;
161  }
162  return true;
163  }
164 
165 };
166 
167 #endif /* _CONFIGFACTORY_HPP */
168