001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.lang.WiscVTI
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 org.apache.derby.vti.VTITemplate;
025:
026: import java.sql.SQLException;
027: import java.sql.ResultSetMetaData;
028: import java.sql.DriverManager;
029: import java.sql.Connection;
030:
031: /**
032: * This class is a VTI for loading data into the Wisconsin benchmark schema.
033: * See The Benchmark Handbook, Second Edition (edited by Jim Gray).
034: */
035: public class WiscVTI extends VTITemplate {
036:
037: int numrows;
038: int prime;
039: int generator;
040: int rowsReturned = 0;
041:
042: int unique1;
043: int unique2;
044: int two;
045: int four;
046: int ten;
047: int twenty;
048: int onePercent;
049: int tenPercent;
050: int twentyPercent;
051: int fiftyPercent;
052: int unique3;
053: int evenOnePercent;
054: int oddOnePercent;
055: String stringu1;
056: String stringu2;
057: String string4;
058:
059: int seed;
060: static final String[] cyclicStrings = {
061: "AAAAxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
062: "HHHHxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
063: "OOOOxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
064: "VVVVxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" };
065:
066: boolean closed = false;
067:
068: static final WiscMetaData metaData = new WiscMetaData();
069:
070: public WiscVTI(int numrows) throws SQLException {
071: this .numrows = numrows;
072:
073: /* Choose prime and generator values for the desired table size */
074: if (numrows <= 1000) {
075: generator = 279;
076: prime = 1009;
077: } else if (numrows <= 10000) {
078: generator = 2969;
079: prime = 10007;
080: } else if (numrows <= 100000) {
081: generator = 21395;
082: prime = 100003;
083: } else if (numrows <= 1000000) {
084: generator = 2107;
085: prime = 1000003;
086: } else if (numrows <= 10000000) {
087: generator = 211;
088: prime = 10000019;
089: } else if (numrows <= 100000000) {
090: generator = 21;
091: prime = 100000007;
092: } else {
093: throw new SQLException(
094: "Too many rows - maximum is 100000000, " + numrows
095: + " requested.");
096: }
097:
098: seed = generator;
099: }
100:
101: public ResultSetMetaData getMetaData() {
102: return metaData;
103: }
104:
105: public boolean next() throws SQLException {
106: if (closed) {
107: throw new SQLException("next() call on a closed result set");
108: }
109:
110: if (rowsReturned >= numrows)
111: return false;
112:
113: seed = rand(seed, numrows);
114:
115: unique1 = seed - 1;
116: unique2 = rowsReturned;
117: two = unique1 % 2;
118: four = unique1 % 4;
119: ten = unique1 % 10;
120: twenty = unique1 % 20;
121: onePercent = unique1 % 100;
122: tenPercent = unique1 % 10;
123: twentyPercent = unique1 % 5;
124: fiftyPercent = unique1 % 2;
125: unique3 = unique1;
126: evenOnePercent = onePercent * 2;
127: oddOnePercent = evenOnePercent + 1;
128: stringu1 = uniqueString(unique1);
129: stringu2 = uniqueString(unique2);
130: string4 = cyclicStrings[rowsReturned % cyclicStrings.length];
131:
132: rowsReturned++;
133:
134: return true;
135: }
136:
137: public int getInt(int columnIndex) throws SQLException {
138: if (closed) {
139: throw new SQLException(
140: "getInt() call on a closed result set");
141: }
142:
143: switch (columnIndex) {
144: case 1:
145: return unique1;
146:
147: case 2:
148: return unique2;
149:
150: case 3:
151: return two;
152:
153: case 4:
154: return four;
155:
156: case 5:
157: return ten;
158:
159: case 6:
160: return twenty;
161:
162: case 7:
163: return onePercent;
164:
165: case 8:
166: return tenPercent;
167:
168: case 9:
169: return twentyPercent;
170:
171: case 10:
172: return fiftyPercent;
173:
174: case 11:
175: return unique3;
176:
177: case 12:
178: return evenOnePercent;
179:
180: case 13:
181: return oddOnePercent;
182:
183: default:
184: throw new SQLException("getInt() invalid for column "
185: + columnIndex);
186: }
187: }
188:
189: public String getString(int columnIndex) throws SQLException {
190: if (closed) {
191: throw new SQLException(
192: "getString() call on a closed result set");
193: }
194:
195: switch (columnIndex) {
196: case 14:
197: return stringu1;
198:
199: case 15:
200: return stringu2;
201:
202: case 16:
203: return string4;
204:
205: default:
206: throw new SQLException("getString() invalid for column "
207: + columnIndex);
208: }
209: }
210:
211: public void close() {
212: closed = true;
213: }
214:
215: private int rand(int seed, int limit) {
216: do {
217: seed = (generator * seed) % prime;
218: } while (seed > limit);
219:
220: return seed;
221: }
222:
223: private static final char[] chars = { 'A', 'B', 'C', 'D', 'E', 'F',
224: 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
225: 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
226:
227: private String uniqueString(int unique) {
228: int i;
229: int rem;
230: char[] retval = new char[52];
231:
232: // First set result string to
233: // "AAAAAAA "
234: for (i = 0; i < 7; i++) {
235: retval[i] = 'A';
236: }
237: for (i = 7; i < retval.length; i++) {
238: retval[i] = 'x';
239: }
240:
241: // Convert unique value from right to left into an alphabetic string
242: i = 6;
243: while (unique > 0) {
244: rem = unique % 26;
245: retval[i] = chars[rem];
246: unique /= 26;
247: i--;
248: }
249:
250: return new String(retval);
251: }
252:
253: public static void WISCInsertWOConnection(int numrows,
254: String tableName) throws SQLException {
255:
256: Connection conn = DriverManager
257: .getConnection("jdbc:default:connection");
258: WISCInsert wi = new WISCInsert();
259: wi.doWISCInsert(numrows, tableName, conn);
260: }
261:
262: }
|