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, version 2.0,
7 as published by the Free Software Foundation.
8
9 This program is also distributed with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have included with MySQL.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 /***************************************************************
27 * I N C L U D E D F I L E S *
28 ***************************************************************/
29
30 #include <ndb_global.h>
31
32 #include "userInterface.h"
33
34 #include "dbPopulate.h"
35 #include <NdbOut.hpp>
36 #include <random.h>
37
38 /***************************************************************
39 * L O C A L C O N S T A N T S *
40 ***************************************************************/
41
42 /***************************************************************
43 * L O C A L D A T A S T R U C T U R E S *
44 ***************************************************************/
45
46 /***************************************************************
47 * L O C A L F U N C T I O N S *
48 ***************************************************************/
49
50 static void getRandomSubscriberData(int subscriberNo,
51 SubscriberNumber number,
52 SubscriberName name);
53
54 static void populate(const char *title,
55 int count,
56 void (*func)(UserHandle*,int),
57 UserHandle *uh);
58
59 static void populateServers(UserHandle *uh, int count);
60 static void populateSubscribers(UserHandle *uh, int count);
61 static void populateGroups(UserHandle *uh, int count);
62
63 /***************************************************************
64 * L O C A L D A T A *
65 ***************************************************************/
66
67 static SequenceValues permissionsDefinition[] = {
68 {90, 1},
69 {10, 0},
70 {0, 0}
71 };
72
73 /***************************************************************
74 * P U B L I C D A T A *
75 ***************************************************************/
76
77
78 /***************************************************************
79 ****************************************************************
80 * 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 *
81 ****************************************************************
82 ***************************************************************/
83
getRandomSubscriberData(int subscriberNo,SubscriberNumber number,SubscriberName name)84 static void getRandomSubscriberData(int subscriberNo,
85 SubscriberNumber number,
86 SubscriberName name)
87 {
88 char sbuf[SUBSCRIBER_NUMBER_LENGTH + 1];
89 sprintf(sbuf, "%.*d", SUBSCRIBER_NUMBER_LENGTH, subscriberNo);
90 memcpy(number, sbuf, SUBSCRIBER_NUMBER_LENGTH);
91
92 memset(name, myRandom48(26)+'A', SUBSCRIBER_NAME_LENGTH);
93 }
94
populate(const char * title,int count,void (* func)(UserHandle *,int),UserHandle * uh)95 static void populate(const char *title,
96 int count,
97 void (*func)(UserHandle*, int),
98 UserHandle *uh)
99 {
100 ndbout_c("Populating %d '%s' ... ",count, title);
101 /* fflush(stdout); */
102 func(uh,count);
103 ndbout_c("done");
104 }
105
populateServers(UserHandle * uh,int count)106 static void populateServers(UserHandle *uh, int count)
107 {
108 int i, j;
109 int len;
110 char tmp[80];
111 int suffix_length = 1;
112 ServerName serverName;
113 SubscriberSuffix suffix;
114
115 int commitCount = 0;
116
117 for(i = 0; i < SUBSCRIBER_NUMBER_SUFFIX_LENGTH; i++)
118 suffix_length *= 10;
119
120 for(i = 0; i < count; i++) {
121 sprintf(tmp, "-Server %d-", i);
122
123 len = strlen(tmp);
124 for(j = 0; j < SERVER_NAME_LENGTH; j++){
125 serverName[j] = tmp[j % len];
126 }
127 /* serverName[j] = 0; not null-terminated */
128
129 for(j = 0; j < suffix_length; j++){
130 char sbuf[SUBSCRIBER_NUMBER_SUFFIX_LENGTH + 1];
131 sprintf(sbuf, "%.*d", SUBSCRIBER_NUMBER_SUFFIX_LENGTH, j);
132 memcpy(suffix, sbuf, SUBSCRIBER_NUMBER_SUFFIX_LENGTH);
133 userDbInsertServer(uh, i, suffix, serverName);
134 commitCount ++;
135 if((commitCount % OP_PER_TRANS) == 0)
136 userDbCommit(uh);
137 }
138 }
139 if((commitCount % OP_PER_TRANS) != 0)
140 userDbCommit(uh);
141 }
142
populateSubscribers(UserHandle * uh,int count)143 static void populateSubscribers(UserHandle *uh, int count)
144 {
145 SubscriberNumber number;
146 SubscriberName name;
147 int i, j, k;
148 int res;
149
150 SequenceValues values[NO_OF_GROUPS+1];
151 RandomSequence seq;
152
153 for(i = 0; i < NO_OF_GROUPS; i++) {
154 values[i].length = 1;
155 values[i].value = i;
156 }
157
158 values[i].length = 0;
159 values[i].value = 0;
160
161 if( initSequence(&seq, values) != 0 ) {
162 ndbout_c("could not set the sequence of random groups");
163 exit(0);
164 }
165
166 #define RETRIES 25
167
168 for(i = 0; i < count; i+= OP_PER_TRANS) {
169 for(j = 0; j<RETRIES; j++){
170 for(k = 0; k<OP_PER_TRANS && i+k < count; k++){
171 getRandomSubscriberData(i+k, number, name);
172 userDbInsertSubscriber(uh, number, getNextRandom(&seq), name);
173 }
174 res = userDbCommit(uh);
175 if(res == 0)
176 break;
177 if(res != 1){
178 ndbout_c("Terminating");
179 exit(0);
180 }
181 }
182 if(j == RETRIES){
183 ndbout_c("Terminating");
184 exit(0);
185 }
186 }
187 }
188
populateGroups(UserHandle * uh,int count)189 static void populateGroups(UserHandle *uh, int count)
190 {
191 int i;
192 int j;
193 int len;
194 RandomSequence seq;
195 Permission allow[NO_OF_GROUPS];
196 ServerBit serverBit;
197 GroupName groupName;
198 char tmp[80];
199 int commitCount = 0;
200
201 if( initSequence(&seq, permissionsDefinition) != 0 ) {
202 ndbout_c("could not set the sequence of random permissions");
203 exit(0);
204 }
205
206 for(i = 0; i < NO_OF_GROUPS; i++)
207 allow[i] = 0;
208
209 for(i = 0; i < NO_OF_SERVERS; i++) {
210 serverBit = 1 << i;
211
212 for(j = 0; j < NO_OF_GROUPS; j++ ) {
213 if( getNextRandom(&seq) )
214 allow[j] |= serverBit;
215 }
216 }
217
218 for(i = 0; i < NO_OF_GROUPS; i++) {
219 sprintf(tmp, "-Group %d-", i);
220
221 len = strlen(tmp);
222
223 for(j = 0; j < GROUP_NAME_LENGTH; j++) {
224 groupName[j] = tmp[j % len];
225 }
226 /* groupName[j] = 0; not null-terminated */
227
228 userDbInsertGroup(uh,
229 i,
230 groupName,
231 allow[i],
232 allow[i],
233 allow[i]);
234 commitCount ++;
235 if((commitCount % OP_PER_TRANS) == 0)
236 userDbCommit(uh);
237 }
238 if((commitCount % OP_PER_TRANS) != 0)
239 userDbCommit(uh);
240 }
241
242 /***************************************************************
243 ****************************************************************
244 * 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 *
245 ****************************************************************
246 ***************************************************************/
247
dbPopulate(UserHandle * uh)248 void dbPopulate(UserHandle *uh)
249 {
250 populate("servers", NO_OF_SERVERS, populateServers, uh);
251 populate("subscribers", NO_OF_SUBSCRIBERS, populateSubscribers, uh);
252 populate("groups", NO_OF_GROUPS, populateGroups, uh);
253 }
254