This is an automated email from the ASF dual-hosted git repository. bchapuis pushed a commit to branch copy-writer-constants in repository https://gitbox.apache.org/repos/asf/incubator-baremaps.git
commit 6059a55120ab6ee5a5b7200b57b5a5cfa006bbe6 Author: Bertil Chapuis <[email protected]> AuthorDate: Tue Jan 2 23:43:21 2024 +0100 Refactor copy writer with constants --- baremaps-core/pom.xml.versionsBackup | 173 --------------------- .../apache/baremaps/postgres/copy/CopyWriter.java | 122 +++++++++++---- .../postgres/copy/GeometryValueHandler.java | 4 +- 3 files changed, 90 insertions(+), 209 deletions(-) diff --git a/baremaps-core/pom.xml.versionsBackup b/baremaps-core/pom.xml.versionsBackup deleted file mode 100644 index e4b75527..00000000 --- a/baremaps-core/pom.xml.versionsBackup +++ /dev/null @@ -1,173 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- -Licensed to the Apache Software Foundation (ASF) under one or more -contributor license agreements. See the NOTICE file distributed with -this work for additional information regarding copyright ownership. -The ASF licenses this file to you under the Apache License, Version 2.0 -(the "License"); you may not use this file except in compliance with -the License. You may obtain a copy of the License at - -http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. ---> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> - <parent> - <groupId>org.apache.baremaps</groupId> - <artifactId>baremaps</artifactId> - <version>0.7.3-SNAPSHOT</version> - </parent> - - <artifactId>baremaps-core</artifactId> - - <dependencies> - <dependency> - <groupId>com.fasterxml.jackson.core</groupId> - <artifactId>jackson-annotations</artifactId> - </dependency> - <dependency> - <groupId>com.fasterxml.jackson.core</groupId> - <artifactId>jackson-core</artifactId> - </dependency> - <dependency> - <groupId>com.fasterxml.jackson.core</groupId> - <artifactId>jackson-databind</artifactId> - </dependency> - <dependency> - <groupId>com.fasterxml.jackson.dataformat</groupId> - <artifactId>jackson-dataformat-csv</artifactId> - </dependency> - <dependency> - <groupId>com.fasterxml.jackson.dataformat</groupId> - <artifactId>jackson-dataformat-yaml</artifactId> - </dependency> - <dependency> - <groupId>com.fasterxml.jackson.datatype</groupId> - <artifactId>jackson-datatype-jdk8</artifactId> - </dependency> - <dependency> - <groupId>com.github.ben-manes.caffeine</groupId> - <artifactId>caffeine</artifactId> - </dependency> - <dependency> - <groupId>com.google.guava</groupId> - <artifactId>guava</artifactId> - </dependency> - <dependency> - <groupId>com.google.protobuf</groupId> - <artifactId>protobuf-java</artifactId> - </dependency> - <dependency> - <groupId>com.zaxxer</groupId> - <artifactId>HikariCP</artifactId> - </dependency> - <dependency> - <groupId>de.bytefish</groupId> - <artifactId>pgbulkinsert</artifactId> - </dependency> - <dependency> - <groupId>it.unimi.dsi</groupId> - <artifactId>fastutil</artifactId> - </dependency> - <dependency> - <groupId>mil.nga.geopackage</groupId> - <artifactId>geopackage</artifactId> - </dependency> - <dependency> - <groupId>net.ripe.ipresource</groupId> - <artifactId>ipresource</artifactId> - </dependency> - <dependency> - <groupId>org.apache.calcite</groupId> - <artifactId>calcite-core</artifactId> - </dependency> - <dependency> - <groupId>org.apache.commons</groupId> - <artifactId>commons-compress</artifactId> - </dependency> - <dependency> - <groupId>org.apache.lucene</groupId> - <artifactId>lucene-core</artifactId> - </dependency> - <dependency> - <groupId>org.apache.lucene</groupId> - <artifactId>lucene-expressions</artifactId> - </dependency> - <dependency> - <groupId>org.apache.lucene</groupId> - <artifactId>lucene-queryparser</artifactId> - </dependency> - <dependency> - <groupId>org.apache.lucene</groupId> - <artifactId>lucene-spatial-extras</artifactId> - </dependency> - <dependency> - <groupId>org.graalvm.js</groupId> - <artifactId>js</artifactId> - </dependency> - <dependency> - <groupId>org.graalvm.sdk</groupId> - <artifactId>graal-sdk</artifactId> - </dependency> - <dependency> - <groupId>org.locationtech.jts</groupId> - <artifactId>jts-core</artifactId> - </dependency> - <dependency> - <groupId>org.locationtech.proj4j</groupId> - <artifactId>proj4j</artifactId> - </dependency> - <!-- TODO: Remove this dependency due to license incompatibility --> - <dependency> - <groupId>org.locationtech.proj4j</groupId> - <artifactId>proj4j-epsg</artifactId> - </dependency> - <dependency> - <groupId>org.postgresql</groupId> - <artifactId>postgresql</artifactId> - </dependency> - <dependency> - <groupId>org.wololo</groupId> - <artifactId>flatgeobuf</artifactId> - <version>3.24.0</version> - </dependency> - <dependency> - <groupId>org.xerial</groupId> - <artifactId>sqlite-jdbc</artifactId> - </dependency> - </dependencies> - - <build> - <plugins> - <plugin> - <groupId>org.xolstice.maven.plugins</groupId> - <artifactId>protobuf-maven-plugin</artifactId> - <version>${version.plugin.protobuf-maven-plugin}</version> - <extensions>true</extensions> - <executions> - <execution> - <goals> - <goal>compile</goal> - <goal>test-compile</goal> - </goals> - <configuration> - <protocArtifact>com.google.protobuf:protoc:3.19.3:exe:${os.detected.classifier}</protocArtifact> - </configuration> - </execution> - </executions> - </plugin> - </plugins> - <extensions> - <extension> - <groupId>kr.motd.maven</groupId> - <artifactId>os-maven-plugin</artifactId> - <version>${version.plugin.os-maven-plugin}</version> - </extension> - </extensions> - </build> -</project> 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 0eadbb28..bdbb2a99 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.DataOutputStream; 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 @@ import org.postgresql.core.Oid; /** 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<String>> 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<Boolean>> BOOLEAN_COLLECTION_HANDLER = + new CollectionValueHandler<>(Oid.BOOL, new BooleanValueHandler()); + + public static final ByteValueHandler<Number> BYTE_HANDLER = + new ByteValueHandler<>(); + + public static final ByteArrayValueHandler BYTE_ARRAY_HANDLER = + new ByteArrayValueHandler(); + + public static final ShortValueHandler<Number> SHORT_HANDLER = + new ShortValueHandler<>(); + + public static final CollectionValueHandler<Short, Collection<Short>> SHORT_COLLECTION_HANDLER = + new CollectionValueHandler<>(Oid.INT2, new ShortValueHandler<>()); + + public static final IntegerValueHandler<Number> INTEGER_HANDLER = + new IntegerValueHandler<>(); + + public static final CollectionValueHandler<Integer, Collection<Integer>> INTEGER_COLLECTION_HANDLER = + new CollectionValueHandler<>(Oid.INT4, new IntegerValueHandler<>()); + + public static final LongValueHandler<Number> LONG_HANDLER = + new LongValueHandler<>(); + + public static final CollectionValueHandler<Long, Collection<Long>> LONG_COLLECTION_HANDLER = + new CollectionValueHandler<>(Oid.INT8, new LongValueHandler<>()); + + public static final FloatValueHandler<Number> FLOAT_HANDLER = + new FloatValueHandler<>(); + + public static final CollectionValueHandler<Float, Collection<Float>> FLOAT_COLLECTION_HANDLER = + new CollectionValueHandler<>(Oid.FLOAT4, new FloatValueHandler<>()); + + public static final DoubleValueHandler<Number> DOUBLE_HANDLER = + new DoubleValueHandler<>(); + + public static final CollectionValueHandler<Double, Collection<Double>> 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 class CopyWriter implements AutoCloseable { * @throws IOException */ public void write(String value) throws IOException { - new StringValueHandler().handle(data, value); + STRING_HANDLER.handle(data, value); } /** @@ -128,7 +184,7 @@ public class CopyWriter implements AutoCloseable { * @throws IOException */ public void write(List<String> value) throws IOException { - new CollectionValueHandler<>(Oid.TEXT, new StringValueHandler()).handle(data, value); + STRING_COLLECTION_HANDLER.handle(data, value); } /** @@ -138,7 +194,7 @@ public class CopyWriter implements AutoCloseable { * @throws IOException */ public void writeBoolean(Boolean value) throws IOException { - new BooleanValueHandler().handle(data, value); + BOOLEAN_HANDLER.handle(data, value); } /** @@ -148,7 +204,7 @@ public class CopyWriter implements AutoCloseable { * @throws IOException */ public void writeBooleanList(List<Boolean> value) throws IOException { - new CollectionValueHandler<>(Oid.BOOL, new BooleanValueHandler()).handle(data, value); + BOOLEAN_COLLECTION_HANDLER.handle(data, value); } /** @@ -158,7 +214,7 @@ public class CopyWriter implements AutoCloseable { * @throws IOException */ public void writeByte(Byte value) throws IOException { - new ByteValueHandler<>().handle(data, value); + BYTE_HANDLER.handle(data, value); } /** @@ -168,7 +224,7 @@ public class CopyWriter implements AutoCloseable { * @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 class CopyWriter implements AutoCloseable { * @throws IOException */ public void writeShort(Short value) throws IOException { - new ShortValueHandler<>().handle(data, value); + SHORT_HANDLER.handle(data, value); } /** @@ -188,7 +244,7 @@ public class CopyWriter implements AutoCloseable { * @throws IOException */ public void writeShortList(List<Short> value) throws IOException { - new CollectionValueHandler<>(Oid.INT2, new ShortValueHandler<Short>()).handle(data, value); + SHORT_COLLECTION_HANDLER.handle(data, value); } /** @@ -198,7 +254,7 @@ public class CopyWriter implements AutoCloseable { * @throws IOException */ public void writeInteger(Integer value) throws IOException { - new IntegerValueHandler<>().handle(data, value); + INTEGER_HANDLER.handle(data, value); } /** @@ -208,7 +264,7 @@ public class CopyWriter implements AutoCloseable { * @throws IOException */ public void writeIntegerList(List<Integer> value) throws IOException { - new CollectionValueHandler<>(Oid.INT4, new IntegerValueHandler<Integer>()).handle(data, value); + INTEGER_COLLECTION_HANDLER.handle(data, value); } /** @@ -218,7 +274,7 @@ public class CopyWriter implements AutoCloseable { * @throws IOException */ public void writeLong(Long value) throws IOException { - new LongValueHandler<>().handle(data, value); + LONG_HANDLER.handle(data, value); } /** @@ -228,7 +284,7 @@ public class CopyWriter implements AutoCloseable { * @throws IOException */ public void writeLongList(List<Long> value) throws IOException { - new CollectionValueHandler<>(Oid.INT8, new LongValueHandler<Long>()).handle(data, value); + LONG_COLLECTION_HANDLER.handle(data, value); } /** @@ -238,7 +294,7 @@ public class CopyWriter implements AutoCloseable { * @throws IOException */ public void writeFloat(Float value) throws IOException { - new FloatValueHandler<>().handle(data, value); + FLOAT_HANDLER.handle(data, value); } /** @@ -248,7 +304,7 @@ public class CopyWriter implements AutoCloseable { * @throws IOException */ public void writeFloatList(List<Float> value) throws IOException { - new CollectionValueHandler<>(Oid.FLOAT4, new FloatValueHandler<Float>()).handle(data, value); + FLOAT_COLLECTION_HANDLER.handle(data, value); } /** @@ -258,7 +314,7 @@ public class CopyWriter implements AutoCloseable { * @throws IOException */ public void writeDouble(Double value) throws IOException { - new DoubleValueHandler<>().handle(data, value); + DOUBLE_HANDLER.handle(data, value); } /** @@ -268,7 +324,7 @@ public class CopyWriter implements AutoCloseable { * @throws IOException */ public void writeDoubleArray(List<Double> value) throws IOException { - new CollectionValueHandler<>(Oid.FLOAT8, new DoubleValueHandler<Double>()).handle(data, value); + DOUBLE_COLLECTION_HANDLER.handle(data, value); } /** @@ -278,7 +334,7 @@ public class CopyWriter implements AutoCloseable { * @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 class CopyWriter implements AutoCloseable { * @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 class CopyWriter implements AutoCloseable { * @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 class CopyWriter implements AutoCloseable { * @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 class CopyWriter implements AutoCloseable { * @throws IOException */ public void writeHstore(Map<String, String> value) throws IOException { - new HstoreValueHandler().handle(data, value); + HSTORE_HANDLER.handle(data, value); } /** @@ -328,7 +384,7 @@ public class CopyWriter implements AutoCloseable { * @throws IOException */ public void writeJsonb(String value) throws IOException { - new JsonbValueHandler().handle(data, value); + JSONB_HANDLER.handle(data, value); } /** @@ -338,7 +394,7 @@ public class CopyWriter implements AutoCloseable { * @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 41704a29..d50fc3d3 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 @@ import org.locationtech.jts.io.WKBWriter; public class GeometryValueHandler extends BaseValueHandler<Geometry> { - 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); }
