Skip to content

Commit 7c64657

Browse files
authored
Add config option to load external DDL file instead of default (#192)
1 parent 16e0ad6 commit 7c64657

File tree

8 files changed

+127
-16
lines changed

8 files changed

+127
-16
lines changed

src/main/java/com/oltpbenchmark/DBWorkload.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public static void main(String[] args) throws Exception {
139139
wrkld.setIsolationMode(xmlConfig.getString("isolation" + pluginTest, isolationMode));
140140
wrkld.setScaleFactor(xmlConfig.getDouble("scalefactor", 1.0));
141141
wrkld.setDataDir(xmlConfig.getString("datadir", "."));
142+
wrkld.setDDLPath(xmlConfig.getString("ddlpath", null));
142143

143144
double selectivity = -1;
144145
try {

src/main/java/com/oltpbenchmark/WorkloadConfiguration.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class WorkloadConfiguration {
4848
private TransactionTypes transTypes = null;
4949
private int isolationMode = Connection.TRANSACTION_SERIALIZABLE;
5050
private String dataDir = null;
51+
private String ddlPath = null;
5152

5253
public String getBenchmarkName() {
5354
return benchmarkName;
@@ -210,6 +211,20 @@ public void setDataDir(String dir) {
210211
this.dataDir = dir;
211212
}
212213

214+
/**
215+
* Return the path in which we can find the ddl script.
216+
*/
217+
public String getDDLPath() {
218+
return this.ddlPath;
219+
}
220+
221+
/**
222+
* Set the path in which we can find the ddl script.
223+
*/
224+
public void setDDLPath(String ddlPath) {
225+
this.ddlPath = ddlPath;
226+
}
227+
213228
/**
214229
* A utility method that init the phaseIterator and dialectMap
215230
*/

src/main/java/com/oltpbenchmark/api/BenchmarkModule.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.slf4j.Logger;
3030
import org.slf4j.LoggerFactory;
3131

32-
import java.io.File;
3332
import java.io.IOException;
3433
import java.io.InputStream;
3534
import java.sql.Connection;
@@ -221,15 +220,18 @@ public final void createDatabase() throws SQLException, IOException {
221220
*/
222221
public final void createDatabase(DatabaseType dbType, Connection conn) throws SQLException, IOException {
223222

224-
String ddlPath = this.getDatabaseDDLPath(dbType);
225223
ScriptRunner runner = new ScriptRunner(conn, true, true);
226224

227-
if (LOG.isDebugEnabled()) {
225+
if (workConf.getDDLPath() != null) {
226+
String ddlPath = workConf.getDDLPath();
227+
LOG.warn("Overriding default DDL script path");
228228
LOG.debug("Executing script [{}] for database type [{}]", ddlPath, dbType);
229+
runner.runExternalScript(ddlPath);
230+
} else {
231+
String ddlPath = this.getDatabaseDDLPath(dbType);
232+
LOG.debug("Executing script [{}] for database type [{}]", ddlPath, dbType);
233+
runner.runScript(ddlPath);
229234
}
230-
231-
runner.runScript(ddlPath);
232-
233235
}
234236

235237

src/main/java/com/oltpbenchmark/catalog/HSQLDBCatalog.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import org.apache.commons.io.IOUtils;
88

99
import java.io.IOException;
10+
import java.net.URL;
1011
import java.nio.charset.Charset;
12+
import java.nio.file.Path;
1113
import java.sql.*;
1214
import java.util.*;
1315
import java.util.regex.Matcher;
@@ -206,13 +208,21 @@ private void init() throws SQLException, IOException {
206208
*/
207209
Map<String, String> getOriginalTableNames() {
208210
// Get the contents of the HSQLDB DDL for the current benchmark.
209-
String ddlPath = this.benchmarkModule.getDatabaseDDLPath(DatabaseType.HSQLDB);
210211
String ddlContents;
211212
try {
212-
ddlContents = IOUtils.toString(Objects.requireNonNull(this.getClass().getResource(ddlPath)), Charset.defaultCharset());
213+
String ddlPath = this.benchmarkModule.getWorkloadConfiguration().getDDLPath();
214+
URL ddlURL;
215+
if (ddlPath == null) {
216+
ddlPath = this.benchmarkModule.getDatabaseDDLPath(DatabaseType.HSQLDB);
217+
ddlURL = Objects.requireNonNull(this.getClass().getResource(ddlPath));
218+
} else {
219+
ddlURL = Path.of(ddlPath).toUri().toURL();
220+
}
221+
ddlContents = IOUtils.toString(ddlURL, Charset.defaultCharset());
213222
} catch (IOException e) {
214223
throw new RuntimeException(e);
215224
}
225+
216226
// Extract and map the original table names to their uppercase versions.
217227
Map<String, String> originalTableNames = new HashMap<>();
218228
Pattern p = Pattern.compile("CREATE[\\s]+TABLE[\\s]+(.*?)[\\s]+", Pattern.CASE_INSENSITIVE);

src/main/java/com/oltpbenchmark/util/ScriptRunner.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,37 @@ public ScriptRunner(Connection connection, boolean autoCommit, boolean stopOnErr
5151
}
5252

5353

54+
public void runExternalScript(String path) throws IOException, SQLException {
55+
56+
LOG.debug("trying to find external file by path {}", path);
57+
58+
try (FileReader reader = new FileReader(path)) {
59+
60+
runScript(reader);
61+
}
62+
}
63+
5464
public void runScript(String path) throws IOException, SQLException {
5565

5666
LOG.debug("trying to find file by path {}", path);
5767

5868
try (InputStream in = this.getClass().getResourceAsStream(path);
5969
Reader reader = new InputStreamReader(in)) {
6070

61-
boolean originalAutoCommit = connection.getAutoCommit();
71+
runScript(reader);
72+
}
73+
}
6274

63-
try {
64-
if (originalAutoCommit != this.autoCommit) {
65-
connection.setAutoCommit(this.autoCommit);
66-
}
67-
runScript(connection, reader);
68-
} finally {
69-
connection.setAutoCommit(originalAutoCommit);
75+
private void runScript(Reader reader) throws IOException, SQLException {
76+
boolean originalAutoCommit = connection.getAutoCommit();
77+
78+
try {
79+
if (originalAutoCommit != this.autoCommit) {
80+
connection.setAutoCommit(this.autoCommit);
7081
}
82+
runScript(connection, reader);
83+
} finally {
84+
connection.setAutoCommit(originalAutoCommit);
7185
}
7286
}
7387

src/test/java/com/oltpbenchmark/api/AbstractTestCase.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,21 @@ public abstract class AbstractTestCase<T extends BenchmarkModule> extends TestCa
6060

6161
protected final boolean createDatabase;
6262
protected final boolean loadDatabase;
63+
protected final String ddlOverridePath;
6364

6465
private static final AtomicInteger portCounter = new AtomicInteger(9001);
6566

6667

6768
public AbstractTestCase(boolean createDatabase, boolean loadDatabase) {
6869
this.createDatabase = createDatabase;
6970
this.loadDatabase = loadDatabase;
71+
this.ddlOverridePath = null;
72+
}
73+
74+
public AbstractTestCase(boolean createDatabase, boolean loadDatabase, String ddlOverridePath) {
75+
this.createDatabase = createDatabase;
76+
this.loadDatabase = loadDatabase;
77+
this.ddlOverridePath = ddlOverridePath;
7078
}
7179

7280
public abstract List<Class<? extends Procedure>> procedures();
@@ -112,6 +120,7 @@ protected final void setUp() throws Exception {
112120
this.workConf.setTerminals(1);
113121
this.workConf.setBatchSize(128);
114122
this.workConf.setBenchmarkName(BenchmarkModule.convertBenchmarkClassToBenchmarkName(benchmarkClass()));
123+
this.workConf.setDDLPath(this.ddlOverridePath);
115124

116125
customWorkloadConfiguration(this.workConf);
117126

src/test/java/com/oltpbenchmark/api/MockBenchmark.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public MockBenchmark() {
2727
this.workConf.setBenchmarkName("mockbenchmark");
2828
}
2929

30+
public MockBenchmark(WorkloadConfiguration workConf) {
31+
super(workConf);
32+
}
33+
3034
@Override
3135
protected Package getProcedurePackageImpl() {
3236
return null;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.oltpbenchmark.api;
2+
3+
import com.oltpbenchmark.catalog.Table;
4+
import com.oltpbenchmark.util.SQLUtil;
5+
6+
import java.nio.file.Paths;
7+
import java.sql.ResultSet;
8+
import java.sql.Statement;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
public class TestDDLOverride extends AbstractTestCase<MockBenchmark> {
13+
14+
public TestDDLOverride() {
15+
super(false, false, Paths.get("src", "test", "resources", "benchmarks", "mockbenchmark", "ddl-hsqldb.sql").toAbsolutePath().toString());
16+
}
17+
18+
@Override
19+
public List<Class<? extends Procedure>> procedures() {
20+
return new ArrayList<>();
21+
}
22+
23+
@Override
24+
public Class<MockBenchmark> benchmarkClass() {
25+
return MockBenchmark.class;
26+
}
27+
28+
@Override
29+
public List<String> ignorableTables() {
30+
return null;
31+
}
32+
33+
public void testCreateWithDdlOverride() throws Exception {
34+
this.benchmark.createDatabase();
35+
36+
assertFalse("Failed to get table names for " + benchmark.getBenchmarkName().toUpperCase(), this.catalog.getTables().isEmpty());
37+
for (Table table : this.catalog.getTables()) {
38+
String tableName = table.getName();
39+
Table catalog_tbl = this.catalog.getTable(tableName);
40+
41+
String sql = SQLUtil.getCountSQL(this.workConf.getDatabaseType(), catalog_tbl);
42+
43+
try (Statement stmt = conn.createStatement();
44+
ResultSet result = stmt.executeQuery(sql);) {
45+
46+
assertNotNull(result);
47+
48+
boolean adv = result.next();
49+
assertTrue(sql, adv);
50+
51+
int count = result.getInt(1);
52+
assertEquals(0, count);
53+
}
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)