Skip to content

Commit 9834ead

Browse files
authored
Add ability to load after scripts from local filesystem (#600)
Still fallsback to loading from resource stream when file is not found. So far logging is only done to the debug stream. Quick (hackish) fix to close #595 - [x] Add unit test
1 parent 15ad613 commit 9834ead

File tree

4 files changed

+77
-7
lines changed

4 files changed

+77
-7
lines changed

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

+39-6
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
import com.oltpbenchmark.catalog.AbstractCatalog;
2020
import com.oltpbenchmark.types.DatabaseType;
2121
import com.oltpbenchmark.util.ClassUtil;
22+
import com.oltpbenchmark.util.FileUtil;
2223
import com.oltpbenchmark.util.SQLUtil;
2324
import com.oltpbenchmark.util.ScriptRunner;
2425
import com.oltpbenchmark.util.ThreadUtil;
26+
import java.io.FileNotFoundException;
2527
import java.io.IOException;
2628
import java.io.InputStream;
2729
import java.sql.Connection;
@@ -89,8 +91,22 @@ public final Connection makeConnection() throws SQLException {
8991

9092
private String afterLoadScriptPath = null;
9193

92-
public final void setAfterLoadScriptPath(String scriptPath) {
93-
this.afterLoadScriptPath = scriptPath;
94+
public final void setAfterLoadScriptPath(String scriptPath) throws FileNotFoundException {
95+
if (scriptPath != null) scriptPath = scriptPath.trim();
96+
try {
97+
this.afterLoadScriptPath = FileUtil.checkPath(scriptPath, "afterload");
98+
return;
99+
} catch (FileNotFoundException ex) {
100+
this.afterLoadScriptPath = null;
101+
}
102+
103+
if (this.afterLoadScriptPath == null && scriptPath != null && !scriptPath.isEmpty()) {
104+
if (this.getClass().getResourceAsStream(scriptPath) == null) {
105+
throw new FileNotFoundException(
106+
"Couldn't find " + scriptPath + " as local file or resource.");
107+
}
108+
this.afterLoadScriptPath = scriptPath;
109+
}
94110
}
95111

96112
public String getAfterLoadScriptPath() {
@@ -241,8 +257,23 @@ public final void runScript(String scriptPath) throws SQLException, IOException
241257
try (Connection conn = this.makeConnection()) {
242258
DatabaseType dbType = this.workConf.getDatabaseType();
243259
ScriptRunner runner = new ScriptRunner(conn, true, true);
244-
LOG.debug("Executing script [{}] for database type [{}]", scriptPath, dbType);
245-
runner.runScript(scriptPath);
260+
LOG.debug(
261+
"Checking for script [{}] on local filesystem for database type [{}]",
262+
scriptPath,
263+
dbType);
264+
if (FileUtil.exists(scriptPath)) {
265+
LOG.debug(
266+
"Executing script [{}] from local filesystem for database type [{}]",
267+
scriptPath,
268+
dbType);
269+
runner.runExternalScript(scriptPath);
270+
} else {
271+
LOG.debug(
272+
"Executing script [{}] from resource stream for database type [{}]",
273+
scriptPath,
274+
dbType);
275+
runner.runScript(scriptPath);
276+
}
246277
}
247278
}
248279

@@ -274,11 +305,13 @@ public final Loader<? extends BenchmarkModule> loadDatabase()
274305

275306
if (this.afterLoadScriptPath != null) {
276307
LOG.debug(
277-
"Running script after load for {} benchmark...",
308+
"Running script {} after load for {} benchmark...",
309+
this.afterLoadScriptPath,
278310
this.workConf.getBenchmarkName().toUpperCase());
279311
runScript(this.afterLoadScriptPath);
280312
LOG.debug(
281-
"Finished running script after load for {} benchmark...",
313+
"Finished running script {} after load for {} benchmark...",
314+
this.afterLoadScriptPath,
282315
this.workConf.getBenchmarkName().toUpperCase());
283316
}
284317

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void runExternalScript(String path) throws IOException, SQLException {
5656

5757
public void runScript(String path) throws IOException, SQLException {
5858

59-
LOG.debug("trying to find file by path {}", path);
59+
LOG.debug("trying to find file by resource stream path {}", path);
6060

6161
try (InputStream in = this.getClass().getResourceAsStream(path);
6262
Reader reader = new InputStreamReader(in)) {

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

+29
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.oltpbenchmark.catalog.Table;
2222
import com.oltpbenchmark.util.Histogram;
2323
import com.oltpbenchmark.util.SQLUtil;
24+
import java.nio.file.Paths;
2425
import java.sql.PreparedStatement;
2526
import java.sql.ResultSet;
2627
import java.sql.SQLException;
@@ -73,6 +74,34 @@ public void testLoadWithAfterLoad() throws Exception {
7374
validateLoad();
7475
}
7576

77+
/** testLoad with external after load script */
78+
@Test
79+
public void testLoadWithExternalAfterLoad() throws Exception {
80+
String afterLoadScriptPath =
81+
Paths.get("src", "test", "java", "com", "oltpbenchmark", "api", "after-load-external.sql")
82+
.toAbsolutePath()
83+
.toString();
84+
85+
this.benchmark.setAfterLoadScriptPath(afterLoadScriptPath);
86+
87+
this.benchmark.loadDatabase();
88+
89+
// A table called extra is added with after-load, with one entry zero
90+
try (PreparedStatement stmt = conn.prepareStatement("SELECT * FROM extra_external");
91+
ResultSet rs = stmt.executeQuery()) {
92+
while (rs.next()) {
93+
assertEquals(
94+
"Table 'extra_external' from " + afterLoadScriptPath + " has value different than 1",
95+
rs.getInt(1),
96+
1);
97+
}
98+
} catch (Exception e) {
99+
fail("Table 'extra_external' from " + afterLoadScriptPath + " was not created");
100+
}
101+
102+
validateLoad();
103+
}
104+
76105
private void validateLoad() throws SQLException {
77106
assertFalse(
78107
"Failed to get table names for " + benchmark.getBenchmarkName().toUpperCase(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
DROP TABLE IF EXISTS extra_external CASCADE;
2+
3+
CREATE TABLE extra_external (
4+
extra_pk int NOT NULL,
5+
PRIMARY KEY (extra_pk)
6+
);
7+
8+
INSERT INTO extra_external VALUES (1);

0 commit comments

Comments
 (0)