Skip to content

Commit 240ca6c

Browse files
committed
Fix some issues with the new sql-storage implementations
1 parent f18f7a9 commit 240ca6c

File tree

4 files changed

+54
-32
lines changed

4 files changed

+54
-32
lines changed

BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/config/storage/SQLConfig.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
@Getter
5555
public class SQLConfig extends StorageConfig {
5656

57-
private static final Pattern URL_DIALECT_PATTERN = Pattern.compile("jdbc:([^:]*)://.*");
57+
private static final Pattern URL_DIALECT_PATTERN = Pattern.compile("jdbc:([^:]*):.*");
5858

5959
private String connectionUrl = "jdbc:mysql://localhost/bluemap?permitMysqlScheme";
6060
private Map<String, String> connectionProperties = new HashMap<>();
@@ -102,7 +102,12 @@ public Dialect getDialect() throws ConfigurationException {
102102
// default from connection-url
103103
if (key == null) {
104104
Matcher matcher = URL_DIALECT_PATTERN.matcher(connectionUrl);
105-
if (!matcher.find()) return Dialect.MYSQL;
105+
if (!matcher.find()) {
106+
throw new ConfigurationException("""
107+
Failed to parse the provided connection-url!
108+
Please check your 'connection-url' setting in your configuration and make sure it is in the correct format.
109+
""".strip());
110+
}
106111
key = Key.bluemap(matcher.group(1)).getFormatted();
107112
}
108113

BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/SQLMapStorage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public void delete(DoublePredicate onProgress) throws IOException {
136136
deleted = sql.purgeMapTiles(mapId, 1000);
137137
totalDeleted += deleted;
138138

139-
if (onProgress.test((double) totalDeleted / tileCount))
139+
if (!onProgress.test((double) totalDeleted / tileCount))
140140
return;
141141

142142
} while (deleted > 0 && totalDeleted < tileCount);

BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/commandset/AbstractCommandSet.java

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import java.sql.*;
3737
import java.util.ArrayList;
3838
import java.util.List;
39-
import java.util.Objects;
4039

4140
@SuppressWarnings("SqlSourceToSinkFlow")
4241
@RequiredArgsConstructor
@@ -45,9 +44,9 @@ public abstract class AbstractCommandSet implements CommandSet {
4544
private final Database db;
4645

4746
final LoadingCache<String, Integer> mapKeys = Caffeine.newBuilder()
48-
.build(this::mapKey);
47+
.build(this::findOrCreateMapKey);
4948
final LoadingCache<Compression, Integer> compressionKeys = Caffeine.newBuilder()
50-
.build(this::compressionKey);
49+
.build(this::findOrCreateCompressionKey);
5150

5251
@Language("sql")
5352
public abstract String createMapTableStatement();
@@ -83,8 +82,8 @@ public int writeMapTile(
8382
String mapId, int lod, int x, int z, Compression compression,
8483
byte[] bytes
8584
) throws IOException {
86-
int mapKey = Objects.requireNonNull(mapKeys.get(mapId));
87-
int compressionKey = Objects.requireNonNull(compressionKeys.get(compression));
85+
int mapKey = mapKey(mapId);
86+
int compressionKey = compressionKey(compression);
8887
return db.run(connection -> {
8988
return executeUpdate(connection,
9089
writeMapTileStatement(),
@@ -201,7 +200,7 @@ public TilePosition[] listMapTiles(String mapId, int lod, Compression compressio
201200

202201
@Override
203202
public int writeMapMeta(String mapId, String itemName, byte[] bytes) throws IOException {
204-
int mapKey = Objects.requireNonNull(mapKeys.get(mapId));
203+
int mapKey = mapKey(mapId);
205204
return db.run(connection -> {
206205
return executeUpdate(connection,
207206
writeMapMetaStatement(),
@@ -265,24 +264,28 @@ public boolean hasMapMeta(String mapId, String itemName) throws IOException {
265264

266265
@Override
267266
public void purgeMap(String mapId) throws IOException {
268-
db.run(connection -> {
267+
synchronized (mapKeys) {
268+
db.run(connection -> {
269269

270-
executeUpdate(connection,
271-
purgeMapTileTableStatement(),
272-
mapId
273-
);
270+
executeUpdate(connection,
271+
purgeMapTileTableStatement(),
272+
mapId
273+
);
274274

275-
executeUpdate(connection,
276-
purgeMapMetaTableStatement(),
277-
mapId
278-
);
275+
executeUpdate(connection,
276+
purgeMapMetaTableStatement(),
277+
mapId
278+
);
279279

280-
executeUpdate(connection,
281-
deleteMapStatement(),
282-
mapId
283-
);
280+
executeUpdate(connection,
281+
deleteMapStatement(),
282+
mapId
283+
);
284284

285-
});
285+
});
286+
287+
mapKeys.invalidate(mapId);
288+
}
286289
}
287290

288291
@Language("sql")
@@ -324,7 +327,14 @@ public String[] listMapIds(int start, int count) throws IOException {
324327
@Language("sql")
325328
public abstract String createMapKeyStatement();
326329

327-
public int mapKey(String mapId) throws IOException {
330+
public int mapKey(String mapId) {
331+
synchronized (mapKeys) {
332+
//noinspection DataFlowIssue
333+
return mapKeys.get(mapId);
334+
}
335+
}
336+
337+
public int findOrCreateMapKey(String mapId) throws IOException {
328338
return db.run(connection -> {
329339
ResultSet result = executeQuery(connection,
330340
findMapKeyStatement(),
@@ -353,7 +363,14 @@ public int mapKey(String mapId) throws IOException {
353363
@Language("sql")
354364
public abstract String createCompressionKeyStatement();
355365

356-
public int compressionKey(Compression compression) throws IOException {
366+
public int compressionKey(Compression compression) {
367+
synchronized (compressionKeys) {
368+
//noinspection DataFlowIssue
369+
return compressionKeys.get(compression);
370+
}
371+
}
372+
373+
public int findOrCreateCompressionKey(Compression compression) throws IOException {
357374
return db.run(connection -> {
358375
ResultSet result = executeQuery(connection,
359376
findCompressionKeyStatement(),

BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/commandset/SqliteCommandSet.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public SqliteCommandSet(Database db) {
3838
public String createMapTableStatement() {
3939
return """
4040
CREATE TABLE IF NOT EXISTS `bluemap_map` (
41-
`id` INTEGER UNSIGNED PRIMARY KEY AUTOINCREMENT,
41+
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
4242
`map_id` TEXT UNIQUE NOT NULL
4343
) STRICT
4444
""";
@@ -49,7 +49,7 @@ public String createMapTableStatement() {
4949
public String createCompressionTableStatement() {
5050
return """
5151
CREATE TABLE IF NOT EXISTS `bluemap_map_tile_compression` (
52-
`id` INTEGER UNSIGNED PRIMARY KEY AUTOINCREMENT,
52+
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
5353
`compression` TEXT UNIQUE NOT NULL
5454
) STRICT
5555
""";
@@ -60,7 +60,7 @@ public String createCompressionTableStatement() {
6060
public String createMapMetaTableStatement() {
6161
return """
6262
CREATE TABLE IF NOT EXISTS `bluemap_map_meta` (
63-
`map` INTEGER UNSIGNED NOT NULL,
63+
`map` INTEGER NOT NULL,
6464
`key` TEXT NOT NULL,
6565
`value` BLOB NOT NULL,
6666
PRIMARY KEY (`map`, `key`),
@@ -78,11 +78,11 @@ FOREIGN KEY (`map`)
7878
public String createMapTileTableStatement() {
7979
return """
8080
CREATE TABLE IF NOT EXISTS `bluemap_map_tile` (
81-
`map` INTEGER UNSIGNED NOT NULL,
82-
`lod` INTEGER UNSIGNED NOT NULL,
81+
`map` INTEGER NOT NULL,
82+
`lod` INTEGER NOT NULL,
8383
`x` INTEGER NOT NULL,
8484
`z` INTEGER NOT NULL,
85-
`compression` INTEGER UNSIGNED NOT NULL,
85+
`compression` INTEGER NOT NULL,
8686
`data` BLOB NOT NULL,
8787
PRIMARY KEY (`map`, `lod`, `x`, `z`),
8888
CONSTRAINT `fk_bluemap_map_tile_map`
@@ -104,7 +104,7 @@ FOREIGN KEY (`compression`)
104104
public String fixLegacyCompressionIdsStatement() {
105105
return """
106106
UPDATE `bluemap_map_tile_compression`
107-
SET `compression` = CONCAT('bluemap:', `compression`)
107+
SET `compression` = 'bluemap:' || `compression`
108108
WHERE NOT `compression` LIKE '%:%'
109109
""";
110110
}

0 commit comments

Comments
 (0)