From fb00713585f70af4f907e3509a5477a58f0cdd9b Mon Sep 17 00:00:00 2001 From: Bertil Chapuis Date: Wed, 3 Jan 2024 00:49:26 +0100 Subject: [PATCH] Refactor copy writer with constants (#817) --- baremaps-core/pom.xml.versionsBackup | 173 ------------------ .../baremaps/postgres/copy/CopyWriter.java | 122 ++++++++---- .../postgres/copy/GeometryValueHandler.java | 4 +- 3 files changed, 90 insertions(+), 209 deletions(-) delete mode 100644 baremaps-core/pom.xml.versionsBackup diff --git a/baremaps-core/pom.xml.versionsBackup b/baremaps-core/pom.xml.versionsBackup deleted file mode 100644 index e4b755272..000000000 --- a/baremaps-core/pom.xml.versionsBackup +++ /dev/null @@ -1,173 +0,0 @@ - - - - 4.0.0 - - org.apache.baremaps - baremaps - 0.7.3-SNAPSHOT - - - baremaps-core - - - - com.fasterxml.jackson.core - jackson-annotations - - - com.fasterxml.jackson.core - jackson-core - - - com.fasterxml.jackson.core - jackson-databind - - - com.fasterxml.jackson.dataformat - jackson-dataformat-csv - - - com.fasterxml.jackson.dataformat - jackson-dataformat-yaml - - - com.fasterxml.jackson.datatype - jackson-datatype-jdk8 - - - com.github.ben-manes.caffeine - caffeine - - - com.google.guava - guava - - - com.google.protobuf - protobuf-java - - - com.zaxxer - HikariCP - - - de.bytefish - pgbulkinsert - - - it.unimi.dsi - fastutil - - - mil.nga.geopackage - geopackage - - - net.ripe.ipresource - ipresource - - - org.apache.calcite - calcite-core - - - org.apache.commons - commons-compress - - - org.apache.lucene - lucene-core - - - org.apache.lucene - lucene-expressions - - - org.apache.lucene - lucene-queryparser - - - org.apache.lucene - lucene-spatial-extras - - - org.graalvm.js - js - - - org.graalvm.sdk - graal-sdk - - - org.locationtech.jts - jts-core - - - org.locationtech.proj4j - proj4j - - - - org.locationtech.proj4j - proj4j-epsg - - - org.postgresql - postgresql - - - org.wololo - flatgeobuf - 3.24.0 - - - org.xerial - sqlite-jdbc - - - - - - - org.xolstice.maven.plugins - protobuf-maven-plugin - ${version.plugin.protobuf-maven-plugin} - true - - - - compile - test-compile - - - com.google.protobuf:protoc:3.19.3:exe:${os.detected.classifier} - - - - - - - - kr.motd.maven - os-maven-plugin - ${version.plugin.os-maven-plugin} - - - - diff --git a/baremaps-core/src/main/java/org/apache/baremaps/postgres/copy/CopyWriter.java b/baremaps-core/src/main/java/org/apache/baremaps/postgres/copy/CopyWriter.java index 0eadbb282..bdbb2a996 100644 --- a/baremaps-core/src/main/java/org/apache/baremaps/postgres/copy/CopyWriter.java +++ b/baremaps-core/src/main/java/org/apache/baremaps/postgres/copy/CopyWriter.java @@ -25,10 +25,9 @@ import java.io.IOException; import java.net.Inet4Address; import java.net.Inet6Address; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; import java.time.LocalDate; import java.time.LocalDateTime; +import java.util.Collection; import java.util.List; import java.util.Map; import org.locationtech.jts.geom.Geometry; @@ -38,17 +37,74 @@ /** A helper for writing in a {@code PGCopyOutputStream}. */ public class CopyWriter implements AutoCloseable { - private static final Charset UTF8 = StandardCharsets.UTF_8; + public static final StringValueHandler STRING_HANDLER = + new StringValueHandler(); - private static final byte IPV4 = 2; - private static final byte IPV4_MASK = 32; - private static final byte IPV4_IS_CIDR = 0; + public static final CollectionValueHandler> STRING_COLLECTION_HANDLER = + new CollectionValueHandler<>(Oid.TEXT, new StringValueHandler()); - private static final byte IPV6 = 3; - private static final int IPV6_MASK = 128; - private static final byte IPV6_IS_CIDR = 0; + public static final BooleanValueHandler BOOLEAN_HANDLER = + new BooleanValueHandler(); - private static final byte JSONB_VERSION = 1; + public static final CollectionValueHandler> BOOLEAN_COLLECTION_HANDLER = + new CollectionValueHandler<>(Oid.BOOL, new BooleanValueHandler()); + + public static final ByteValueHandler BYTE_HANDLER = + new ByteValueHandler<>(); + + public static final ByteArrayValueHandler BYTE_ARRAY_HANDLER = + new ByteArrayValueHandler(); + + public static final ShortValueHandler SHORT_HANDLER = + new ShortValueHandler<>(); + + public static final CollectionValueHandler> SHORT_COLLECTION_HANDLER = + new CollectionValueHandler<>(Oid.INT2, new ShortValueHandler<>()); + + public static final IntegerValueHandler INTEGER_HANDLER = + new IntegerValueHandler<>(); + + public static final CollectionValueHandler> INTEGER_COLLECTION_HANDLER = + new CollectionValueHandler<>(Oid.INT4, new IntegerValueHandler<>()); + + public static final LongValueHandler LONG_HANDLER = + new LongValueHandler<>(); + + public static final CollectionValueHandler> LONG_COLLECTION_HANDLER = + new CollectionValueHandler<>(Oid.INT8, new LongValueHandler<>()); + + public static final FloatValueHandler FLOAT_HANDLER = + new FloatValueHandler<>(); + + public static final CollectionValueHandler> FLOAT_COLLECTION_HANDLER = + new CollectionValueHandler<>(Oid.FLOAT4, new FloatValueHandler<>()); + + public static final DoubleValueHandler DOUBLE_HANDLER = + new DoubleValueHandler<>(); + + public static final CollectionValueHandler> DOUBLE_COLLECTION_HANDLER = + new CollectionValueHandler<>(Oid.FLOAT8, new DoubleValueHandler<>()); + + public static final LocalDateValueHandler LOCAL_DATE_HANDLER = + new LocalDateValueHandler(); + + public static final LocalDateTimeValueHandler LOCAL_DATE_TIME_HANDLER = + new LocalDateTimeValueHandler(); + + public static final Inet4AddressValueHandler INET_4_ADDRESS_HANDLER = + new Inet4AddressValueHandler(); + + public static final Inet6AddressValueHandler INET_6_ADDRESS_HANDLER = + new Inet6AddressValueHandler(); + + public static final HstoreValueHandler HSTORE_HANDLER = + new HstoreValueHandler(); + + public static final JsonbValueHandler JSONB_HANDLER = + new JsonbValueHandler(); + + public static final GeometryValueHandler GEOMETRY_HANDLER = + new GeometryValueHandler(); private final DataOutputStream data; @@ -118,7 +174,7 @@ public void startRow(int columns) throws IOException { * @throws IOException */ public void write(String value) throws IOException { - new StringValueHandler().handle(data, value); + STRING_HANDLER.handle(data, value); } /** @@ -128,7 +184,7 @@ public void write(String value) throws IOException { * @throws IOException */ public void write(List value) throws IOException { - new CollectionValueHandler<>(Oid.TEXT, new StringValueHandler()).handle(data, value); + STRING_COLLECTION_HANDLER.handle(data, value); } /** @@ -138,7 +194,7 @@ public void write(List value) throws IOException { * @throws IOException */ public void writeBoolean(Boolean value) throws IOException { - new BooleanValueHandler().handle(data, value); + BOOLEAN_HANDLER.handle(data, value); } /** @@ -148,7 +204,7 @@ public void writeBoolean(Boolean value) throws IOException { * @throws IOException */ public void writeBooleanList(List value) throws IOException { - new CollectionValueHandler<>(Oid.BOOL, new BooleanValueHandler()).handle(data, value); + BOOLEAN_COLLECTION_HANDLER.handle(data, value); } /** @@ -158,7 +214,7 @@ public void writeBooleanList(List value) throws IOException { * @throws IOException */ public void writeByte(Byte value) throws IOException { - new ByteValueHandler<>().handle(data, value); + BYTE_HANDLER.handle(data, value); } /** @@ -168,7 +224,7 @@ public void writeByte(Byte value) throws IOException { * @throws IOException */ public void writeByteArray(byte[] value) throws IOException { - new ByteArrayValueHandler().handle(data, value); + BYTE_ARRAY_HANDLER.handle(data, value); } /** @@ -178,7 +234,7 @@ public void writeByteArray(byte[] value) throws IOException { * @throws IOException */ public void writeShort(Short value) throws IOException { - new ShortValueHandler<>().handle(data, value); + SHORT_HANDLER.handle(data, value); } /** @@ -188,7 +244,7 @@ public void writeShort(Short value) throws IOException { * @throws IOException */ public void writeShortList(List value) throws IOException { - new CollectionValueHandler<>(Oid.INT2, new ShortValueHandler()).handle(data, value); + SHORT_COLLECTION_HANDLER.handle(data, value); } /** @@ -198,7 +254,7 @@ public void writeShortList(List value) throws IOException { * @throws IOException */ public void writeInteger(Integer value) throws IOException { - new IntegerValueHandler<>().handle(data, value); + INTEGER_HANDLER.handle(data, value); } /** @@ -208,7 +264,7 @@ public void writeInteger(Integer value) throws IOException { * @throws IOException */ public void writeIntegerList(List value) throws IOException { - new CollectionValueHandler<>(Oid.INT4, new IntegerValueHandler()).handle(data, value); + INTEGER_COLLECTION_HANDLER.handle(data, value); } /** @@ -218,7 +274,7 @@ public void writeIntegerList(List value) throws IOException { * @throws IOException */ public void writeLong(Long value) throws IOException { - new LongValueHandler<>().handle(data, value); + LONG_HANDLER.handle(data, value); } /** @@ -228,7 +284,7 @@ public void writeLong(Long value) throws IOException { * @throws IOException */ public void writeLongList(List value) throws IOException { - new CollectionValueHandler<>(Oid.INT8, new LongValueHandler()).handle(data, value); + LONG_COLLECTION_HANDLER.handle(data, value); } /** @@ -238,7 +294,7 @@ public void writeLongList(List value) throws IOException { * @throws IOException */ public void writeFloat(Float value) throws IOException { - new FloatValueHandler<>().handle(data, value); + FLOAT_HANDLER.handle(data, value); } /** @@ -248,7 +304,7 @@ public void writeFloat(Float value) throws IOException { * @throws IOException */ public void writeFloatList(List value) throws IOException { - new CollectionValueHandler<>(Oid.FLOAT4, new FloatValueHandler()).handle(data, value); + FLOAT_COLLECTION_HANDLER.handle(data, value); } /** @@ -258,7 +314,7 @@ public void writeFloatList(List value) throws IOException { * @throws IOException */ public void writeDouble(Double value) throws IOException { - new DoubleValueHandler<>().handle(data, value); + DOUBLE_HANDLER.handle(data, value); } /** @@ -268,7 +324,7 @@ public void writeDouble(Double value) throws IOException { * @throws IOException */ public void writeDoubleArray(List value) throws IOException { - new CollectionValueHandler<>(Oid.FLOAT8, new DoubleValueHandler()).handle(data, value); + DOUBLE_COLLECTION_HANDLER.handle(data, value); } /** @@ -278,7 +334,7 @@ public void writeDoubleArray(List value) throws IOException { * @throws IOException */ public void writeLocalDate(LocalDate value) throws IOException { - new LocalDateValueHandler().handle(data, value); + LOCAL_DATE_HANDLER.handle(data, value); } /** @@ -288,7 +344,7 @@ public void writeLocalDate(LocalDate value) throws IOException { * @throws IOException */ public void writeLocalDateTime(LocalDateTime value) throws IOException { - new LocalDateTimeValueHandler().handle(data, value); + LOCAL_DATE_TIME_HANDLER.handle(data, value); } /** @@ -298,7 +354,7 @@ public void writeLocalDateTime(LocalDateTime value) throws IOException { * @throws IOException */ public void writeInet4Adress(Inet4Address value) throws IOException { - new Inet4AddressValueHandler().handle(data, value); + INET_4_ADDRESS_HANDLER.handle(data, value); } /** @@ -308,7 +364,7 @@ public void writeInet4Adress(Inet4Address value) throws IOException { * @throws IOException */ public void writeInet6Adress(Inet6Address value) throws IOException { - new Inet6AddressValueHandler().handle(data, value); + INET_6_ADDRESS_HANDLER.handle(data, value); } /** @@ -318,7 +374,7 @@ public void writeInet6Adress(Inet6Address value) throws IOException { * @throws IOException */ public void writeHstore(Map value) throws IOException { - new HstoreValueHandler().handle(data, value); + HSTORE_HANDLER.handle(data, value); } /** @@ -328,7 +384,7 @@ public void writeHstore(Map value) throws IOException { * @throws IOException */ public void writeJsonb(String value) throws IOException { - new JsonbValueHandler().handle(data, value); + JSONB_HANDLER.handle(data, value); } /** @@ -338,7 +394,7 @@ public void writeJsonb(String value) throws IOException { * @throws IOException */ public void writeGeometry(Geometry value) throws IOException { - new GeometryValueHandler().handle(data, value); + GEOMETRY_HANDLER.handle(data, value); } /** Close the writer. */ diff --git a/baremaps-core/src/main/java/org/apache/baremaps/postgres/copy/GeometryValueHandler.java b/baremaps-core/src/main/java/org/apache/baremaps/postgres/copy/GeometryValueHandler.java index 41704a290..d50fc3d38 100644 --- a/baremaps-core/src/main/java/org/apache/baremaps/postgres/copy/GeometryValueHandler.java +++ b/baremaps-core/src/main/java/org/apache/baremaps/postgres/copy/GeometryValueHandler.java @@ -27,11 +27,9 @@ public class GeometryValueHandler extends BaseValueHandler { - private final WKBWriter writer = new WKBWriter(2, wkbNDR, true); - @Override protected void internalHandle(DataOutputStream buffer, Geometry value) throws IOException { - byte[] wkb = writer.write(value); + byte[] wkb = new WKBWriter(2, wkbNDR, true).write(value); buffer.writeInt(wkb.length); buffer.write(wkb, 0, wkb.length); }