Skip to content

Commit 70fc17f

Browse files
authored
refactor: improve PBF file loop syntax and cleanup JVM args in import script (#42)
- Fixed syntax error in `import.sh` PBF file verification loop. - Removed unused JVM arguments for cleaner configuration. feat: optimize building data processing in ImportService - Added shard ID calculation and WKB serialization for building geometry. - Removed redundant geometry checks and streamlined shard buffer updates. - Synchronized shard list modifications for thread safety.
1 parent 075b6ea commit 70fc17f

2 files changed

Lines changed: 20 additions & 30 deletions

File tree

scripts/import.sh

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ if [ ${#PBF_FILES} -eq 0 ]; then
8181
fi
8282

8383
# Verify each PBF file exists
84-
for f in ${PBF_FILES[@}; do
84+
for f in ${PBF_FILES[@]}; do
8585
if [ ! -f "$f" ]; then
8686
echo "Error: PBF file '$f' does not exist"
8787
exit 1
@@ -128,10 +128,6 @@ JVM_ARGS="$JVM_ARGS -XX:+UseG1GC"
128128
JVM_ARGS="$JVM_ARGS -XX:MaxGCPauseMillis=200"
129129
JVM_ARGS="$JVM_ARGS -XX:+UnlockExperimentalVMOptions"
130130
JVM_ARGS="$JVM_ARGS -XX:+UseTransparentHugePages"
131-
JVM_ARGS="$JVM_ARGS -XX:+ExitOnOutOfMemoryError"
132-
JVM_ARGS="$JVM_ARGS -XX:MaxDirectMemorySize=4G"
133-
JVM_ARGS="$JVM_ARGS -XX:MaxMetaspaceSize=512M"
134-
JVM_ARGS="$JVM_ARGS -Xss512k"
135131
JVM_ARGS="$JVM_ARGS --add-exports=java.base/jdk.internal.ref=ALL-UNNAMED"
136132
JVM_ARGS="$JVM_ARGS --add-exports=java.base/sun.nio.ch=ALL-UNNAMED"
137133
JVM_ARGS="$JVM_ARGS --add-exports=jdk.unsupported/sun.misc=ALL-UNNAMED"

src/main/java/com/dedicatedcode/paikka/service/importer/ImportService.java

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,7 @@ private void processBuildingBoundariesFromIndex(RocksDB poiIndexDb, RocksDB node
916916
PoiIndexRec recInner = recsToProcess.get(i);
917917
try {
918918
org.locationtech.jts.geom.Geometry geometry = buildGeometryFromWay(wayIdInner, nodeCache, wayIndexDb, stats);
919+
919920
if (geometry == null) continue;
920921
if (!geometry.isValid()) {
921922
try {
@@ -925,7 +926,10 @@ private void processBuildingBoundariesFromIndex(RocksDB poiIndexDb, RocksDB node
925926
}
926927
}
927928
if (geometry == null || geometry.isEmpty() || !geometry.isValid()) continue;
928-
results.add(new BuildingData(wayIdInner, recInner.subtype, null, geometry));
929+
byte[] wkb = new WKBWriter().write(geometry);
930+
Coordinate center = geometry.getEnvelopeInternal().centre();
931+
long shardId = s2Helper.getShardId(center.getY(), center.getX());
932+
results.add(new BuildingData(wayIdInner, recInner.subtype, null, wkb, shardId));
929933
} catch (Exception e) {
930934
stats.recordError(ImportStatistics.Stage.PROCESSING_BUILDINGS, Kind.GEOMETRY, wayIdInner, "build-building-geometry-batch", e);
931935
}
@@ -947,9 +951,10 @@ private void processBuildingBoundariesFromIndex(RocksDB poiIndexDb, RocksDB node
947951
List<BuildingData> batchResults = f.get();
948952
Map<Long, List<BuildingData>> currentActiveBuffer = shardBufferRef.get();
949953
for (BuildingData r : batchResults) {
950-
Coordinate center = r.geometry().getEnvelopeInternal().centre();
951-
long shardId = s2Helper.getShardId(center.getY(), center.getX());
952-
currentActiveBuffer.computeIfAbsent(shardId, k -> new CopyOnWriteArrayList<>()).add(r);
954+
List<BuildingData> shardList = currentActiveBuffer.computeIfAbsent(r.shardId, k -> new ArrayList<>());
955+
synchronized (shardList) {
956+
shardList.add(r);
957+
}
953958
stats.incrementBuildingsProcessed();
954959
}
955960
} catch (Exception e) {
@@ -984,7 +989,10 @@ private void processBuildingBoundariesFromIndex(RocksDB poiIndexDb, RocksDB node
984989
}
985990
}
986991
if (geometry == null || geometry.isEmpty() || !geometry.isValid()) continue;
987-
results.add(new BuildingData(wayIdInner, recInner.subtype, null, geometry));
992+
byte[] wkb = new WKBWriter().write(geometry);
993+
Coordinate center = geometry.getEnvelopeInternal().centre();
994+
long shardId = s2Helper.getShardId(center.getY(), center.getX());
995+
results.add(new BuildingData(wayIdInner, recInner.subtype, null, wkb, shardId));
988996
} catch (Exception e) {
989997
stats.recordError(ImportStatistics.Stage.PROCESSING_BUILDINGS, Kind.GEOMETRY, wayIdInner, "build-building-geometry-batch", e);
990998
}
@@ -1005,9 +1013,10 @@ private void processBuildingBoundariesFromIndex(RocksDB poiIndexDb, RocksDB node
10051013
List<BuildingData> batchResults = f.get();
10061014
Map<Long, List<BuildingData>> currentActiveBuffer = shardBufferRef.get();
10071015
for (BuildingData r : batchResults) {
1008-
Coordinate center = r.geometry().getEnvelopeInternal().centre();
1009-
long shardId = s2Helper.getShardId(center.getY(), center.getX());
1010-
currentActiveBuffer.computeIfAbsent(shardId, k -> new CopyOnWriteArrayList<>()).add(r);
1016+
List<BuildingData> shardList = currentActiveBuffer.computeIfAbsent(r.shardId, k -> new ArrayList<>());
1017+
synchronized (shardList) {
1018+
shardList.add(r);
1019+
}
10111020
stats.incrementBuildingsProcessed();
10121021
}
10131022
} catch (Exception e) {
@@ -1053,21 +1062,6 @@ private boolean hasAddressTags(OsmEntity entity) {
10531062
return false;
10541063
}
10551064

1056-
private boolean isBuildingWay(OsmWay way) {
1057-
for (int i = 0; i < way.getNumberOfTags(); i++) {
1058-
OsmTag tag = way.getTag(i);
1059-
if ("building".equals(tag.getKey())) {
1060-
String val = tag.getValue();
1061-
return switch (val) {
1062-
case "yes", "commercial", "retail", "industrial",
1063-
"office", "apartments", "residential" -> true;
1064-
default -> false;
1065-
};
1066-
}
1067-
}
1068-
return false;
1069-
}
1070-
10711065
private void writeShardBatchAppendOnly(Map<Long, List<PoiData>> shardBuffer, RocksDB appendDb, ImportStatistics stats) throws Exception {
10721066
FlatBufferBuilder builder = new FlatBufferBuilder(1024 * 32);
10731067

@@ -1308,7 +1302,7 @@ private int serializeBuildingData(FlatBufferBuilder builder, BuildingData buildi
13081302

13091303
int geometryOff = 0;
13101304
if (building.geometry() != null) {
1311-
byte[] wkb = new WKBWriter().write(building.geometry());
1305+
byte[] wkb = building.geometry();
13121306
int geometryDataOff = Geometry.createDataVector(builder, wkb);
13131307
geometryOff = Geometry.createGeometry(builder, geometryDataOff);
13141308
}
@@ -2022,7 +2016,7 @@ private record AddressData(String street, String houseNumber, String postcode, S
20222016
private record BoundaryResultLite(long osmId, int level, String name, String code, org.locationtech.jts.geom.Geometry geometry) {
20232017
}
20242018

2025-
private record BuildingData(long id, String name, String code, org.locationtech.jts.geom.Geometry geometry) {
2019+
private record BuildingData(long id, String name, String code, byte[] geometry, long shardId) {
20262020
}
20272021

20282022
private Long safeEntityId(EntityContainer container) {

0 commit comments

Comments
 (0)