Commit a7b7dc20 authored by Rafael Troilo's avatar Rafael Troilo
Browse files

format for our checkstyle

parent 0811e12d
package org.heigit.ohsome.oshdb.helpers;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Map;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.heigit.bigspatialdata.oshdb.api.db.OSHDBDatabase;
import org.heigit.bigspatialdata.oshdb.api.db.OSHDBH2;
import org.heigit.bigspatialdata.oshdb.api.db.OSHDBIgnite;
......@@ -12,17 +18,6 @@ import org.heigit.bigspatialdata.oshdb.api.object.OSMEntitySnapshot;
import org.heigit.bigspatialdata.oshdb.util.exceptions.OSHDBKeytablesNotFoundException;
import org.heigit.bigspatialdata.oshdb.util.tagtranslator.TagTranslator;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* A basic OSHDBDriver class for connecting to h2 or ignite oshdb instances.
*
......@@ -31,119 +26,126 @@ import java.util.regex.Pattern;
* oshdb.getSnapshotView()
* .areaOfInterest((Geometry & Polygonal) areaOfInterest)
* .timestamps(tstamps)
* .osmTag(key)
* ...
* .osmTag(key)
* ...
* });
* }</pre>
*/
public class OSHDBDriver {
/*
props example:
oshdb="ignite:Path_To_Config" \ "h2:Path_To_Database"
prefix="global"
keytable="jdbc:postgresql://IP_OR_URL/keytables\${prefix}?user=ohsome&password=secret"
#multithreading default false, only used by h2 connections
multithreading=true
*/
public static void connect(Map<String,String> props,Consumer<OSHDBConnection> connect) throws Exception {
String 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 keyTableUrl = getInterpolated(props,"keytable");
if(keyTableUrl == null){
throw new IllegalArgumentException("ignite specified but missing keytable");
}
connectToIgnite(cfg,prefix,keyTableUrl, connect);
}else if(oshdb.toLowerCase().startsWith("h2:")) {
String h2 = oshdb.substring(3);
String prefix = getInterpolated(props,"prefix","");
boolean multithreading = "true"
.equalsIgnoreCase(getInterpolated(props,"multithreading"));
connectToH2(h2,prefix,multithreading,connect);
} else{
throw new IllegalArgumentException("unknown oshdb value! "+oshdb);
}
/** open connection to oshdb instance.
props example:
oshdb="ignite:Path_To_Config" \ "h2:Path_To_Database"
prefix="global"
keytable="jdbc:postgresql://IP_OR_URL/keytables\${prefix}?user=ohsome&password=secret"
#multithreading default false, only used by h2 connections
multithreading=true
*/
public static void connect(Map<String, String> props, Consumer<OSHDBConnection> connect)
throws Exception {
String oshdb = getInterpolated(props, "oshdb");
if (oshdb == null) {
throw new IllegalArgumentException("props need to have to specifiy oshdb!");
}
public static void connectToH2(String url, String prefix, Consumer<OSHDBConnection> connect) throws Exception {
connectToH2(url,prefix,true,connect);
if (oshdb.toLowerCase().startsWith("ignite:")) {
String cfg = oshdb.substring(7);
String prefix = getInterpolated(props, "prefix", "");
String keyTableUrl = getInterpolated(props, "keytable");
if (keyTableUrl == null) {
throw new IllegalArgumentException("ignite specified but missing keytable");
}
connectToIgnite(cfg, prefix, keyTableUrl, connect);
} else if (oshdb.toLowerCase().startsWith("h2:")) {
String h2 = oshdb.substring(3);
String prefix = getInterpolated(props, "prefix", "");
boolean multithreading = "true"
.equalsIgnoreCase(getInterpolated(props, "multithreading"));
connectToH2(h2, prefix, multithreading, connect);
} else {
throw new IllegalArgumentException("unknown oshdb value! " + oshdb);
}
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())) {
oshdb.prefix(prefix);
oshdb.multithreading(multithreading);
final OSHDBConnection connection = new OSHDBConnection(oshdb, keyTables);
connect.accept(connection);
}
}
public static void connectToH2(String url, String prefix, Consumer<OSHDBConnection> connect)
throws Exception {
connectToH2(url, prefix, true, connect);
}
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())) {
oshdb.prefix(prefix);
oshdb.multithreading(multithreading);
final OSHDBConnection connection = new OSHDBConnection(oshdb, keyTables);
connect.accept(connection);
}
public static void connectToIgnite(String cfg, String prefix, String keytableUrl, Consumer<OSHDBConnection> connect) throws Exception {
try (final Connection ktConnection = DriverManager.getConnection(keytableUrl);
final OSHDBJdbc keytables = new OSHDBJdbc(ktConnection);
final OSHDBIgnite oshdb = new OSHDBIgnite(cfg)) {
oshdb.prefix(prefix);
final OSHDBConnection connection = new OSHDBConnection(oshdb, keytables);
connect.accept(connection);
}
}
public static void connectToIgnite(String cfg, String prefix, String keytableUrl,
Consumer<OSHDBConnection> connect) throws Exception {
try (final Connection ktConnection = DriverManager.getConnection(keytableUrl);
final OSHDBJdbc keytables = new OSHDBJdbc(ktConnection);
final OSHDBIgnite oshdb = new OSHDBIgnite(cfg)) {
oshdb.prefix(prefix);
final OSHDBConnection connection = new OSHDBConnection(oshdb, keytables);
connect.accept(connection);
}
}
public static class OSHDBConnection {
public static class OSHDBConnection {
private final OSHDBDatabase oshdb;
private final OSHDBJdbc keytables;
private final TagTranslator tagTranslator;
private final OSHDBDatabase oshdb;
private final OSHDBJdbc keytables;
private final TagTranslator tagTranslator;
public OSHDBConnection(OSHDBDatabase oshdb, OSHDBJdbc keytables) throws OSHDBKeytablesNotFoundException {
this.oshdb = oshdb;
this.keytables = keytables;
this.tagTranslator = new TagTranslator(keytables.getConnection());
}
public OSHDBConnection(OSHDBDatabase oshdb, OSHDBJdbc keytables)
throws OSHDBKeytablesNotFoundException {
this.oshdb = oshdb;
this.keytables = keytables;
this.tagTranslator = new TagTranslator(keytables.getConnection());
}
public MapReducer<OSMContribution> getContributionView() {
return OSMContributionView.on(oshdb).keytables(keytables);
}
public MapReducer<OSMContribution> getContributionView() {
return OSMContributionView.on(oshdb).keytables(keytables);
}
public MapReducer<OSMEntitySnapshot> getSnapshotView() {
return OSMEntitySnapshotView.on(oshdb).keytables(keytables);
}
public MapReducer<OSMEntitySnapshot> getSnapshotView() {
return OSMEntitySnapshotView.on(oshdb).keytables(keytables);
}
public OSHDBDatabase getOSHDB() {
return oshdb;
}
public OSHDBDatabase getOSHDB() {
return oshdb;
}
public OSHDBJdbc getKeytables() {
return keytables;
}
public OSHDBJdbc getKeytables() {
return keytables;
}
public TagTranslator getTagTranslator() {
return tagTranslator;
}
public TagTranslator getTagTranslator() {
return tagTranslator;
}
}
private static final Pattern substitute = Pattern.compile("\\$\\{(\\w+)\\}");
private static String getInterpolated(Map<String, String> props, String key) {
return getInterpolated(props, key, null);
}
private static final Pattern substitute = Pattern.compile("\\$\\{(\\w+)\\}");
private static String getInterpolated(Map<String,String> props, String key){
return getInterpolated(props,key,null);
private static String getInterpolated(Map<String, String> props, String key,
String defaultValue) {
String value = props.get(key);
if (value == null) {
return defaultValue;
}
private static String getInterpolated(Map<String,String> props, String key, String defaultValue){
String value = props.get(key);
if(value == null){
return defaultValue;
}
Matcher matcher = substitute.matcher(value);
StringBuffer sb = new StringBuffer();
while(matcher.find()){
String sub = matcher.group(1);
matcher.appendReplacement(sb, props.getOrDefault(sub, "\\${" + sub + "}"));
}
matcher.appendTail(sb);
return sb.toString();
Matcher matcher = substitute.matcher(value);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
String sub = matcher.group(1);
matcher.appendReplacement(sb, props.getOrDefault(sub, "\\${" + sub + "}"));
}
matcher.appendTail(sb);
return sb.toString();
}
}
Supports Markdown
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