KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > lang > WISCInsert


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.lang.WISCInsert
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.tests.lang;
23
24 import java.sql.*;
25
26 /**
27  * This class is a VTI for loading data into the Wisconsin benchmark schema.
28  * See The Benchmark Handbook, Second Edition (edited by Jim Gray).
29  */

30 public class WISCInsert {
31
32     private static final char[] chars = {
33         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
34         'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
35     };
36
37     int numrows;
38     int prime;
39     int generator;
40     int rowsReturned = 0;
41
42     int unique1;
43     int unique2;
44     int two;
45     int four;
46     int ten;
47     int twenty;
48     int onePercent;
49     int tenPercent;
50     int twentyPercent;
51     int fiftyPercent;
52     int unique3;
53     int evenOnePercent;
54     int oddOnePercent;
55     String JavaDoc stringu1;
56     String JavaDoc stringu2;
57     String JavaDoc string4;
58
59     int seed;
60     static final String JavaDoc[] cyclicStrings = {
61         "AAAAxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
62         "HHHHxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
63         "OOOOxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
64         "VVVVxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
65         };
66
67     boolean closed = false;
68
69     public WISCInsert()
70     {
71     }
72
73     public int doWISCInsert(int numrows, String JavaDoc tableName, Connection conn) throws SQLException {
74         this.numrows = numrows;
75
76         /* Choose prime and generator values for the desired table size */
77         if (numrows <= 1000) {
78             generator = 279;
79             prime = 1009;
80         } else if (numrows <= 10000) {
81             generator = 2969;
82             prime = 10007;
83         } else if (numrows <= 100000) {
84             generator = 21395;
85             prime = 100003;
86         } else if (numrows <= 1000000) {
87             generator = 2107;
88             prime = 1000003;
89         } else if (numrows <= 10000000) {
90             generator = 211;
91             prime = 10000019;
92         } else if (numrows <= 100000000) {
93             generator = 21;
94             prime = 100000007;
95         } else {
96             throw new SQLException("Too many rows - maximum is 100000000, " +
97                                     numrows + " requested.");
98         }
99
100         seed = generator;
101
102         String JavaDoc insertString = "insert into " + tableName + " values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
103         PreparedStatement ps = conn.prepareStatement(insertString);
104
105         // loop the insert statement
106
for (int i = 0; i < numrows; i++)
107         {
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
132     public boolean next() throws SQLException {
133         if (rowsReturned >= numrows)
134             return false;
135
136         seed = rand(seed, numrows);
137
138         unique1 = seed - 1;
139         unique2 = rowsReturned;
140         two = unique1 % 2;
141         four = unique1 % 4;
142         ten = unique1 % 10;
143         twenty = unique1 % 20;
144         onePercent = unique1 % 100;
145         tenPercent = unique1 % 10;
146         twentyPercent = unique1 % 5;
147         fiftyPercent = unique1 % 2;
148         unique3 = unique1;
149         evenOnePercent = onePercent * 2;
150         oddOnePercent = evenOnePercent + 1;
151         stringu1 = uniqueString(unique1);
152         stringu2 = uniqueString(unique2);
153         string4 = cyclicStrings[rowsReturned % cyclicStrings.length];
154
155         rowsReturned++;
156
157         return true;
158     }
159
160
161     private int rand(int seed, int limit) {
162         do {
163             seed = (generator * seed) % prime;
164         } while (seed > limit);
165
166         return seed;
167     }
168
169  
170     private String JavaDoc uniqueString(int unique) {
171         int i;
172         int rem;
173         char[] retval = new char[52];
174
175         // First set result string to
176
// "AAAAAAA "
177
for (i = 0; i < 7; i++) {
178             retval[i] = 'A';
179         }
180         for (i = 7; i < retval.length; i++) {
181             retval[i] = 'x';
182         }
183
184         // Convert unique value from right to left into an alphabetic string
185
i = 6;
186         while (unique > 0) {
187             rem = unique % 26;
188             retval[i] = chars[rem];
189             unique /= 26;
190             i--;
191         }
192
193         return new String JavaDoc(retval);
194     }
195
196
197     public String JavaDoc getShortTestDescription()
198     {
199         StringBuffer JavaDoc st = new StringBuffer JavaDoc( "insert values into wisconsin benchmark schema.");
200         st.append("See The Benchmark Handbook, Second Edition (edited by Jim Gray).");
201         return st.toString();
202     }
203
204
205     public String JavaDoc getLongTestDescription()
206     {
207         StringBuffer JavaDoc st = new StringBuffer JavaDoc(getShortTestDescription() +"\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.");
208         return st.toString();
209
210     }
211
212
213     public boolean isCloudscapeSpecificTest()
214     {
215         return false;
216     }
217
218 }
219
Popular Tags