Feat : add read BYTEA support

This commit is contained in:
MrSolarius 2023-06-12 20:02:07 +02:00
parent 3df2bebe32
commit 6b4134d878
2 changed files with 69 additions and 7 deletions

View File

@ -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 class PostgreSQLStorage extends SQLStorage {
}, 2);
});
}
@Override
public Optional<CompressedInputStream> 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<InputStream> 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);
}
}
}

View File

@ -506,7 +506,7 @@ public abstract class SQLStorage extends Storage {
}
}
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 @@ public abstract class SQLStorage extends Storage {
}
@SuppressWarnings("SameParameterValue")
void recoveringConnection(ConnectionConsumer action, int tries) throws SQLException, IOException {
protected void recoveringConnection(ConnectionConsumer action, int tries) throws SQLException, IOException {
recoveringConnection((ConnectionFunction<Void>) action, tries);
}
@SuppressWarnings("SameParameterValue")
private <R> R recoveringConnection(ConnectionFunction<R> action, int tries) throws SQLException, IOException {
protected <R> R recoveringConnection(ConnectionFunction<R> action, int tries) throws SQLException, IOException {
SQLException sqlException = null;
try {