2626
2727import com .github .benmanes .caffeine .cache .Caffeine ;
2828import com .github .benmanes .caffeine .cache .LoadingCache ;
29+ import de .bluecolored .bluemap .core .logger .Logger ;
2930import de .bluecolored .bluemap .core .storage .compression .Compression ;
3031import de .bluecolored .bluemap .core .storage .sql .Database ;
3132import de .bluecolored .bluemap .core .util .Key ;
3536
3637import java .io .IOException ;
3738import java .sql .*;
38- import java .util .ArrayList ;
39- import java .util .List ;
39+ import java .util .*;
4040
4141@ SuppressWarnings ("SqlSourceToSinkFlow" )
4242@ RequiredArgsConstructor
@@ -53,6 +53,9 @@ public abstract class AbstractCommandSet implements CommandSet {
5353 protected final LoadingCache <Key , Integer > gridStorageKeys = Caffeine .newBuilder ()
5454 .build (this ::findOrCreateGridStorageKey );
5555
56+ @ Language ("sql" )
57+ public abstract String listExistingTablesStatement ();
58+
5659 @ Language ("sql" )
5760 public abstract String createMapTableStatement ();
5861
@@ -74,6 +77,27 @@ public abstract class AbstractCommandSet implements CommandSet {
7477 @ Override
7578 public void initializeTables () throws IOException {
7679 db .run (connection -> {
80+ try {
81+ Set <String > tables = new HashSet <>(6 );
82+ ResultSet result = executeQuery (connection , listExistingTablesStatement ());
83+ while (result .next ()) {
84+ tables .add (result .getString (1 ));
85+ }
86+
87+ if (tables .containsAll (Set .of (
88+ "bluemap_map" ,
89+ "bluemap_compression" ,
90+ "bluemap_item_storage" ,
91+ "bluemap_item_storage_data" ,
92+ "bluemap_grid_storage" ,
93+ "bluemap_grid_storage_data"
94+ ))) return ;
95+ } catch (SQLException ex ) {
96+ Logger .global .logWarning ("Failed to check for existing tables, will try to create them..." );
97+ Logger .global .logDebug (ex .toString ());
98+ }
99+
100+ // create tables (if not exists)
77101 executeUpdate (connection , createMapTableStatement ());
78102 executeUpdate (connection , createCompressionTableStatement ());
79103 executeUpdate (connection , createItemStorageTableStatement ());
@@ -343,7 +367,6 @@ public String[] listMapIds(int start, int count) throws IOException {
343367
344368 public int mapKey (String mapId ) {
345369 synchronized (mapKeys ) {
346- //noinspection DataFlowIssue
347370 return mapKeys .get (mapId );
348371 }
349372 }
@@ -379,7 +402,6 @@ public int findOrCreateMapKey(String mapId) throws IOException {
379402
380403 public int compressionKey (Compression compression ) {
381404 synchronized (compressionKeys ) {
382- //noinspection DataFlowIssue
383405 return compressionKeys .get (compression );
384406 }
385407 }
@@ -415,7 +437,6 @@ public int findOrCreateCompressionKey(Compression compression) throws IOExceptio
415437
416438 public int itemStorageKey (Key key ) {
417439 synchronized (itemStorageKeys ) {
418- //noinspection DataFlowIssue
419440 return itemStorageKeys .get (key );
420441 }
421442 }
@@ -451,7 +472,6 @@ public int findOrCreateItemStorageKey(Key key) throws IOException {
451472
452473 public int gridStorageKey (Key key ) {
453474 synchronized (gridStorageKeys ) {
454- //noinspection DataFlowIssue
455475 return gridStorageKeys .get (key );
456476 }
457477 }
0 commit comments