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 @@ 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<String, String> 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();
}

View File

@ -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);

View File

@ -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<String, Integer> mapKeys = Caffeine.newBuilder()
.build(this::mapKey);
.build(this::findOrCreateMapKey);
final LoadingCache<Compression, Integer> 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(),

View File

@ -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 '%:%'
""";
}