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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
|
/*
*
* Derby - Class OERandom
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.apache.derbyTesting.system.oe.util;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Random;
import org.apache.derbyTesting.system.oe.client.Load;
/**
* Methods to implement the random database population types
* for the Order Entry Benchmark. The rules for generating
* the random data is per the TPC-C specification.
*/
public class OERandom {
final Random rand;
protected final int Clast;
protected final int Cid;
protected final int Citem;
/**
* Create a matching OERandom, for use in multi-threaded
* runs where all the submitters need to share the same
* Clast, Cid and Citem values.
* @param oer
*/
public OERandom(OERandom oer) {
// Since these objects may be created at the same time,
// within the resolution of currentTimeMillis(), then
// ensure they have different seeds.
rand = new Random(System.currentTimeMillis() + oer.rand.nextLong());
Clast = oer.Clast;
Cid = oer.Cid;
Citem = oer.Citem;
}
public OERandom(int last, long seed) {
rand = new Random(seed);
Clast = last;
Cid = this.randomInt(0, 255);
Citem = this.randomInt(0, 255);
initAStrings();
}
public OERandom(int last) {
this(last, System.currentTimeMillis());
}
private static int[] RESCALE = { 0, 10, 100, 1000, 10000, 100000, 1000000 };
private StringBuffer decimalString = new StringBuffer(12);
public String randomDecimalString(int start, int end, int scale) {
int val = randomInt(start, end);
int whole = val / RESCALE[scale];
int part = val % RESCALE[scale];
decimalString.setLength(0);
decimalString.append(whole);
decimalString.append('.');
int pos = decimalString.length();
decimalString.append(part);
int tempScale = decimalString.length() - pos;
if (tempScale < scale) {
for (int i = 0; i < (scale - tempScale); i++)
decimalString.insert(pos, '0');
}
return decimalString.toString();
}
/**
* Payment amount between 1.00 and 5,000.00
* @return Payment amount between 1.00 and 5,000.00
*/
public BigDecimal payment()
{
return randomDecimal(1, 500000, 2);
}
public BigDecimal randomDecimal(int start, int end, int scale) {
BigInteger bi = BigInteger.valueOf(randomInt(start, end));
return new BigDecimal(bi, scale);
}
/**
* tpcc 4.3.2.5 Implements random within [x .. y ] for int
*/
public int randomInt(int start, int end) {
double drand = rand.nextDouble();
double rrand = (drand * (end - start)) + 0.5;
return ((int) rrand) + start;
}
/**
* Return a random district [1..10]
*/
public short district()
{
return (short) randomInt(1, 10);
}
/**
* Return a random carrier [1..10]
*/
public short carrier()
{
return (short) randomInt(1, 10);
}
/**
* Return a random threshold for the stock level [10..20]
*/
public int threshold()
{
return randomInt(10, 20);
}
/**
* tpcc 4.3.2.2 (random a string)
*/
public String randomAString(int min, int max) {
int len = randomInt(min, max);
char[] c = new char[len];
for (int i = 0; i < len; i++) {
double drand = rand.nextDouble();
if (i == 0) {
if (drand < 2.0)
c[0] = (char) randomInt((int) 'A', (int) 'Z');
else {
switch (randomInt(1, 10)) {
case 1:
c[0] = '\u00c0';
break;
case 2:
c[0] = '\u00c1';
break;
case 3:
c[0] = '\u00c2';
break;
case 4:
c[0] = '\u00ca';
break;
case 5:
c[0] = '\u00cb';
break;
case 6:
c[0] = '\u00d4';
break;
case 7:
c[0] = '\u00d8';
break;
case 8:
c[0] = '\u00d1';
break;
case 9:
c[0] = '\u00cd';
break;
default:
c[0] = '\u00dc';
break;
}
}
continue;
}
if (drand < 2.0)
c[i] = (char) randomInt((int) 'a', (int) 'z');
else {
switch (randomInt(1, 10)) {
case 1:
c[i] = '\u00e2';
break;
case 2:
c[i] = '\u00e4';
break;
case 3:
c[i] = '\u00e7';
break;
case 4:
c[i] = '\u00e8';
break;
case 5:
c[i] = '\u00ec';
break;
case 6:
c[i] = '\u00ef';
break;
case 7:
c[i] = '\u00f6';
break;
case 8:
c[i] = '\u00f9';
break;
case 9:
c[i] = '\u00fc';
break;
default:
c[i] = '\u00e5';
break;
}
}
}
return new String(c);
}
/**
* tpcc 4.3.2.2 (random n string)
*/
public String randomNString(int min, int max) {
int len = randomInt(min, max);
char[] c = new char[len];
for (int i = 0; i < len; i++) {
c[i] = (char) randomInt((int) '0', (int) '9');
}
return new String(c);
}
/**
* tpcc 4.3.2.3
*/
private final String[] SYLLABLES = { "BAR", "OUGHT", "ABLE", "PRI", "PRES",
"ESE", "ANTI", "CALLY", "ATION", "EING" };
protected String randomCLast(int n) {
return SYLLABLES[n / 100] + SYLLABLES[(n / 10) % 10]
+ SYLLABLES[n % 10];
}
/**
* Generate the zipcode value
* return zipcode value according to the requirements specified in
* Clause 4.3.2.7 of TPC-C spec
*/
public String randomZIP() {
return randomNString(4, 4) + "11111";
}
/**
* Section 2.1.6 of TPC-C specification, for OL_I_ID NURand(A, x, y) =
* (((random(0, A) | random(x, y)) + C) % (y - x + 1)) + x C is a run-time
* constant randomly chosen within [0 .. A] NURand(8191, 1,100000)
*
* @return nonuniform random number
*/
public int NURand8191() {
int l = randomInt(0, 8191);
int r = randomInt(1, Load.ITEM_COUNT);
int C = randomInt(0, 8191);
return ((l | r) + C) % (Load.ITEM_COUNT - 1 + 1) + 1;
}
/**
* Section 2.1.6 of TPC-C specification for CID NURand(A, x, y) =
* (((random(0, A) | random(x, y)) + C) % (y - x + 1)) + x NURand(1023,
* 1,3000)
*
* @return nonuniform random number
*/
public int NURand1023() {
int l = randomInt(0, 1023);
int r = randomInt(1, (Load.CUSTOMER_COUNT_W / Load.DISTRICT_COUNT_W));
int C = randomInt(0, 1023);
return ((l | r) + C)
% ((Load.CUSTOMER_COUNT_W / Load.DISTRICT_COUNT_W) - 1 + 1) + 1;
}
/**
* Section 2.1.6 of TPC-C specification, for C_LAST NURand(A, x, y) =
* (((random(0, A) | random(x, y)) + C) % (y - x + 1)) + x NURand(255,0,999)
*
* @return nonuniform random number
*/
public int NURand255() {
int l = randomInt(0, 255);
int r = randomInt(0, 999);
int C = randomInt(0, 255);
return ((l | r) + C) % (999 - 0 + 1) + 0;
}
public String randomState() {
StringBuffer s = new StringBuffer(2);
for (int i = 0; i < 2; i++) {
s.append((char) randomInt((int) 'A', (int) 'Z'));
}
return s.toString();
}
/**
* Clause 4.3.2.3 of the TPC-C specification
* @param cid - customer id.
* @return the generated Customer's last name per the requirements
* in the TPC-C spec.
*/
public String randomCLastPopulate(int cid) {
// First thousand customers (C_ID is one based)
// have a fixed last name based upon the contiguous
// values from 0-999, section 4.3.3.1
if (cid <= 1000)
return randomCLast(cid-1); // range 0 - 999
return randomCLast(NURand255());
}
public String randomCLast() {
return randomCLast(NURand255());
}
/**
* Clause 4.3.3.1 of TPC-C spec. random a-string [26 .. 50]. For 10%
* of the rows, selected at random, the string "ORIGINAL" must be held by 8
* consecutive characters starting at a random position within the string
*
* @return string data per the TPC-C requirements
*/
public String randomData() {
String s = randomAString26_50();
if (rand.nextDouble() < 0.9)
return s;
int pos = randomInt(0, s.length() - 9);
if (pos == 0)
return "ORIGINAL" + s.substring(8);
if (pos == (s.length() - 9))
return s.substring(0, s.length() - 9) + "ORIGINAL";
return s.substring(0, pos) + "ORIGINAL"
+ s.substring(pos + 8, s.length());
}
public int[] randomIntPerm(int count) {
int[] data = new int[count];
for (int i = 0; i < count; i++) {
data[i] = i + 1;
}
for (int j = 0; j < (count * 4); j++) {
int a = randomInt(0, count - 1);
int b = randomInt(0, count - 1);
int val = data[a];
data[a] = data[b];
data[b] = val;
}
return data;
}
private final static String[] left24 = new String[10];
private final static String[] left300 = new String[10];
private final static String[] right200 = new String[10];
private final static String[] left10 = new String[10];
private final static String[] right10 = new String[10];
private final static String[] left14 = new String[10];
private final static String[] left26 = new String[10];
private final static String[] right24 = new String[10];
private final static String[] left8 = new String[10];
private final static String[] right8 = new String[10];
private static boolean doneInit;
private void initAStrings() {
synchronized (left24) {
if (doneInit)
return;
for (int i = 0; i < 10; i++) {
// 24..24
left24[i] = randomAString(24, 24);
// 300...500
left300[i] = randomAString(300, 300);
right200[i] = randomAString(0, 200);
// 10...20
left10[i] = randomAString(10, 10);
right10[i] = randomAString(0, 10);
// 14 .. 24
left14[i] = randomAString(10, 10);
// 26 .. 50
left26[i] = randomAString(26, 26);
right24[i] = randomAString(0, 24);
// 8 .. 16
left8[i] = randomAString(8, 8);
right8[i] = randomAString(0, 8);
}
doneInit = true;
}
}
public String randomAString24() {
return left24[randomInt(0, 9)];
}
/**
* Section 4.3.2.2(and comments 1 and 2).
* The notation random a-string [x .. y] (respectively,
* n-string [x ..y]) represents a string of random alphanumeric (respectively,
* numeric)characters of a random length of minimum x, maximum y, and mean (y+x)/2.
*
* @return string value.
*/
public String randomAString300_500() {
String l = left300[randomInt(0, 9)];
String r = right200[randomInt(0, 9)];
return l.concat(r);
}
public String randomAString10_20() {
String l = left10[randomInt(0, 9)];
String r = right10[randomInt(0, 9)];
return l.concat(r);
}
public String randomAString14_24() {
String l = left14[randomInt(0, 9)];
String r = right10[randomInt(0, 9)];
return l.concat(r);
}
public String randomAString26_50() {
String l = left26[randomInt(0, 9)];
String r = right24[randomInt(0, 9)];
return l.concat(r);
}
public String randomAString8_16() {
String l = left8[randomInt(0, 9)];
String r = right8[randomInt(0, 9)];
return l.concat(r);
}
}
|