MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dbPopulate.cpp
1 /*
2  Copyright (C) 2005, 2006 MySQL AB, 2008, 2009 Sun Microsystems, Inc.
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 /***************************************************************
20 * I N C L U D E D F I L E S *
21 ***************************************************************/
22 
23 #include <ndb_global.h>
24 
25 #include "userInterface.h"
26 
27 #include "dbPopulate.h"
28 #include <NdbOut.hpp>
29 #include <random.h>
30 
31 /***************************************************************
32 * L O C A L C O N S T A N T S *
33 ***************************************************************/
34 
35 /***************************************************************
36 * L O C A L D A T A S T R U C T U R E S *
37 ***************************************************************/
38 
39 /***************************************************************
40 * L O C A L F U N C T I O N S *
41 ***************************************************************/
42 
43 static void getRandomSubscriberData(int subscriberNo,
44  SubscriberNumber number,
45  SubscriberName name);
46 
47 static void populate(const char *title,
48  int count,
49  void (*func)(UserHandle*,int),
50  UserHandle *uh);
51 
52 static void populateServers(UserHandle *uh, int count);
53 static void populateSubscribers(UserHandle *uh, int count);
54 static void populateGroups(UserHandle *uh, int count);
55 
56 /***************************************************************
57 * L O C A L D A T A *
58 ***************************************************************/
59 
60 static SequenceValues permissionsDefinition[] = {
61  {90, 1},
62  {10, 0},
63  {0, 0}
64 };
65 
66 /***************************************************************
67 * P U B L I C D A T A *
68 ***************************************************************/
69 
70 
71 /***************************************************************
72 ****************************************************************
73 * L O C A L F U N C T I O N S C O D E S E C T I O N *
74 ****************************************************************
75 ***************************************************************/
76 
77 static void getRandomSubscriberData(int subscriberNo,
78  SubscriberNumber number,
79  SubscriberName name)
80 {
81  char sbuf[SUBSCRIBER_NUMBER_LENGTH + 1];
82  sprintf(sbuf, "%.*d", SUBSCRIBER_NUMBER_LENGTH, subscriberNo);
83  memcpy(number, sbuf, SUBSCRIBER_NUMBER_LENGTH);
84 
85  memset(name, myRandom48(26)+'A', SUBSCRIBER_NAME_LENGTH);
86 }
87 
88 static void populate(const char *title,
89  int count,
90  void (*func)(UserHandle*, int),
91  UserHandle *uh)
92 {
93  ndbout_c("Populating %d '%s' ... ",count, title);
94  /* fflush(stdout); */
95  func(uh,count);
96  ndbout_c("done");
97 }
98 
99 static void populateServers(UserHandle *uh, int count)
100 {
101  int i, j;
102  int len;
103  char tmp[80];
104  int suffix_length = 1;
105  ServerName serverName;
106  SubscriberSuffix suffix;
107 
108  int commitCount = 0;
109 
110  for(i = 0; i < SUBSCRIBER_NUMBER_SUFFIX_LENGTH; i++)
111  suffix_length *= 10;
112 
113  for(i = 0; i < count; i++) {
114  sprintf(tmp, "-Server %d-", i);
115 
116  len = strlen(tmp);
117  for(j = 0; j < SERVER_NAME_LENGTH; j++){
118  serverName[j] = tmp[j % len];
119  }
120  /* serverName[j] = 0; not null-terminated */
121 
122  for(j = 0; j < suffix_length; j++){
123  char sbuf[SUBSCRIBER_NUMBER_SUFFIX_LENGTH + 1];
124  sprintf(sbuf, "%.*d", SUBSCRIBER_NUMBER_SUFFIX_LENGTH, j);
125  memcpy(suffix, sbuf, SUBSCRIBER_NUMBER_SUFFIX_LENGTH);
126  userDbInsertServer(uh, i, suffix, serverName);
127  commitCount ++;
128  if((commitCount % OP_PER_TRANS) == 0)
129  userDbCommit(uh);
130  }
131  }
132  if((commitCount % OP_PER_TRANS) != 0)
133  userDbCommit(uh);
134 }
135 
136 static void populateSubscribers(UserHandle *uh, int count)
137 {
138  SubscriberNumber number;
139  SubscriberName name;
140  int i, j, k;
141  int res;
142 
143  SequenceValues values[NO_OF_GROUPS+1];
144  RandomSequence seq;
145 
146  for(i = 0; i < NO_OF_GROUPS; i++) {
147  values[i].length = 1;
148  values[i].value = i;
149  }
150 
151  values[i].length = 0;
152  values[i].value = 0;
153 
154  if( initSequence(&seq, values) != 0 ) {
155  ndbout_c("could not set the sequence of random groups");
156  exit(0);
157  }
158 
159 #define RETRIES 25
160 
161  for(i = 0; i < count; i+= OP_PER_TRANS) {
162  for(j = 0; j<RETRIES; j++){
163  for(k = 0; k<OP_PER_TRANS && i+k < count; k++){
164  getRandomSubscriberData(i+k, number, name);
165  userDbInsertSubscriber(uh, number, getNextRandom(&seq), name);
166  }
167  res = userDbCommit(uh);
168  if(res == 0)
169  break;
170  if(res != 1){
171  ndbout_c("Terminating");
172  exit(0);
173  }
174  }
175  if(j == RETRIES){
176  ndbout_c("Terminating");
177  exit(0);
178  }
179  }
180 }
181 
182 static void populateGroups(UserHandle *uh, int count)
183 {
184  int i;
185  int j;
186  int len;
187  RandomSequence seq;
188  Permission allow[NO_OF_GROUPS];
189  ServerBit serverBit;
190  GroupName groupName;
191  char tmp[80];
192  int commitCount = 0;
193 
194  if( initSequence(&seq, permissionsDefinition) != 0 ) {
195  ndbout_c("could not set the sequence of random permissions");
196  exit(0);
197  }
198 
199  for(i = 0; i < NO_OF_GROUPS; i++)
200  allow[i] = 0;
201 
202  for(i = 0; i < NO_OF_SERVERS; i++) {
203  serverBit = 1 << i;
204 
205  for(j = 0; j < NO_OF_GROUPS; j++ ) {
206  if( getNextRandom(&seq) )
207  allow[j] |= serverBit;
208  }
209  }
210 
211  for(i = 0; i < NO_OF_GROUPS; i++) {
212  sprintf(tmp, "-Group %d-", i);
213 
214  len = strlen(tmp);
215 
216  for(j = 0; j < GROUP_NAME_LENGTH; j++) {
217  groupName[j] = tmp[j % len];
218  }
219  /* groupName[j] = 0; not null-terminated */
220 
221  userDbInsertGroup(uh,
222  i,
223  groupName,
224  allow[i],
225  allow[i],
226  allow[i]);
227  commitCount ++;
228  if((commitCount % OP_PER_TRANS) == 0)
229  userDbCommit(uh);
230  }
231  if((commitCount % OP_PER_TRANS) != 0)
232  userDbCommit(uh);
233 }
234 
235 /***************************************************************
236 ****************************************************************
237 * P U B L I C F U N C T I O N S C O D E S E C T I O N *
238 ****************************************************************
239 ***************************************************************/
240 
241 void dbPopulate(UserHandle *uh)
242 {
243  populate("servers", NO_OF_SERVERS, populateServers, uh);
244  populate("subscribers", NO_OF_SUBSCRIBERS, populateSubscribers, uh);
245  populate("groups", NO_OF_GROUPS, populateGroups, uh);
246 }