diff --git a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/config/storage/SQLConfig.java b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/config/storage/SQLConfig.java index d135b1a1..9dd7e8a8 100644 --- a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/config/storage/SQLConfig.java +++ b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/config/storage/SQLConfig.java @@ -54,7 +54,7 @@ import java.util.regex.Pattern; @Getter public class SQLConfig extends StorageConfig { - private static final Pattern URL_DIALECT_PATTERN = Pattern.compile("jdbc:([^:]*)://.*"); + private static final Pattern URL_DIALECT_PATTERN = Pattern.compile("jdbc:([^:]*):.*"); private String connectionUrl = "jdbc:mysql://localhost/bluemap?permitMysqlScheme"; private Map connectionProperties = new HashMap<>(); @@ -102,7 +102,12 @@ public class SQLConfig extends StorageConfig { // default from connection-url if (key == null) { Matcher matcher = URL_DIALECT_PATTERN.matcher(connectionUrl); - if (!matcher.find()) return Dialect.MYSQL; + if (!matcher.find()) { + throw new ConfigurationException(""" + Failed to parse the provided connection-url! + Please check your 'connection-url' setting in your configuration and make sure it is in the correct format. + """.strip()); + } key = Key.bluemap(matcher.group(1)).getFormatted(); } diff --git a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/SQLMapStorage.java b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/SQLMapStorage.java index 7e781e61..d55abebf 100644 --- a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/SQLMapStorage.java +++ b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/SQLMapStorage.java @@ -136,7 +136,7 @@ public class SQLMapStorage implements MapStorage { deleted = sql.purgeMapTiles(mapId, 1000); totalDeleted += deleted; - if (onProgress.test((double) totalDeleted / tileCount)) + if (!onProgress.test((double) totalDeleted / tileCount)) return; } while (deleted > 0 && totalDeleted < tileCount); diff --git a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/commandset/AbstractCommandSet.java b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/commandset/AbstractCommandSet.java index 1a2b8bfe..9b3548c8 100644 --- a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/commandset/AbstractCommandSet.java +++ b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/commandset/AbstractCommandSet.java @@ -36,7 +36,6 @@ import java.io.IOException; import java.sql.*; import java.util.ArrayList; import java.util.List; -import java.util.Objects; @SuppressWarnings("SqlSourceToSinkFlow") @RequiredArgsConstructor @@ -45,9 +44,9 @@ public abstract class AbstractCommandSet implements CommandSet { private final Database db; final LoadingCache mapKeys = Caffeine.newBuilder() - .build(this::mapKey); + .build(this::findOrCreateMapKey); final LoadingCache compressionKeys = Caffeine.newBuilder() - .build(this::compressionKey); + .build(this::findOrCreateCompressionKey); @Language("sql") public abstract String createMapTableStatement(); @@ -83,8 +82,8 @@ public abstract class AbstractCommandSet implements CommandSet { String mapId, int lod, int x, int z, Compression compression, byte[] bytes ) throws IOException { - int mapKey = Objects.requireNonNull(mapKeys.get(mapId)); - int compressionKey = Objects.requireNonNull(compressionKeys.get(compression)); + int mapKey = mapKey(mapId); + int compressionKey = compressionKey(compression); return db.run(connection -> { return executeUpdate(connection, writeMapTileStatement(), @@ -201,7 +200,7 @@ public abstract class AbstractCommandSet implements CommandSet { @Override public int writeMapMeta(String mapId, String itemName, byte[] bytes) throws IOException { - int mapKey = Objects.requireNonNull(mapKeys.get(mapId)); + int mapKey = mapKey(mapId); return db.run(connection -> { return executeUpdate(connection, writeMapMetaStatement(), @@ -265,24 +264,28 @@ public abstract class AbstractCommandSet implements CommandSet { @Override public void purgeMap(String mapId) throws IOException { - db.run(connection -> { + synchronized (mapKeys) { + db.run(connection -> { - executeUpdate(connection, - purgeMapTileTableStatement(), - mapId - ); + executeUpdate(connection, + purgeMapTileTableStatement(), + mapId + ); - executeUpdate(connection, - purgeMapMetaTableStatement(), - mapId - ); + executeUpdate(connection, + purgeMapMetaTableStatement(), + mapId + ); - executeUpdate(connection, - deleteMapStatement(), - mapId - ); + executeUpdate(connection, + deleteMapStatement(), + mapId + ); - }); + }); + + mapKeys.invalidate(mapId); + } } @Language("sql") @@ -324,7 +327,14 @@ public abstract class AbstractCommandSet implements CommandSet { @Language("sql") public abstract String createMapKeyStatement(); - public int mapKey(String mapId) throws IOException { + public int mapKey(String mapId) { + synchronized (mapKeys) { + //noinspection DataFlowIssue + return mapKeys.get(mapId); + } + } + + public int findOrCreateMapKey(String mapId) throws IOException { return db.run(connection -> { ResultSet result = executeQuery(connection, findMapKeyStatement(), @@ -353,7 +363,14 @@ public abstract class AbstractCommandSet implements CommandSet { @Language("sql") public abstract String createCompressionKeyStatement(); - public int compressionKey(Compression compression) throws IOException { + public int compressionKey(Compression compression) { + synchronized (compressionKeys) { + //noinspection DataFlowIssue + return compressionKeys.get(compression); + } + } + + public int findOrCreateCompressionKey(Compression compression) throws IOException { return db.run(connection -> { ResultSet result = executeQuery(connection, findCompressionKeyStatement(), diff --git a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/commandset/SqliteCommandSet.java b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/commandset/SqliteCommandSet.java index 93e5a65d..2e2a795c 100644 --- a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/commandset/SqliteCommandSet.java +++ b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/commandset/SqliteCommandSet.java @@ -38,7 +38,7 @@ public class SqliteCommandSet extends AbstractCommandSet { public String createMapTableStatement() { return """ CREATE TABLE IF NOT EXISTS `bluemap_map` ( - `id` INTEGER UNSIGNED PRIMARY KEY AUTOINCREMENT, + `id` INTEGER PRIMARY KEY AUTOINCREMENT, `map_id` TEXT UNIQUE NOT NULL ) STRICT """; @@ -49,7 +49,7 @@ public class SqliteCommandSet extends AbstractCommandSet { public String createCompressionTableStatement() { return """ CREATE TABLE IF NOT EXISTS `bluemap_map_tile_compression` ( - `id` INTEGER UNSIGNED PRIMARY KEY AUTOINCREMENT, + `id` INTEGER PRIMARY KEY AUTOINCREMENT, `compression` TEXT UNIQUE NOT NULL ) STRICT """; @@ -60,7 +60,7 @@ public class SqliteCommandSet extends AbstractCommandSet { public String createMapMetaTableStatement() { return """ CREATE TABLE IF NOT EXISTS `bluemap_map_meta` ( - `map` INTEGER UNSIGNED NOT NULL, + `map` INTEGER NOT NULL, `key` TEXT NOT NULL, `value` BLOB NOT NULL, PRIMARY KEY (`map`, `key`), @@ -78,11 +78,11 @@ public class SqliteCommandSet extends AbstractCommandSet { public String createMapTileTableStatement() { return """ CREATE TABLE IF NOT EXISTS `bluemap_map_tile` ( - `map` INTEGER UNSIGNED NOT NULL, - `lod` INTEGER UNSIGNED NOT NULL, + `map` INTEGER NOT NULL, + `lod` INTEGER NOT NULL, `x` INTEGER NOT NULL, `z` INTEGER NOT NULL, - `compression` INTEGER UNSIGNED NOT NULL, + `compression` INTEGER NOT NULL, `data` BLOB NOT NULL, PRIMARY KEY (`map`, `lod`, `x`, `z`), CONSTRAINT `fk_bluemap_map_tile_map` @@ -104,7 +104,7 @@ public class SqliteCommandSet extends AbstractCommandSet { public String fixLegacyCompressionIdsStatement() { return """ UPDATE `bluemap_map_tile_compression` - SET `compression` = CONCAT('bluemap:', `compression`) + SET `compression` = 'bluemap:' || `compression` WHERE NOT `compression` LIKE '%:%' """; }