|
1 | 1 | package org.heigit.ohsome.oshdb.api.db;
|
2 | 2 |
|
| 3 | +import static java.util.stream.Collectors.toList; |
| 4 | + |
3 | 5 | import com.google.common.base.Joiner;
|
4 | 6 | import java.sql.Connection;
|
| 7 | +import java.sql.ResultSet; |
5 | 8 | import java.sql.SQLException;
|
6 | 9 | import java.util.Collection;
|
7 |
| -import java.util.LinkedList; |
8 |
| -import java.util.List; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.HashSet; |
9 | 12 | import java.util.Optional;
|
10 |
| -import java.util.stream.Collectors; |
| 13 | +import java.util.Set; |
11 | 14 | import java.util.stream.Stream;
|
12 | 15 | import javax.sql.DataSource;
|
13 | 16 | import org.heigit.ohsome.oshdb.api.mapreducer.MapReducer;
|
|
20 | 23 | import org.heigit.ohsome.oshdb.util.mappable.OSHDBMapReducible;
|
21 | 24 | import org.heigit.ohsome.oshdb.util.tagtranslator.JdbcTagTranslator;
|
22 | 25 | import org.heigit.ohsome.oshdb.util.tagtranslator.TagTranslator;
|
| 26 | +import org.jetbrains.annotations.NotNull; |
23 | 27 |
|
24 | 28 | /**
|
25 | 29 | * OSHDB database backend connector to a JDBC database file.
|
@@ -56,31 +60,52 @@ public OSHDBJdbc prefix(String prefix) {
|
56 | 60 | @Override
|
57 | 61 | public <X extends OSHDBMapReducible> MapReducer<X> createMapReducer(Class<X> forClass) {
|
58 | 62 | 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); |
74 | 65 | } catch (SQLException e) {
|
75 | 66 | throw new OSHDBException(e);
|
76 | 67 | }
|
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) { |
78 | 104 | if (this.useMultithreading) {
|
79 |
| - mapReducer = new MapReducerJdbcMultithread<>(this, forClass); |
80 |
| - } else { |
81 |
| - mapReducer = new MapReducerJdbcSinglethread<>(this, forClass); |
| 105 | + return new MapReducerJdbcMultithread<>(this, forClass); |
82 | 106 | }
|
83 |
| - return mapReducer; |
| 107 | + |
| 108 | + return new MapReducerJdbcSinglethread<>(this, forClass); |
84 | 109 | }
|
85 | 110 |
|
86 | 111 | @Override
|
|
0 commit comments