-
Notifications
You must be signed in to change notification settings - Fork 194
/
Copy pathCHBenCHmarkLoader.java
328 lines (249 loc) · 10.3 KB
/
CHBenCHmarkLoader.java
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
/*
* Copyright 2020 by OLTPBenchmark Project
*
* Licensed 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 com.oltpbenchmark.benchmarks.chbenchmark;
import com.oltpbenchmark.api.Loader;
import com.oltpbenchmark.api.LoaderThread;
import com.oltpbenchmark.benchmarks.chbenchmark.pojo.Nation;
import com.oltpbenchmark.benchmarks.chbenchmark.pojo.Region;
import com.oltpbenchmark.benchmarks.chbenchmark.pojo.Supplier;
import com.oltpbenchmark.util.RandomGenerator;
import org.apache.commons.io.IOUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.concurrent.CountDownLatch;
public class CHBenCHmarkLoader extends Loader<CHBenCHmark> {
private static final RandomGenerator ran = new RandomGenerator();
//create possible keys for n_nationkey ([a-zA-Z0-9])
private static final int[] nationkeys = new int[62];
static {
for (char i = 0; i < 10; i++) {
nationkeys[i] = '0' + i;
}
for (char i = 0; i < 26; i++) {
nationkeys[i + 10] = 'A' + i;
}
for (char i = 0; i < 26; i++) {
nationkeys[i + 36] = 'a' + i;
}
}
public CHBenCHmarkLoader(CHBenCHmark benchmark) {
super(benchmark);
}
@Override
public List<LoaderThread> createLoaderThreads() {
List<LoaderThread> threads = new ArrayList<>();
final CountDownLatch regionLatch = new CountDownLatch(1);
threads.add(new LoaderThread(this.benchmark) {
@Override
public void load(Connection conn) throws SQLException {
try (PreparedStatement statement = conn.prepareStatement("INSERT INTO region " + " (r_regionkey, r_name, r_comment) " + "VALUES (?, ?, ?)")) {
loadRegions(conn, statement);
}
}
@Override
public void afterLoad() {
regionLatch.countDown();
}
});
final CountDownLatch nationLatch = new CountDownLatch(1);
threads.add(new LoaderThread(this.benchmark) {
@Override
public void load(Connection conn) throws SQLException {
try (PreparedStatement statement = conn.prepareStatement("INSERT INTO nation " + " (n_nationkey, n_name, n_regionkey, n_comment) " + "VALUES (?, ?, ?, ?)")) {
loadNations(conn, statement);
}
}
@Override
public void beforeLoad() {
try {
regionLatch.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
@Override
public void afterLoad() {
nationLatch.countDown();
}
});
threads.add(new LoaderThread(this.benchmark) {
@Override
public void load(Connection conn) throws SQLException {
try (PreparedStatement statement = conn.prepareStatement("INSERT INTO supplier " + " (su_suppkey, su_name, su_address, su_nationkey, su_phone, su_acctbal, su_comment) " + "VALUES (?, ?, ?, ?, ?, ?, ?)")) {
loadSuppliers(conn, statement);
}
}
@Override
public void beforeLoad() {
try {
nationLatch.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
return threads;
}
private void truncateTable(Connection conn, String strTable) {
LOG.debug("Truncating '{}' ...", strTable);
try (Statement statement = conn.createStatement()) {
statement.execute("DELETE FROM " + strTable);
} catch (SQLException se) {
LOG.debug(se.getMessage());
}
}
private int loadRegions(Connection conn, PreparedStatement statement) throws SQLException {
int k = 0;
int t = 0;
BufferedReader br = null;
truncateTable(conn, "region");
truncateTable(conn, "nation");
truncateTable(conn, "supplier");
Region region = new Region();
final String path = "/benchmarks/" + this.benchmark.getBenchmarkName() + "/region_gen.tbl";
try (InputStream resourceAsStream = this.getClass().getResourceAsStream(path)) {
List<String> lines = IOUtils.readLines(resourceAsStream, Charset.defaultCharset());
for (String line : lines) {
StringTokenizer st = new StringTokenizer(line, "|");
if (!st.hasMoreTokens()) {
LOG.error("invalid input file: {}", path);
}
region.r_regionkey = Integer.parseInt(st.nextToken());
if (!st.hasMoreTokens()) {
LOG.error("invalid input file: {}", path);
}
region.r_name = st.nextToken();
if (!st.hasMoreTokens()) {
LOG.error("invalid input file: {}", path);
}
region.r_comment = st.nextToken();
if (st.hasMoreTokens()) {
LOG.error("invalid input file: {}", path);
}
k++;
statement.setLong(1, region.r_regionkey);
statement.setString(2, region.r_name);
statement.setString(3, region.r_comment);
statement.addBatch();
if ((k % workConf.getBatchSize()) == 0) {
statement.executeBatch();
statement.clearBatch();
}
}
statement.executeBatch();
statement.clearBatch();
} catch (SQLException se) {
LOG.debug(se.getMessage());
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
return (k);
}
private int loadNations(Connection conn, PreparedStatement statement) {
int k = 0;
int t = 0;
Nation nation = new Nation();
final String path = "/benchmarks/" + this.benchmark.getBenchmarkName() + "/nation_gen.tbl";
try (final InputStream resourceAsStream = this.getClass().getResourceAsStream(path)) {
List<String> lines = IOUtils.readLines(resourceAsStream, Charset.defaultCharset());
for (String line : lines) {
StringTokenizer st = new StringTokenizer(line, "|");
if (!st.hasMoreTokens()) {
LOG.error("invalid input file: {}", path);
}
nation.n_nationkey = Integer.parseInt(st.nextToken());
if (!st.hasMoreTokens()) {
LOG.error("invalid input file: {}", path);
}
nation.n_name = st.nextToken();
if (!st.hasMoreTokens()) {
LOG.error("invalid input file: {}", path);
}
nation.n_regionkey = Integer.parseInt(st.nextToken());
if (!st.hasMoreTokens()) {
LOG.error("invalid input file: {}", path);
}
nation.n_comment = st.nextToken();
if (st.hasMoreTokens()) {
LOG.error("invalid input file: {}", path);
}
k++;
statement.setLong(1, nation.n_nationkey);
statement.setString(2, nation.n_name);
statement.setLong(3, nation.n_regionkey);
statement.setString(4, nation.n_comment);
statement.addBatch();
if ((k % workConf.getBatchSize()) == 0) {
statement.executeBatch();
statement.clearBatch();
}
}
statement.executeBatch();
statement.clearBatch();
} catch (SQLException se) {
LOG.debug(se.getMessage());
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
return (k);
}
private int loadSuppliers(Connection conn, PreparedStatement statement) {
int k = 0;
int t = 0;
try {
Supplier supplier = new Supplier();
for (int index = 1; index <= 10000; index++) {
supplier.su_suppkey = index;
supplier.su_name = ran.astring(25, 25);
supplier.su_address = ran.astring(20, 40);
supplier.su_nationkey = nationkeys[ran.number(0, 61)];
supplier.su_phone = ran.nstring(15, 15);
supplier.su_acctbal = (float) ran.fixedPoint(2, 10000., 1000000000.);
supplier.su_comment = ran.astring(51, 101);
k++;
statement.setLong(1, supplier.su_suppkey);
statement.setString(2, supplier.su_name);
statement.setString(3, supplier.su_address);
statement.setLong(4, supplier.su_nationkey);
statement.setString(5, supplier.su_phone);
statement.setDouble(6, supplier.su_acctbal);
statement.setString(7, supplier.su_comment);
statement.addBatch();
if ((k % workConf.getBatchSize()) == 0) {
statement.executeBatch();
statement.clearBatch();
}
}
statement.executeBatch();
statement.clearBatch();
} catch (SQLException se) {
LOG.debug(se.getMessage());
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
return (k);
}
}