Feat : Create two new storageClass for different storage approche

This commit is contained in:
MrSolarius 2023-06-12 19:08:14 +02:00
parent 2a19574a68
commit 3df2bebe32
7 changed files with 118 additions and 28 deletions

View File

@ -27,7 +27,6 @@ package de.bluecolored.bluemap.common.config.storage;
import de.bluecolored.bluemap.api.debug.DebugDump;
import de.bluecolored.bluemap.core.storage.Compression;
import de.bluecolored.bluemap.core.storage.sql.SQLStorageSettings;
import de.bluecolored.bluemap.core.storage.sql.dialect.Dialect;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import java.net.MalformedURLException;
@ -47,8 +46,6 @@ public class SQLConfig extends StorageConfig implements SQLStorageSettings {
private Map<String, String> connectionProperties = new HashMap<>();
@DebugDump private Dialect dialect = Dialect.MariaDB;
@DebugDump private Compression compression = Compression.GZIP;
@DebugDump private transient URL driverJarURL = null;
@ -71,11 +68,6 @@ public class SQLConfig extends StorageConfig implements SQLStorageSettings {
return Optional.ofNullable(driverClass);
}
@Override
public Dialect getDialect() {
return dialect;
}
@Override
public String getConnectionUrl() {
return connectionUrl;

View File

@ -31,7 +31,7 @@ import de.bluecolored.bluemap.core.storage.sql.SQLStorage;
public enum StorageType {
FILE (FileConfig.class, FileStorage::new),
SQL (SQLConfig.class, SQLStorage::new);
SQL (SQLConfig.class, SQLStorage::create);
private final Class<? extends StorageConfig> configType;
private final StorageFactory<? extends StorageConfig> storageFactory;

View File

@ -0,0 +1,11 @@
package de.bluecolored.bluemap.core.storage.sql;
import de.bluecolored.bluemap.core.storage.sql.dialect.MariaDBFactory;
import java.net.MalformedURLException;
public class MariaDBStorage extends SQLStorage{
public MariaDBStorage(SQLStorageSettings config) throws MalformedURLException, SQLDriverException {
super(new MariaDBFactory(), config);
}
}

View File

@ -0,0 +1,63 @@
package de.bluecolored.bluemap.core.storage.sql;
import com.flowpowered.math.vector.Vector2i;
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.net.MalformedURLException;
import java.sql.PreparedStatement;
public class PostgreSQLStorage extends SQLStorage {
public PostgreSQLStorage(SQLStorageSettings config) throws MalformedURLException, SQLDriverException {
super(new PostgresFactory(), config);
}
@Override
public OutputStream writeMapTile(String mapId, int lod, Vector2i tile) throws IOException {
Compression compression = lod == 0 ? this.hiresCompression : Compression.NONE;
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
return new WrappedOutputStream(compression.compress(byteOut), () -> {
int mapFK = getMapFK(mapId);
int tileCompressionFK = getMapTileCompressionFK(compression);
recoveringConnection(connection -> {
byte[] byteData = byteOut.toByteArray();
ByteArrayInputStream inputStream = new ByteArrayInputStream(byteData);
PreparedStatement statement = connection.prepareStatement(this.dialect.writeMapTile());
statement.setInt(1, mapFK);
statement.setInt(2, lod);
statement.setInt(3, tile.getX());
statement.setInt(4, tile.getY());
statement.setInt(5, tileCompressionFK);
statement.setBinaryStream(6, inputStream);
statement.executeUpdate();
}, 2);
});
}
@Override
public OutputStream writeMeta(String mapId, String name) {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
return new WrappedOutputStream(byteOut, () -> {
int mapFK = getMapFK(mapId);
recoveringConnection(connection -> {
byte[] byteData = byteOut.toByteArray();
ByteArrayInputStream inputStream = new ByteArrayInputStream(byteData);
PreparedStatement statement = connection.prepareStatement(this.dialect.writeMeta());
statement.setInt(1, mapFK);
statement.setString(2, name);
statement.setBinaryStream(3, inputStream);
statement.executeUpdate();
}, 2);
});
}
}

View File

@ -30,6 +30,7 @@ import com.github.benmanes.caffeine.cache.LoadingCache;
import de.bluecolored.bluemap.core.BlueMap;
import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.storage.*;
import de.bluecolored.bluemap.core.storage.sql.dialect.Dialect;
import de.bluecolored.bluemap.core.storage.sql.dialect.SQLQueryFactory;
import de.bluecolored.bluemap.core.util.WrappedOutputStream;
import org.apache.commons.dbcp2.*;
@ -49,11 +50,11 @@ import java.util.*;
import java.util.concurrent.CompletionException;
import java.util.function.Function;
public class SQLStorage extends Storage {
public abstract class SQLStorage extends Storage {
private final SQLQueryFactory dialect;
protected final SQLQueryFactory dialect;
private final DataSource dataSource;
private final Compression hiresCompression;
protected final Compression hiresCompression;
private final LoadingCache<String, Integer> mapFKs = Caffeine.newBuilder()
.executor(BlueMap.THREAD_POOL)
@ -64,9 +65,9 @@ public class SQLStorage extends Storage {
private volatile boolean closed;
public SQLStorage(SQLStorageSettings config) throws MalformedURLException, SQLDriverException {
public SQLStorage(SQLQueryFactory dialect, SQLStorageSettings config) throws MalformedURLException, SQLDriverException {
this.dialect = dialect;
this.closed = false;
this.dialect = config.getDialect().getDialectFactory();
try {
if (config.getDriverClass().isPresent()) {
if (config.getDriverJar().isPresent()) {
@ -515,7 +516,7 @@ public class SQLStorage extends Storage {
}
@SuppressWarnings("UnusedReturnValue")
private int executeUpdate(Connection connection, @Language("sql") String sql, Object... parameters) throws SQLException {
protected int executeUpdate(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++) {
@ -525,7 +526,7 @@ public class SQLStorage extends Storage {
}
@SuppressWarnings("SameParameterValue")
private void recoveringConnection(ConnectionConsumer action, int tries) throws SQLException, IOException {
void recoveringConnection(ConnectionConsumer action, int tries) throws SQLException, IOException {
recoveringConnection((ConnectionFunction<Void>) action, tries);
}
@ -559,7 +560,7 @@ public class SQLStorage extends Storage {
throw sqlException;
}
private int getMapFK(String mapId) throws SQLException {
protected int getMapFK(String mapId) throws SQLException {
try {
return Objects.requireNonNull(mapFKs.get(mapId));
} catch (CompletionException ex) {
@ -572,7 +573,7 @@ public class SQLStorage extends Storage {
}
}
private int getMapTileCompressionFK(Compression compression) throws SQLException {
int getMapTileCompressionFK(Compression compression) throws SQLException {
try {
return Objects.requireNonNull(mapTileCompressionFKs.get(compression));
} catch (CompletionException ex) {
@ -674,6 +675,13 @@ public class SQLStorage extends Storage {
return new PoolingDataSource<>(connectionPool);
}
public static SQLStorage create(SQLStorageSettings settings) {
String dbUrl = settings.getConnectionUrl();
String provider = dbUrl.strip().split(":")[1];
System.out.println("Using SQL-Provider: " + provider);
return Dialect.getStorage(provider,settings);
}
@FunctionalInterface
public interface ConnectionConsumer extends ConnectionFunction<Void> {

View File

@ -1,7 +1,6 @@
package de.bluecolored.bluemap.core.storage.sql;
import de.bluecolored.bluemap.core.storage.Compression;
import de.bluecolored.bluemap.core.storage.sql.dialect.Dialect;
import java.net.MalformedURLException;
import java.net.URL;
@ -14,8 +13,6 @@ public interface SQLStorageSettings {
Optional<String> getDriverClass();
Dialect getDialect();
String getConnectionUrl();
Map<String, String> getConnectionProperties();

View File

@ -1,17 +1,36 @@
package de.bluecolored.bluemap.core.storage.sql.dialect;
import de.bluecolored.bluemap.core.storage.sql.MariaDBStorage;
import de.bluecolored.bluemap.core.storage.sql.PostgreSQLStorage;
import de.bluecolored.bluemap.core.storage.sql.SQLStorage;
import de.bluecolored.bluemap.core.storage.sql.SQLStorageSettings;
public enum Dialect {
MariaDB(new MariaDBFactory()),
PostgreSQL(new PostgresFactory());
MariaDB( MariaDBStorage.class, "mariadb" ),
PostgreSQL( PostgreSQLStorage.class,"postgresql");
private final SQLQueryFactory dialectFactory;
private final Class<? extends SQLStorage> storageClass;
private final String dialectName;
Dialect(SQLQueryFactory storageFactory) {
this.dialectFactory = storageFactory;
<C extends SQLStorage> Dialect(Class<C> storageClass, String dialectName) {
this.storageClass = storageClass;
this.dialectName = dialectName;
}
public String getDialectName() {
return dialectName;
}
public SQLQueryFactory getDialectFactory() {
return dialectFactory;
public static SQLStorage getStorage(String dialectName, SQLStorageSettings settings){
for (Dialect dialect : values()) {
if (dialect.getDialectName().equals(dialectName)) {
try {
return dialect.storageClass.getConstructor(SQLStorageSettings.class).newInstance(settings);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return null;
}
}