Skip to content

Commit 7af02ad

Browse files
mmerdesrtroilo
authored andcommitted
#470: refactor to smaller methods
1 parent 041f312 commit 7af02ad

File tree

1 file changed

+48
-23
lines changed

1 file changed

+48
-23
lines changed

oshdb-api/src/main/java/org/heigit/ohsome/oshdb/api/db/OSHDBJdbc.java

+48-23
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package org.heigit.ohsome.oshdb.api.db;
22

3+
import static java.util.stream.Collectors.toList;
4+
35
import com.google.common.base.Joiner;
46
import java.sql.Connection;
7+
import java.sql.ResultSet;
58
import java.sql.SQLException;
69
import java.util.Collection;
7-
import java.util.LinkedList;
8-
import java.util.List;
10+
import java.util.HashMap;
11+
import java.util.HashSet;
912
import java.util.Optional;
10-
import java.util.stream.Collectors;
13+
import java.util.Set;
1114
import java.util.stream.Stream;
1215
import javax.sql.DataSource;
1316
import org.heigit.ohsome.oshdb.api.mapreducer.MapReducer;
@@ -20,6 +23,7 @@
2023
import org.heigit.ohsome.oshdb.util.mappable.OSHDBMapReducible;
2124
import org.heigit.ohsome.oshdb.util.tagtranslator.JdbcTagTranslator;
2225
import org.heigit.ohsome.oshdb.util.tagtranslator.TagTranslator;
26+
import org.jetbrains.annotations.NotNull;
2327

2428
/**
2529
* OSHDB database backend connector to a JDBC database file.
@@ -56,31 +60,52 @@ public OSHDBJdbc prefix(String prefix) {
5660
@Override
5761
public <X extends OSHDBMapReducible> MapReducer<X> createMapReducer(Class<X> forClass) {
5862
try {
59-
Collection<String> expectedTables = Stream.of(OSMType.values()).map(TableNames::forOSMType)
60-
.filter(Optional::isPresent).map(Optional::get)
61-
.map(t -> t.toString(this.prefix()).toLowerCase()).collect(Collectors.toList());
62-
List<String> allTables = new LinkedList<>();
63-
try (var conn = getConnection()) {
64-
var metaData = conn.getMetaData();
65-
try (var rs = metaData.getTables(null, null, "%", new String[] {"TABLE"})) {
66-
while (rs.next()) {
67-
allTables.add(rs.getString("TABLE_NAME").toLowerCase());
68-
}
69-
if (!allTables.containsAll(expectedTables)) {
70-
throw new OSHDBTableNotFoundException(Joiner.on(", ").join(expectedTables));
71-
}
72-
}
73-
}
63+
Collection<String> expectedTables = getExpectedTables();
64+
checkTables(expectedTables);
7465
} catch (SQLException e) {
7566
throw new OSHDBException(e);
7667
}
77-
MapReducer<X> mapReducer;
68+
69+
return mapReducer(forClass);
70+
}
71+
72+
@NotNull
73+
private Collection<String> getExpectedTables() {
74+
return Stream.of(OSMType.values())
75+
.map(TableNames::forOSMType)
76+
.filter(Optional::isPresent)
77+
.map(Optional::get)
78+
.map(table -> table.toString(this.prefix()).toLowerCase())
79+
.collect(toList());
80+
}
81+
82+
private void checkTables(Collection<String> expectedTables) throws SQLException {
83+
try (var conn = getConnection()) {
84+
try (var rs = getTables(conn)) {
85+
var allTables = new HashSet<String>();
86+
while (rs.next()) {
87+
allTables.add(rs.getString("TABLE_NAME").toLowerCase());
88+
}
89+
if (!allTables.containsAll(expectedTables)) {
90+
throw new OSHDBTableNotFoundException(Joiner.on(", ").join(expectedTables));
91+
}
92+
}
93+
}
94+
}
95+
96+
private static ResultSet getTables(Connection connection) throws SQLException {
97+
return connection
98+
.getMetaData()
99+
.getTables(null, null, "%", new String[]{"TABLE"});
100+
}
101+
102+
@NotNull
103+
private <X extends OSHDBMapReducible> MapReducer<X> mapReducer(Class<X> forClass) {
78104
if (this.useMultithreading) {
79-
mapReducer = new MapReducerJdbcMultithread<>(this, forClass);
80-
} else {
81-
mapReducer = new MapReducerJdbcSinglethread<>(this, forClass);
105+
return new MapReducerJdbcMultithread<>(this, forClass);
82106
}
83-
return mapReducer;
107+
108+
return new MapReducerJdbcSinglethread<>(this, forClass);
84109
}
85110

86111
@Override

0 commit comments

Comments
 (0)