From 6b4134d878cca5c229f568f36af7c89614544b89 Mon Sep 17 00:00:00 2001 From: MrSolarius <45536184+mrsolarius@users.noreply.github.com> Date: Mon, 12 Jun 2023 20:02:07 +0200 Subject: [PATCH] Feat : add read BYTEA support --- .../core/storage/sql/PostgreSQLStorage.java | 70 +++++++++++++++++-- .../bluemap/core/storage/sql/SQLStorage.java | 6 +- 2 files changed, 69 insertions(+), 7 deletions(-) diff --git a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/PostgreSQLStorage.java b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/PostgreSQLStorage.java index daa0c30f..f34d44bd 100644 --- a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/PostgreSQLStorage.java +++ b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/PostgreSQLStorage.java @@ -1,16 +1,17 @@ package de.bluecolored.bluemap.core.storage.sql; import com.flowpowered.math.vector.Vector2i; +import de.bluecolored.bluemap.core.storage.CompressedInputStream; import de.bluecolored.bluemap.core.storage.Compression; import de.bluecolored.bluemap.core.storage.sql.dialect.PostgresFactory; import de.bluecolored.bluemap.core.util.WrappedOutputStream; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.OutputStream; +import java.io.*; import java.net.MalformedURLException; import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Optional; public class PostgreSQLStorage extends SQLStorage { public PostgreSQLStorage(SQLStorageSettings config) throws MalformedURLException, SQLDriverException { @@ -60,4 +61,65 @@ public OutputStream writeMeta(String mapId, String name) { }, 2); }); } + + @Override + public Optional readMapTile(String mapId, int lod, Vector2i tile) throws IOException { + Compression compression = lod == 0 ? this.hiresCompression : Compression.NONE; + + try { + byte[] data = recoveringConnection(connection -> { + ResultSet result = executeQuery(connection, + this.dialect.readMapTile(), + mapId, + lod, + tile.getX(), + tile.getY(), + compression.getTypeId() + ); + + if (result.next()) { + return result.getBytes(1); + } else { + return null; + } + }, 2); + + if (data == null) { + return Optional.empty(); + } + + InputStream inputStream = new ByteArrayInputStream(data); + return Optional.of(new CompressedInputStream(inputStream, compression)); + } catch (SQLException ex) { + throw new IOException(ex); + } + } + + @Override + public Optional readMeta(String mapId, String name) throws IOException { + try { + byte[] data = recoveringConnection(connection -> { + ResultSet result = executeQuery(connection, + this.dialect.readMeta(), + mapId, + escapeMetaName(name) + ); + if (result.next()) { + return result.getBytes(1); + } else { + return null; + } + }, 2); + + if (data == null) { + return Optional.empty(); + } + + InputStream inputStream = new ByteArrayInputStream(data); + return Optional.of(inputStream); + } catch (SQLException ex) { + throw new IOException(ex); + } + } + } diff --git a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/SQLStorage.java b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/SQLStorage.java index 0c3f36cd..9a82c780 100644 --- a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/SQLStorage.java +++ b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/SQLStorage.java @@ -506,7 +506,7 @@ public void close() throws IOException { } } - private ResultSet executeQuery(Connection connection, @Language("sql") String sql, Object... parameters) throws SQLException { + protected ResultSet executeQuery(Connection connection, @Language("sql") String sql, Object... parameters) throws SQLException { // we only use this prepared statement once, but the DB-Driver caches those and reuses them PreparedStatement statement = connection.prepareStatement(sql); for (int i = 0; i < parameters.length; i++) { @@ -526,12 +526,12 @@ protected int executeUpdate(Connection connection, @Language("sql") String sql, } @SuppressWarnings("SameParameterValue") - void recoveringConnection(ConnectionConsumer action, int tries) throws SQLException, IOException { + protected void recoveringConnection(ConnectionConsumer action, int tries) throws SQLException, IOException { recoveringConnection((ConnectionFunction) action, tries); } @SuppressWarnings("SameParameterValue") - private R recoveringConnection(ConnectionFunction action, int tries) throws SQLException, IOException { + protected R recoveringConnection(ConnectionFunction action, int tries) throws SQLException, IOException { SQLException sqlException = null; try {