From 98780514c61ee2fdf5f9bce798aa2a4d26e7e32e Mon Sep 17 00:00:00 2001 From: "Lukas Rieger (Blue)" Date: Sat, 27 Aug 2022 15:19:32 +0200 Subject: [PATCH] Fix tile-saving errors after reloading bluemap with sql storage --- .../bluemap/core/map/lowres/LowresLayer.java | 6 ++++++ .../de/bluecolored/bluemap/core/storage/Storage.java | 10 ++++++++++ .../bluemap/core/storage/file/FileStorage.java | 5 +++++ .../bluemap/core/storage/sql/SQLStorage.java | 10 ++++++++++ 4 files changed, 31 insertions(+) diff --git a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/map/lowres/LowresLayer.java b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/map/lowres/LowresLayer.java index 059d10f2..ce5ee995 100644 --- a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/map/lowres/LowresLayer.java +++ b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/map/lowres/LowresLayer.java @@ -84,6 +84,12 @@ private LowresTile createTile(Vector2i tilePos) { private void saveTile(Vector2i tilePos, @Nullable LowresTile tile, RemovalCause removalCause) { if (tile == null) return; + // check if storage is closed + if (mapStorage.getStorage().isClosed()){ + Logger.global.logDebug("Tried to save tile " + tilePos + " (lod: " + lod + ") but storage is already closed."); + return; + } + // save the tile try (OutputStream out = mapStorage.write(lod, tilePos)) { tile.save(out); diff --git a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/Storage.java b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/Storage.java index de8fcb9d..6f0aec75 100644 --- a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/Storage.java +++ b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/Storage.java @@ -59,6 +59,8 @@ public TileStorage tileStorage(final String mapId, final int lod) { return new TileStorage(mapId, lod); } + public abstract boolean isClosed(); + public class MapStorage { private final String mapId; @@ -79,6 +81,10 @@ public void delete(int lod, Vector2i tile) throws IOException { deleteMapTile(mapId, lod, tile); } + public Storage getStorage() { + return Storage.this; + } + } public class TileStorage { @@ -103,6 +109,10 @@ public void delete(Vector2i tile) throws IOException { deleteMapTile(mapId, lod, tile); } + public Storage getStorage() { + return Storage.this; + } + } } diff --git a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/file/FileStorage.java b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/file/FileStorage.java index 479c75f3..7d395e69 100644 --- a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/file/FileStorage.java +++ b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/file/FileStorage.java @@ -57,6 +57,11 @@ public FileStorage(Path root, Compression compression) { @Override public void initialize() {} + @Override + public boolean isClosed() { + return false; + } + @Override public void close() throws IOException {} diff --git a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/SQLStorage.java b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/SQLStorage.java index 859c37ec..d3a29f13 100644 --- a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/SQLStorage.java +++ b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/storage/sql/SQLStorage.java @@ -61,7 +61,11 @@ public class SQLStorage extends Storage { .executor(BlueMap.THREAD_POOL) .build(this::loadMapTileCompressionFK); + private volatile boolean closed; + public SQLStorage(SQLStorageSettings config) throws MalformedURLException, SQLDriverException { + this.closed = false; + try { if (config.getDriverClass().isPresent()) { if (config.getDriverJar().isPresent()) { @@ -478,8 +482,14 @@ public void initialize() throws IOException { } } + @Override + public boolean isClosed() { + return closed; + } + @Override public void close() throws IOException { + this.closed = true; if (dataSource instanceof AutoCloseable) { try { ((AutoCloseable) dataSource).close();