From fadb84f2a9a953812b832bfcfdd2e26746580aa8 Mon Sep 17 00:00:00 2001 From: Christian Koop Date: Tue, 24 Oct 2023 01:51:02 +0200 Subject: [PATCH] Revert "fix: A lot of issues introduced after migrating to the new database API" This reverts commit 2eec84444770df53b68af1b09d272589c3079319. --- EpicAnchors-Plugin/pom.xml | 1 - .../epicanchors/AnchorManagerImpl.java | 8 +- .../com/craftaro/epicanchors/EpicAnchors.java | 15 ++- .../epicanchors/files/DataManager.java | 97 +++++++++++++++++++ ...horMigration.java => AnchorMigration.java} | 4 +- .../files/migration/_1_InitialMigration.java | 8 +- .../epicanchors/utils/DataHelper.java | 12 ++- 7 files changed, 130 insertions(+), 15 deletions(-) create mode 100644 EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/files/DataManager.java rename EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/files/migration/{_2_AnchorMigration.java => AnchorMigration.java} (98%) diff --git a/EpicAnchors-Plugin/pom.xml b/EpicAnchors-Plugin/pom.xml index e7c07dd..1d44dfc 100644 --- a/EpicAnchors-Plugin/pom.xml +++ b/EpicAnchors-Plugin/pom.xml @@ -56,7 +56,6 @@ false **/nms/v*/** - **/core/third_party/org/h2/** diff --git a/EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/AnchorManagerImpl.java b/EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/AnchorManagerImpl.java index 6f8b54e..3e22153 100644 --- a/EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/AnchorManagerImpl.java +++ b/EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/AnchorManagerImpl.java @@ -66,7 +66,7 @@ public class AnchorManagerImpl implements AnchorManager { protected void saveAll() { for (Set anchorSet : this.anchors.values()) { Collection asData = new ArrayList<>(anchorSet.size()); - this.dataManager.saveBatchSync(asData); + this.dataManager.saveBatch(asData); } } @@ -121,7 +121,7 @@ public class AnchorManagerImpl implements AnchorManager { if (tmpAnchors != null) { Collection asData = new ArrayList<>(tmpAnchors.size()); - this.dataManager.saveBatchSync(asData); + this.dataManager.saveBatch(asData); for (Anchor anchor : tmpAnchors) { ((AnchorImpl) anchor).deInit(this.plugin); @@ -236,7 +236,7 @@ public class AnchorManagerImpl implements AnchorManager { } Anchor anchor = new AnchorImpl(dataManager.getNextId("anchors"), owner, loc, ticks); - this.dataManager.saveSync(anchor); + this.dataManager.save(anchor); Bukkit.getScheduler().runTask(this.plugin, () -> { //TODO: Do we need to run this sync, or we are already on the main thread? Block block = loc.getBlock(); block.setType(Settings.MATERIAL.getMaterial().parseMaterial()); @@ -290,7 +290,7 @@ public class AnchorManagerImpl implements AnchorManager { anchor.getLocation().add(.5, .5, .5), 100, .5, .5, .5); ((AnchorImpl) anchor).deInit(this.plugin); - this.dataManager.deleteSync(anchor); + this.dataManager.delete(anchor); } /* Anchor access */ diff --git a/EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/EpicAnchors.java b/EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/EpicAnchors.java index c2c6bf7..265cf5c 100644 --- a/EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/EpicAnchors.java +++ b/EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/EpicAnchors.java @@ -4,6 +4,8 @@ import com.craftaro.core.SongodaCore; import com.craftaro.core.SongodaPlugin; import com.craftaro.core.commands.CommandManager; import com.craftaro.core.configuration.Config; +import com.craftaro.core.database.DatabaseConnector; +import com.craftaro.core.database.SQLiteConnector; import com.craftaro.core.gui.GuiManager; import com.craftaro.core.hooks.EconomyManager; import com.craftaro.core.hooks.HologramManager; @@ -14,8 +16,9 @@ import com.craftaro.epicanchors.commands.sub.GiveCommand; import com.craftaro.epicanchors.commands.sub.ReloadCommand; import com.craftaro.epicanchors.commands.sub.SettingsCommand; import com.craftaro.epicanchors.commands.sub.ShowCommand; +import com.craftaro.epicanchors.files.DataManager; import com.craftaro.epicanchors.files.Settings; -import com.craftaro.epicanchors.files.migration._2_AnchorMigration; +import com.craftaro.epicanchors.files.migration.AnchorMigration; import com.craftaro.epicanchors.files.migration._1_InitialMigration; import com.craftaro.epicanchors.listener.AnchorListener; import com.craftaro.epicanchors.listener.BlockListener; @@ -44,7 +47,15 @@ public final class EpicAnchors extends SongodaPlugin { public void onPluginEnable() { SongodaCore.registerPlugin(this, 31, XMaterial.END_PORTAL_FRAME); - initDatabase(Arrays.asList(new _1_InitialMigration(), new _2_AnchorMigration())); + // Initialize database +// this.getLogger().info("Initializing SQLite..."); +// DatabaseConnector dbCon = new SQLiteConnector(this); +// this.dataManager = new DataManager(dbCon, this); +// AnchorMigration anchorMigration = new AnchorMigration(dbCon, this.dataManager, new _1_InitialMigration()); +// anchorMigration.runMigrations(); +// anchorMigration.migrateLegacyData(this); + + initDatabase(Arrays.asList(new _1_InitialMigration(), new AnchorMigration())); this.anchorManager = new AnchorManagerImpl(this, this.dataManager); EpicAnchorsApi.initApi(this.anchorManager); diff --git a/EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/files/DataManager.java b/EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/files/DataManager.java new file mode 100644 index 0000000..8e196e3 --- /dev/null +++ b/EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/files/DataManager.java @@ -0,0 +1,97 @@ +package com.craftaro.epicanchors.files; + +import com.craftaro.core.database.DatabaseConnector; +import com.craftaro.epicanchors.AnchorImpl; +import com.craftaro.epicanchors.api.Anchor; +import com.craftaro.epicanchors.files.migration.AnchorMigration; +import com.craftaro.epicanchors.utils.Callback; +import com.craftaro.epicanchors.utils.UpdateCallback; +import com.craftaro.epicanchors.utils.Utils; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Objects; +import java.util.UUID; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +public class DataManager { + + + +// public void updateAnchors(Collection anchors, UpdateCallback callback) { +// this.databaseConnector.connect((con) -> { +// con.setAutoCommit(false); +// +// SQLException err = null; +// +// for (Anchor anchor : anchors) { +// try (PreparedStatement ps = con.prepareStatement("UPDATE " + this.anchorTable + +// " SET ticks_left =? WHERE id =?;")) { +// ps.setInt(1, anchor.getTicksLeft()); +// ps.setInt(2, anchor.getDbId()); +// +// ps.executeUpdate(); +// } catch (SQLException ex) { +// err = ex; +// break; +// } +// } +// +// if (err == null) { +// con.commit(); +// +// resolveUpdateCallback(callback, null); +// } else { +// con.rollback(); +// +// resolveUpdateCallback(callback, err); +// } +// +// con.setAutoCommit(true); +// }); +// } +// +// public void deleteAnchorAsync(Anchor anchor) { +// deleteAnchorAsync(anchor, null); +// } +// +// public void deleteAnchorAsync(Anchor anchor, UpdateCallback callback) { +// this.thread.execute(() -> +// this.databaseConnector.connect((con) -> { +// try (PreparedStatement ps = con.prepareStatement("DELETE FROM " + this.anchorTable + +// " WHERE id =?;")) { +// ps.setInt(1, anchor.getDbId()); +// +// ps.executeUpdate(); +// +// resolveUpdateCallback(callback, null); +// } catch (Exception ex) { +// resolveUpdateCallback(callback, ex); +// } +// }) +// ); +// } +// +// public static String getTableName(String prefix, String name) { +// String result = prefix + name; +// +// if (!result.matches("[a-z0-9_]+")) { +// throw new IllegalStateException("The generated table name '" + result + "' contains invalid characters"); +// } +// +// return result; +// } +} diff --git a/EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/files/migration/_2_AnchorMigration.java b/EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/files/migration/AnchorMigration.java similarity index 98% rename from EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/files/migration/_2_AnchorMigration.java rename to EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/files/migration/AnchorMigration.java index 3b262ae..4d50db1 100644 --- a/EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/files/migration/_2_AnchorMigration.java +++ b/EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/files/migration/AnchorMigration.java @@ -18,8 +18,8 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.logging.Level; -public class _2_AnchorMigration extends DataMigration { - public _2_AnchorMigration() { +public class AnchorMigration extends DataMigration { + public AnchorMigration() { super(2); } diff --git a/EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/files/migration/_1_InitialMigration.java b/EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/files/migration/_1_InitialMigration.java index 3baca8d..250e5fb 100644 --- a/EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/files/migration/_1_InitialMigration.java +++ b/EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/files/migration/_1_InitialMigration.java @@ -1,6 +1,7 @@ package com.craftaro.epicanchors.files.migration; import com.craftaro.core.database.DataMigration; +import com.craftaro.epicanchors.EpicAnchors; import java.sql.Connection; import java.sql.SQLException; @@ -14,14 +15,15 @@ public class _1_InitialMigration extends DataMigration { @Override public void migrate(Connection connection, String tablePrefix) throws SQLException { try (Statement statement = connection.createStatement()) { - statement.execute("CREATE TABLE " + tablePrefix + "anchors (" + - "id INTEGER NOT NULL PRIMARY KEY auto_increment," + + statement.execute("CREATE TABLE " + EpicAnchors.getPlugin(EpicAnchors.class).getDataManager().getTablePrefix() + "anchors (" + + "id INTEGER NOT NULL," + "world_name TEXT NOT NULL," + "x INTEGER NOT NULL," + "y INTEGER NOT NULL," + "z INTEGER NOT NULL," + "ticks_left INTEGER NOT NULL," + - "owner VARCHAR(36)" + + "owner VARCHAR(36)," + + "PRIMARY KEY(id AUTOINCREMENT)" + ");"); } } diff --git a/EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/utils/DataHelper.java b/EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/utils/DataHelper.java index f718e38..d95eb5b 100644 --- a/EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/utils/DataHelper.java +++ b/EpicAnchors-Plugin/src/main/java/com/craftaro/epicanchors/utils/DataHelper.java @@ -1,6 +1,8 @@ package com.craftaro.epicanchors.utils; +import com.craftaro.core.database.Data; import com.craftaro.core.database.DataManager; +import com.craftaro.core.third_party.org.jooq.Queries; import com.craftaro.core.third_party.org.jooq.Query; import com.craftaro.core.third_party.org.jooq.Record1; import com.craftaro.core.third_party.org.jooq.Result; @@ -8,13 +10,17 @@ import com.craftaro.core.third_party.org.jooq.impl.DSL; import com.craftaro.epicanchors.AnchorImpl; import com.craftaro.epicanchors.EpicAnchors; import com.craftaro.epicanchors.api.Anchor; -import com.craftaro.epicanchors.files.migration._2_AnchorMigration; +import com.craftaro.epicanchors.files.migration.AnchorMigration; import org.bukkit.World; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.List; public class DataHelper { @@ -62,7 +68,7 @@ public class DataHelper { } - public static void migrateAnchor(List<_2_AnchorMigration.LegacyAnchorEntry> anchorQueue, UpdateCallback callback) { + public static void migrateAnchor(List anchorQueue, UpdateCallback callback) { DataManager dataManager = EpicAnchors.getPlugin(EpicAnchors.class).getDataManager(); //recreate it with Jooq @@ -71,7 +77,7 @@ public class DataHelper { connection.setAutoCommit(false); try { List queries = new ArrayList<>(); - for (_2_AnchorMigration.LegacyAnchorEntry entry : anchorQueue) { + for (AnchorMigration.LegacyAnchorEntry entry : anchorQueue) { queries.add(dslContext.insertInto(DSL.table(dataManager.getTablePrefix() + "anchors")) .columns( DSL.field("world_name"),