001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.lang.WISCInsert
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.tests.lang;
023:
024: import java.sql.*;
025:
026: /**
027: * This class is a VTI for loading data into the Wisconsin benchmark schema.
028: * See The Benchmark Handbook, Second Edition (edited by Jim Gray).
029: */
030: public class WISCInsert {
031:
032: private static final char[] chars = { 'A', 'B', 'C', 'D', 'E', 'F',
033: 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
034: 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
035:
036: int numrows;
037: int prime;
038: int generator;
039: int rowsReturned = 0;
040:
041: int unique1;
042: int unique2;
043: int two;
044: int four;
045: int ten;
046: int twenty;
047: int onePercent;
048: int tenPercent;
049: int twentyPercent;
050: int fiftyPercent;
051: int unique3;
052: int evenOnePercent;
053: int oddOnePercent;
054: String stringu1;
055: String stringu2;
056: String string4;
057:
058: int seed;
059: static final String[] cyclicStrings = {
060: "AAAAxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
061: "HHHHxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
062: "OOOOxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
063: "VVVVxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" };
064:
065: boolean closed = false;
066:
067: public WISCInsert() {
068: }
069:
070: public int doWISCInsert(int numrows, String tableName,
071: Connection conn) throws SQLException {
072: this .numrows = numrows;
073:
074: /* Choose prime and generator values for the desired table size */
075: if (numrows <= 1000) {
076: generator = 279;
077: prime = 1009;
078: } else if (numrows <= 10000) {
079: generator = 2969;
080: prime = 10007;
081: } else if (numrows <= 100000) {
082: generator = 21395;
083: prime = 100003;
084: } else if (numrows <= 1000000) {
085: generator = 2107;
086: prime = 1000003;
087: } else if (numrows <= 10000000) {
088: generator = 211;
089: prime = 10000019;
090: } else if (numrows <= 100000000) {
091: generator = 21;
092: prime = 100000007;
093: } else {
094: throw new SQLException(
095: "Too many rows - maximum is 100000000, " + numrows
096: + " requested.");
097: }
098:
099: seed = generator;
100:
101: String insertString = "insert into "
102: + tableName
103: + " values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
104: PreparedStatement ps = conn.prepareStatement(insertString);
105:
106: // loop the insert statement
107: for (int i = 0; i < numrows; i++) {
108: next();
109: ps.setInt(1, unique1);
110: ps.setInt(2, unique2);
111: ps.setInt(3, two);
112: ps.setInt(4, four);
113: ps.setInt(5, ten);
114: ps.setInt(6, twenty);
115: ps.setInt(7, onePercent);
116: ps.setInt(8, tenPercent);
117: ps.setInt(9, twentyPercent);
118: ps.setInt(10, fiftyPercent);
119: ps.setInt(11, unique3);
120: ps.setInt(12, evenOnePercent);
121: ps.setInt(13, oddOnePercent);
122: ps.setString(14, stringu1);
123: ps.setString(15, stringu2);
124: ps.setString(16, string4);
125: ps.executeUpdate();
126: // commit every once in a while?
127: }
128: return numrows;
129: }
130:
131: public boolean next() throws SQLException {
132: if (rowsReturned >= numrows)
133: return false;
134:
135: seed = rand(seed, numrows);
136:
137: unique1 = seed - 1;
138: unique2 = rowsReturned;
139: two = unique1 % 2;
140: four = unique1 % 4;
141: ten = unique1 % 10;
142: twenty = unique1 % 20;
143: onePercent = unique1 % 100;
144: tenPercent = unique1 % 10;
145: twentyPercent = unique1 % 5;
146: fiftyPercent = unique1 % 2;
147: unique3 = unique1;
148: evenOnePercent = onePercent * 2;
149: oddOnePercent = evenOnePercent + 1;
150: stringu1 = uniqueString(unique1);
151: stringu2 = uniqueString(unique2);
152: string4 = cyclicStrings[rowsReturned % cyclicStrings.length];
153:
154: rowsReturned++;
155:
156: return true;
157: }
158:
159: private int rand(int seed, int limit) {
160: do {
161: seed = (generator * seed) % prime;
162: } while (seed > limit);
163:
164: return seed;
165: }
166:
167: private String uniqueString(int unique) {
168: int i;
169: int rem;
170: char[] retval = new char[52];
171:
172: // First set result string to
173: // "AAAAAAA "
174: for (i = 0; i < 7; i++) {
175: retval[i] = 'A';
176: }
177: for (i = 7; i < retval.length; i++) {
178: retval[i] = 'x';
179: }
180:
181: // Convert unique value from right to left into an alphabetic string
182: i = 6;
183: while (unique > 0) {
184: rem = unique % 26;
185: retval[i] = chars[rem];
186: unique /= 26;
187: i--;
188: }
189:
190: return new String(retval);
191: }
192:
193: public String getShortTestDescription() {
194: StringBuffer st = new StringBuffer(
195: "insert values into wisconsin benchmark schema.");
196: st
197: .append("See The Benchmark Handbook, Second Edition (edited by Jim Gray).");
198: return st.toString();
199: }
200:
201: public String getLongTestDescription() {
202: StringBuffer st = new StringBuffer(
203: getShortTestDescription()
204: + "\n Called from performance.wisc.WiscLoad. This is not actually a test itself. Based on a scale value by which to multiply the number of rows, the values are generated. This class is based on the vti org.apache.derbyTesting.functionTests.tests.lang.Wisc, however, this will work with any database, not just Cloudscape.");
205: return st.toString();
206:
207: }
208:
209: public boolean isCloudscapeSpecificTest() {
210: return false;
211: }
212:
213: }
|