Fix some issues with the new sql-storage implementations

This commit is contained in:
Lukas Rieger (Blue) 2024-04-07 12:55:41 +02:00
parent f18f7a9a16
commit 240ca6c00e
No known key found for this signature in database
GPG Key ID: AA33883B1BBA03E6
4 changed files with 54 additions and 32 deletions

View File

@ -54,7 +54,7 @@
@Getter @Getter
public class SQLConfig extends StorageConfig { 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 String connectionUrl = "jdbc:mysql://localhost/bluemap?permitMysqlScheme";
private Map<String, String> connectionProperties = new HashMap<>(); private Map<String, String> connectionProperties = new HashMap<>();
@ -102,7 +102,12 @@ public Dialect getDialect() throws ConfigurationException {
// default from connection-url // default from connection-url
if (key == null) { if (key == null) {
Matcher matcher = URL_DIALECT_PATTERN.matcher(connectionUrl); 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(); key = Key.bluemap(matcher.group(1)).getFormatted();
} }

View File

@ -136,7 +136,7 @@ public void delete(DoublePredicate onProgress) throws IOException {
deleted = sql.purgeMapTiles(mapId, 1000); deleted = sql.purgeMapTiles(mapId, 1000);
totalDeleted += deleted; totalDeleted += deleted;
if (onProgress.test((double) totalDeleted / tileCount)) if (!onProgress.test((double) totalDeleted / tileCount))
return; return;
} while (deleted > 0 && totalDeleted < tileCount); } while (deleted > 0 && totalDeleted < tileCount);

View File

@ -36,7 +36,6 @@
import java.sql.*; import java.sql.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
@SuppressWarnings("SqlSourceToSinkFlow") @SuppressWarnings("SqlSourceToSinkFlow")
@RequiredArgsConstructor @RequiredArgsConstructor
@ -45,9 +44,9 @@ public abstract class AbstractCommandSet implements CommandSet {
private final Database db; private final Database db;
final LoadingCache<String, Integer> mapKeys = Caffeine.newBuilder() final LoadingCache<String, Integer> mapKeys = Caffeine.newBuilder()
.build(this::mapKey); .build(this::findOrCreateMapKey);
final LoadingCache<Compression, Integer> compressionKeys = Caffeine.newBuilder() final LoadingCache<Compression, Integer> compressionKeys = Caffeine.newBuilder()
.build(this::compressionKey); .build(this::findOrCreateCompressionKey);
@Language("sql") @Language("sql")
public abstract String createMapTableStatement(); public abstract String createMapTableStatement();
@ -83,8 +82,8 @@ public int writeMapTile(
String mapId, int lod, int x, int z, Compression compression, String mapId, int lod, int x, int z, Compression compression,
byte[] bytes byte[] bytes
) throws IOException { ) throws IOException {
int mapKey = Objects.requireNonNull(mapKeys.get(mapId)); int mapKey = mapKey(mapId);
int compressionKey = Objects.requireNonNull(compressionKeys.get(compression)); int compressionKey = compressionKey(compression);
return db.run(connection -> { return db.run(connection -> {
return executeUpdate(connection, return executeUpdate(connection,
writeMapTileStatement(), writeMapTileStatement(),
@ -201,7 +200,7 @@ public TilePosition[] listMapTiles(String mapId, int lod, Compression compressio
@Override @Override
public int writeMapMeta(String mapId, String itemName, byte[] bytes) throws IOException { 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 db.run(connection -> {
return executeUpdate(connection, return executeUpdate(connection,
writeMapMetaStatement(), writeMapMetaStatement(),
@ -265,6 +264,7 @@ public boolean hasMapMeta(String mapId, String itemName) throws IOException {
@Override @Override
public void purgeMap(String mapId) throws IOException { public void purgeMap(String mapId) throws IOException {
synchronized (mapKeys) {
db.run(connection -> { db.run(connection -> {
executeUpdate(connection, executeUpdate(connection,
@ -283,6 +283,9 @@ public void purgeMap(String mapId) throws IOException {
); );
}); });
mapKeys.invalidate(mapId);
}
} }
@Language("sql") @Language("sql")
@ -324,7 +327,14 @@ public String[] listMapIds(int start, int count) throws IOException {
@Language("sql") @Language("sql")
public abstract String createMapKeyStatement(); 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 -> { return db.run(connection -> {
ResultSet result = executeQuery(connection, ResultSet result = executeQuery(connection,
findMapKeyStatement(), findMapKeyStatement(),
@ -353,7 +363,14 @@ public int mapKey(String mapId) throws IOException {
@Language("sql") @Language("sql")
public abstract String createCompressionKeyStatement(); 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 -> { return db.run(connection -> {
ResultSet result = executeQuery(connection, ResultSet result = executeQuery(connection,
findCompressionKeyStatement(), findCompressionKeyStatement(),

View File

@ -38,7 +38,7 @@ public SqliteCommandSet(Database db) {
public String createMapTableStatement() { public String createMapTableStatement() {
return """ return """
CREATE TABLE IF NOT EXISTS `bluemap_map` ( CREATE TABLE IF NOT EXISTS `bluemap_map` (
`id` INTEGER UNSIGNED PRIMARY KEY AUTOINCREMENT, `id` INTEGER PRIMARY KEY AUTOINCREMENT,
`map_id` TEXT UNIQUE NOT NULL `map_id` TEXT UNIQUE NOT NULL
) STRICT ) STRICT
"""; """;
@ -49,7 +49,7 @@ public String createMapTableStatement() {
public String createCompressionTableStatement() { public String createCompressionTableStatement() {
return """ return """
CREATE TABLE IF NOT EXISTS `bluemap_map_tile_compression` ( 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 `compression` TEXT UNIQUE NOT NULL
) STRICT ) STRICT
"""; """;
@ -60,7 +60,7 @@ public String createCompressionTableStatement() {
public String createMapMetaTableStatement() { public String createMapMetaTableStatement() {
return """ return """
CREATE TABLE IF NOT EXISTS `bluemap_map_meta` ( CREATE TABLE IF NOT EXISTS `bluemap_map_meta` (
`map` INTEGER UNSIGNED NOT NULL, `map` INTEGER NOT NULL,
`key` TEXT NOT NULL, `key` TEXT NOT NULL,
`value` BLOB NOT NULL, `value` BLOB NOT NULL,
PRIMARY KEY (`map`, `key`), PRIMARY KEY (`map`, `key`),
@ -78,11 +78,11 @@ FOREIGN KEY (`map`)
public String createMapTileTableStatement() { public String createMapTileTableStatement() {
return """ return """
CREATE TABLE IF NOT EXISTS `bluemap_map_tile` ( CREATE TABLE IF NOT EXISTS `bluemap_map_tile` (
`map` INTEGER UNSIGNED NOT NULL, `map` INTEGER NOT NULL,
`lod` INTEGER UNSIGNED NOT NULL, `lod` INTEGER NOT NULL,
`x` INTEGER NOT NULL, `x` INTEGER NOT NULL,
`z` INTEGER NOT NULL, `z` INTEGER NOT NULL,
`compression` INTEGER UNSIGNED NOT NULL, `compression` INTEGER NOT NULL,
`data` BLOB NOT NULL, `data` BLOB NOT NULL,
PRIMARY KEY (`map`, `lod`, `x`, `z`), PRIMARY KEY (`map`, `lod`, `x`, `z`),
CONSTRAINT `fk_bluemap_map_tile_map` CONSTRAINT `fk_bluemap_map_tile_map`
@ -104,7 +104,7 @@ FOREIGN KEY (`compression`)
public String fixLegacyCompressionIdsStatement() { public String fixLegacyCompressionIdsStatement() {
return """ return """
UPDATE `bluemap_map_tile_compression` UPDATE `bluemap_map_tile_compression`
SET `compression` = CONCAT('bluemap:', `compression`) SET `compression` = 'bluemap:' || `compression`
WHERE NOT `compression` LIKE '%:%' WHERE NOT `compression` LIKE '%:%'
"""; """;
} }