Commit e5e68d99 authored by Johannes Visintini's avatar Johannes Visintini
Browse files

Merge branch 'tidy-up' into 'master'

general tidy up

See merge request !15
parents f7d22aee bdafe829
Pipeline #2828 created
package org.heigit.ohsome.oshdb.helpers;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.heigit.ohsome.oshdb.api.db.OSHDBDatabase;
import org.heigit.ohsome.oshdb.api.db.OSHDBH2;
......@@ -13,9 +11,9 @@ import org.heigit.ohsome.oshdb.api.db.OSHDBJdbc;
import org.heigit.ohsome.oshdb.api.mapreducer.MapReducer;
import org.heigit.ohsome.oshdb.api.mapreducer.OSMContributionView;
import org.heigit.ohsome.oshdb.api.mapreducer.OSMEntitySnapshotView;
import org.heigit.ohsome.oshdb.util.exceptions.OSHDBKeytablesNotFoundException;
import org.heigit.ohsome.oshdb.util.mappable.OSMContribution;
import org.heigit.ohsome.oshdb.util.mappable.OSMEntitySnapshot;
import org.heigit.ohsome.oshdb.util.exceptions.OSHDBKeytablesNotFoundException;
import org.heigit.ohsome.oshdb.util.tagtranslator.TagTranslator;
/**
......@@ -33,6 +31,10 @@ import org.heigit.ohsome.oshdb.util.tagtranslator.TagTranslator;
*/
public class OSHDBDriver {
private OSHDBDriver() {
throw new IllegalStateException("Driver class");
}
/** open connection to oshdb instance.
*
* <p>props example:</p>
......@@ -47,22 +49,22 @@ public class OSHDBDriver {
*/
public static void connect(Properties props, Consumer<OSHDBConnection> connect)
throws Exception {
String oshdb = getInterpolated(props, "oshdb");
var oshdb = getInterpolated(props, "oshdb");
if (oshdb == null) {
throw new IllegalArgumentException("props need to have to specifiy oshdb!");
}
if (oshdb.toLowerCase().startsWith("ignite:")) {
String cfg = oshdb.substring(7);
String prefix = getInterpolated(props, "prefix", "");
String keyTablesUrl = getInterpolated(props, "keytables");
var cfg = oshdb.substring(7);
var prefix = getInterpolated(props, "prefix", "");
var keyTablesUrl = getInterpolated(props, "keytables");
if (keyTablesUrl == null) {
throw new IllegalArgumentException("ignite specified but missing keytables");
}
connectToIgnite(cfg, prefix, keyTablesUrl, connect);
} else if (oshdb.toLowerCase().startsWith("h2:")) {
String h2 = oshdb.substring(3);
String prefix = getInterpolated(props, "prefix", "");
boolean multithreading = "true"
var h2 = oshdb.substring(3);
var prefix = getInterpolated(props, "prefix", "");
var multithreading = "true"
.equalsIgnoreCase(getInterpolated(props, "multithreading"));
connectToH2(h2, prefix, multithreading, connect);
} else {
......@@ -75,24 +77,28 @@ public class OSHDBDriver {
connectToH2(url, prefix, true, connect);
}
// OSHDBJdbc throws "Exception"
@SuppressWarnings("java:S112")
public static void connectToH2(String h2, String prefix,
boolean multithreading, Consumer<OSHDBConnection> connect) throws Exception {
try (final OSHDBH2 oshdb = new OSHDBH2(h2);
final OSHDBJdbc keyTables = new OSHDBJdbc(oshdb.getConnection())) {
try (final var oshdb = new OSHDBH2(h2);
final var keyTables = new OSHDBJdbc(oshdb.getConnection())) {
oshdb.prefix(prefix);
oshdb.multithreading(multithreading);
final OSHDBConnection connection = new OSHDBConnection(oshdb, keyTables);
final var connection = new OSHDBConnection(oshdb, keyTables);
connect.accept(connection);
}
}
// OSHDBJdbc throws "Exception"
@SuppressWarnings("java:S112")
public static void connectToIgnite(String cfg, String prefix, String keytablesUrl,
Consumer<OSHDBConnection> connect) throws Exception {
try (final Connection ktConnection = DriverManager.getConnection(keytablesUrl);
final OSHDBJdbc keytables = new OSHDBJdbc(ktConnection);
final OSHDBIgnite oshdb = new OSHDBIgnite(cfg)) {
try (final var ktConnection = DriverManager.getConnection(keytablesUrl);
final var keytables = new OSHDBJdbc(ktConnection);
final var oshdb = new OSHDBIgnite(cfg)) {
oshdb.prefix(prefix);
final OSHDBConnection connection = new OSHDBConnection(oshdb, keytables);
final var connection = new OSHDBConnection(oshdb, keytables);
connect.accept(connection);
}
}
......@@ -143,10 +149,10 @@ public class OSHDBDriver {
if (value == null) {
return defaultValue;
}
Matcher matcher = substitute.matcher(value);
StringBuffer sb = new StringBuffer();
var matcher = substitute.matcher(value);
var sb = new StringBuffer();
while (matcher.find()) {
String sub = matcher.group(1);
var sub = matcher.group(1);
matcher.appendReplacement(sb, props.getProperty(sub, "\\${" + sub + "}"));
}
matcher.appendTail(sb);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment