Skip to content

Commit 8d34430

Browse files
committed
reworked address join from main export query
1 parent e894ae7 commit 8d34430

File tree

5 files changed

+83
-56
lines changed

5 files changed

+83
-56
lines changed

citydb-database-postgres/src/main/resources/org/citydb/database/postgres/query_feature_hierarchy.sql

+2-17
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ WITH RECURSIVE FEATURE_HIERARCHY AS
5757
5858
INNER JOIN FEATURE_HIERARCHY H ON H.VAL_FEATURE_ID = P.FEATURE_ID AND H.VAL_RELATION_TYPE = 1
5959
WHERE NOT IS_CYCLE)
60-
SELECT DISTINCT ON (H.ID)
60+
SELECT
6161
H.ID,
6262
H.FEATURE_ID,
6363
H.PARENT_ID,
@@ -97,22 +97,7 @@ SELECT DISTINCT ON (H.ID)
9797
F.VALID_TO,
9898
{} AS GEOMETRY,
9999
G.GEOMETRY_PROPERTIES,
100-
G.FEATURE_ID AS GEOMETRY_FEATURE_ID,
101-
A.OBJECTID AS ADDRESS_OBJECTID,
102-
A.IDENTIFIER AS ADDRESS_IDENTIFIER,
103-
A.IDENTIFIER_CODESPACE AS ADDRESS_IDENTIFIER_CODESPACE,
104-
A.STREET,
105-
A.HOUSE_NUMBER,
106-
A.PO_BOX,
107-
A.ZIP_CODE,
108-
A.CITY,
109-
A.STATE,
110-
A.COUNTRY,
111-
A.FREE_TEXT,
112-
{} AS MULTI_POINT,
113-
A.CONTENT,
114-
A.CONTENT_MIME_TYPE
100+
G.FEATURE_ID AS GEOMETRY_FEATURE_ID
115101
FROM FEATURE_HIERARCHY H
116102
LEFT JOIN @[email protected] F ON F.ID = H.VAL_FEATURE_ID
117103
LEFT JOIN @[email protected]_DATA G ON G.ID = H.VAL_GEOMETRY_ID
118-
LEFT JOIN @[email protected] A ON A.ID = H.VAL_ADDRESS_ID

citydb-database/src/main/java/org/citydb/database/util/IndexHelper.java

-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public class IndexHelper {
5353
Index.PROPERTY_VAL_TIMESTAMP,
5454
Index.PROPERTY_VAL_DOUBLE,
5555
Index.PROPERTY_VAL_INT,
56-
Index.PROPERTY_VAL_LOD,
5756
Index.PROPERTY_VAL_STRING,
5857
Index.PROPERTY_VAL_UOM,
5958
Index.PROPERTY_VAL_URI));

citydb-operation/src/main/java/org/citydb/operation/exporter/address/AddressExporter.java

+60-30
Original file line numberDiff line numberDiff line change
@@ -33,54 +33,84 @@
3333

3434
import java.sql.ResultSet;
3535
import java.sql.SQLException;
36+
import java.sql.Statement;
37+
import java.util.Collections;
38+
import java.util.HashMap;
3639
import java.util.Map;
40+
import java.util.Set;
3741

3842
public class AddressExporter extends DatabaseExporter {
43+
private final Table address;
44+
private final Select select;
3945

4046
public AddressExporter(ExportHelper helper) throws SQLException {
4147
super(helper);
42-
stmt = helper.getConnection().prepareStatement(getQuery().toSql());
48+
address = tableHelper.getTable(org.citydb.database.schema.Table.ADDRESS);
49+
select = getBaseQuery();
50+
stmt = helper.getConnection().prepareStatement(Select.of(select)
51+
.where(address.column("id").eq(Placeholder.empty()))
52+
.toSql());
4353
}
4454

45-
private Select getQuery() {
46-
Table address = tableHelper.getTable(org.citydb.database.schema.Table.ADDRESS);
55+
private Select getBaseQuery() {
4756
return Select.newInstance()
48-
.select(address.columns(Map.of("objectid", "address_objectid", "identifier", "address_identifier",
49-
"identifier_codespace", "address_identifier_codespace")))
50-
.select(address.columns("street", "house_number", "po_box", "zip_code", "city",
51-
"state", "country", "free_text", "content", "content_mime_type"))
57+
.select(address.columns("id", "objectid", "identifier", "identifier_codespace", "street",
58+
"house_number", "po_box", "zip_code", "city", "state", "country", "free_text", "content",
59+
"content_mime_type"))
5260
.select(helper.getTransformOperator(address.column("multi_point")))
53-
.from(address)
54-
.where(address.column("id").eq(Placeholder.empty()));
61+
.from(address);
62+
}
63+
64+
private Select getQuery(Set<Long> ids) {
65+
return Select.of(select)
66+
.where(operationHelper.in(address.column("id"), ids));
5567
}
5668

5769
public Address doExport(long id) throws ExportException, SQLException {
5870
stmt.setLong(1, id);
5971
try (ResultSet rs = stmt.executeQuery()) {
60-
if (rs.next()) {
61-
return doExport(id, rs);
62-
}
72+
return doExport(rs).get(id);
6373
}
74+
}
6475

65-
return null;
76+
public Map<Long, Address> doExport(Set<Long> ids) throws ExportException, SQLException {
77+
if (ids.size() == 1) {
78+
stmt.setLong(1, ids.iterator().next());
79+
try (ResultSet rs = stmt.executeQuery()) {
80+
return doExport(rs);
81+
}
82+
} else if (!ids.isEmpty()) {
83+
try (Statement stmt = helper.getConnection().createStatement();
84+
ResultSet rs = stmt.executeQuery(getQuery(ids).toSql())) {
85+
return doExport(rs);
86+
}
87+
} else {
88+
return Collections.emptyMap();
89+
}
6690
}
6791

68-
public Address doExport(long addressId, ResultSet rs) throws ExportException, SQLException {
69-
return Address.newInstance()
70-
.setObjectId(rs.getString("address_objectid"))
71-
.setIdentifier(rs.getString("address_identifier"))
72-
.setIdentifierCodeSpace(rs.getString("address_identifier_codespace"))
73-
.setStreet(rs.getString("street"))
74-
.setHouseNumber(rs.getString("house_number"))
75-
.setPoBox(rs.getString("po_box"))
76-
.setZipCode(rs.getString("zip_code"))
77-
.setCity(rs.getString("city"))
78-
.setState(rs.getString("state"))
79-
.setCountry(rs.getString("country"))
80-
.setFreeText(getArrayValue(rs.getString("free_text")))
81-
.setMultiPoint(getGeometry(rs.getObject("multi_point"), MultiPoint.class))
82-
.setGenericContent(rs.getString("content"))
83-
.setGenericContentMimeType(rs.getString("content_mime_type"))
84-
.setDescriptor(AddressDescriptor.of(addressId));
92+
private Map<Long, Address> doExport(ResultSet rs) throws ExportException, SQLException {
93+
Map<Long, Address> addresses = new HashMap<>();
94+
while (rs.next()) {
95+
long id = rs.getLong("id");
96+
addresses.put(id, Address.newInstance()
97+
.setObjectId(rs.getString("objectid"))
98+
.setIdentifier(rs.getString("identifier"))
99+
.setIdentifierCodeSpace(rs.getString("identifier_codespace"))
100+
.setStreet(rs.getString("street"))
101+
.setHouseNumber(rs.getString("house_number"))
102+
.setPoBox(rs.getString("po_box"))
103+
.setZipCode(rs.getString("zip_code"))
104+
.setCity(rs.getString("city"))
105+
.setState(rs.getString("state"))
106+
.setCountry(rs.getString("country"))
107+
.setFreeText(getArrayValue(rs.getString("free_text")))
108+
.setMultiPoint(getGeometry(rs.getObject("multi_point"), MultiPoint.class))
109+
.setGenericContent(rs.getString("content"))
110+
.setGenericContentMimeType(rs.getString("content_mime_type"))
111+
.setDescriptor(AddressDescriptor.of(id)));
112+
}
113+
114+
return addresses;
85115
}
86116
}

citydb-operation/src/main/java/org/citydb/operation/exporter/geometry/ImplicitGeometryExporter.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ public ImplicitGeometry doExport(long id) throws ExportException, SQLException {
9595
}
9696

9797
public Map<Long, ImplicitGeometry> doExport(Set<Long> ids, Collection<Appearance> appearances) throws ExportException, SQLException {
98-
if (!ids.isEmpty()) {
98+
if (ids.size() == 1) {
99+
stmt.setLong(1, ids.iterator().next());
100+
try (ResultSet rs = stmt.executeQuery()) {
101+
return doExport(appearances, rs);
102+
}
103+
} else if (!ids.isEmpty()) {
99104
try (Statement stmt = helper.getConnection().createStatement();
100105
ResultSet rs = stmt.executeQuery(getQuery(ids).toSql())) {
101106
return doExport(appearances, rs);

citydb-operation/src/main/java/org/citydb/operation/exporter/hierarchy/HierarchyBuilder.java

+15-7
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public static HierarchyBuilder newInstance(long rootId, ExportHelper helper) {
6969

7070
public HierarchyBuilder initialize(ResultSet rs) throws ExportException, SQLException {
7171
Set<Long> appearanceIds = new HashSet<>();
72+
Set<Long> addressIds = new HashSet<>();
7273
Set<Long> implicitGeometryIds = new HashSet<>();
7374

7475
while (rs.next()) {
@@ -94,9 +95,8 @@ public HierarchyBuilder initialize(ResultSet rs) throws ExportException, SQLExce
9495
}
9596

9697
long addressId = rs.getLong("val_address_id");
97-
if (!rs.wasNull() && hierarchy.getAddress(addressId) == null) {
98-
hierarchy.addAddress(addressId, tableHelper.getOrCreateExporter(AddressExporter.class)
99-
.doExport(addressId, rs));
98+
if (!rs.wasNull()) {
99+
addressIds.add(addressId);
100100
}
101101

102102
long implicitGeometryId = rs.getLong("val_implicitgeom_id");
@@ -114,15 +114,23 @@ public HierarchyBuilder initialize(ResultSet rs) throws ExportException, SQLExce
114114
}
115115
}
116116

117-
if (exportAppearances) {
117+
if (exportAppearances && !appearanceIds.isEmpty()) {
118118
tableHelper.getOrCreateExporter(AppearanceExporter.class)
119119
.doExport(appearanceIds, implicitGeometryIds)
120120
.forEach(hierarchy::addAppearance);
121121
}
122122

123-
tableHelper.getOrCreateExporter(ImplicitGeometryExporter.class)
124-
.doExport(implicitGeometryIds, hierarchy.getAppearances().values())
125-
.forEach(hierarchy::addImplicitGeometry);
123+
if (!addressIds.isEmpty()) {
124+
tableHelper.getOrCreateExporter(AddressExporter.class)
125+
.doExport(addressIds)
126+
.forEach(hierarchy::addAddress);
127+
}
128+
129+
if (!implicitGeometryIds.isEmpty()) {
130+
tableHelper.getOrCreateExporter(ImplicitGeometryExporter.class)
131+
.doExport(implicitGeometryIds, hierarchy.getAppearances().values())
132+
.forEach(hierarchy::addImplicitGeometry);
133+
}
126134

127135
return this;
128136
}

0 commit comments

Comments
 (0)