Revert "Updates to new database system"

This reverts commit f3c8778ad2.
This commit is contained in:
Christian Koop 2023-10-24 01:52:18 +02:00
parent b4c5282990
commit f3377b08f9
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3
9 changed files with 289 additions and 253 deletions

View File

@ -1,6 +1,5 @@
package com.craftaro.epicanchors.api; package com.craftaro.epicanchors.api;
import com.craftaro.core.database.Data;
import org.bukkit.Chunk; import org.bukkit.Chunk;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
@ -8,7 +7,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.UUID; import java.util.UUID;
public interface Anchor extends Data { public interface Anchor {
int getDbId(); int getDbId();
UUID getOwner(); UUID getOwner();

View File

@ -1,7 +1,5 @@
package com.craftaro.epicanchors; package com.craftaro.epicanchors;
import com.craftaro.core.database.Data;
import com.craftaro.core.database.SerializedLocation;
import com.craftaro.epicanchors.api.Anchor; import com.craftaro.epicanchors.api.Anchor;
import com.craftaro.epicanchors.utils.WorldUtils; import com.craftaro.epicanchors.utils.WorldUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -12,8 +10,6 @@ import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.UUID; import java.util.UUID;
@ -25,15 +21,6 @@ public class AnchorImpl implements Anchor {
private final Location location; private final Location location;
private int ticksLeft; private int ticksLeft;
/**
* Default constructor for deserialization.
*/
public AnchorImpl() {
dbId = 0;
owner = null;
location = null;
}
public AnchorImpl(int dbId, @Nullable UUID owner, @NotNull Location location, int ticksLeft) { public AnchorImpl(int dbId, @Nullable UUID owner, @NotNull Location location, int ticksLeft) {
if (dbId <= 0) throw new IllegalArgumentException("Invalid value for dbId"); if (dbId <= 0) throw new IllegalArgumentException("Invalid value for dbId");
if (ticksLeft <= 0 && ticksLeft != -1) throw new IllegalArgumentException("Invalid value for ticksLeft"); if (ticksLeft <= 0 && ticksLeft != -1) throw new IllegalArgumentException("Invalid value for ticksLeft");
@ -150,29 +137,4 @@ public class AnchorImpl implements Anchor {
public boolean isInfinite() { public boolean isInfinite() {
return this.ticksLeft == -1; return this.ticksLeft == -1;
} }
@Override
public Map<String, Object> serialize() {
Map<String, Object> map = new LinkedHashMap<>();
map.put("id", dbId);
map.putAll(SerializedLocation.of(location));
map.put("ticks_left", ticksLeft);
map.put("owner", owner == null ? null : owner.toString());
return map;
}
@Override
public Data deserialize(Map<String, Object> map) {
return new AnchorImpl(
(int) map.get("id"),
map.get("owner") == null ? null : UUID.fromString((String) map.get("owner")),
SerializedLocation.of(map),
(int) map.get("ticks_left")
);
}
@Override
public String getTableName() {
return "anchors";
}
} }

View File

@ -3,8 +3,6 @@ package com.craftaro.epicanchors;
import com.craftaro.core.SongodaPlugin; import com.craftaro.core.SongodaPlugin;
import com.craftaro.core.compatibility.CompatibleMaterial; import com.craftaro.core.compatibility.CompatibleMaterial;
import com.craftaro.core.compatibility.CompatibleParticleHandler; import com.craftaro.core.compatibility.CompatibleParticleHandler;
import com.craftaro.core.database.Data;
import com.craftaro.core.database.DataManager;
import com.craftaro.core.hooks.HologramManager; import com.craftaro.core.hooks.HologramManager;
import com.craftaro.core.third_party.com.cryptomorin.xseries.XMaterial; import com.craftaro.core.third_party.com.cryptomorin.xseries.XMaterial;
import com.craftaro.core.third_party.com.cryptomorin.xseries.XSound; import com.craftaro.core.third_party.com.cryptomorin.xseries.XSound;
@ -14,9 +12,9 @@ import com.craftaro.core.utils.TimeUtils;
import com.craftaro.epicanchors.api.Anchor; import com.craftaro.epicanchors.api.Anchor;
import com.craftaro.epicanchors.api.AnchorAccessCheck; import com.craftaro.epicanchors.api.AnchorAccessCheck;
import com.craftaro.epicanchors.api.AnchorManager; import com.craftaro.epicanchors.api.AnchorManager;
import com.craftaro.epicanchors.files.DataManager;
import com.craftaro.epicanchors.files.Settings; import com.craftaro.epicanchors.files.Settings;
import com.craftaro.epicanchors.utils.Callback; import com.craftaro.epicanchors.utils.Callback;
import com.craftaro.epicanchors.utils.DataHelper;
import com.craftaro.epicanchors.utils.UpdateCallback; import com.craftaro.epicanchors.utils.UpdateCallback;
import com.craftaro.epicanchors.utils.Utils; import com.craftaro.epicanchors.utils.Utils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -33,7 +31,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
@ -65,8 +62,7 @@ public class AnchorManagerImpl implements AnchorManager {
protected void saveAll() { protected void saveAll() {
for (Set<Anchor> anchorSet : this.anchors.values()) { for (Set<Anchor> anchorSet : this.anchors.values()) {
Collection<Data> asData = new ArrayList<>(anchorSet.size()); this.dataManager.updateAnchors(anchorSet, null);
this.dataManager.saveBatch(asData);
} }
} }
@ -87,7 +83,7 @@ public class AnchorManagerImpl implements AnchorManager {
long start = System.nanoTime(); long start = System.nanoTime();
DataHelper.getAnchors(world, (ex, result) -> { this.dataManager.getAnchors(world, (ex, result) -> {
if (ex == null) { if (ex == null) {
this.anchors.computeIfAbsent(world, key -> new HashSet<>()); this.anchors.computeIfAbsent(world, key -> new HashSet<>());
@ -118,10 +114,8 @@ public class AnchorManagerImpl implements AnchorManager {
protected void deInitAnchors(@NotNull World world) { protected void deInitAnchors(@NotNull World world) {
Set<Anchor> tmpAnchors = this.anchors.remove(world); Set<Anchor> tmpAnchors = this.anchors.remove(world);
if (tmpAnchors != null) { if (tmpAnchors != null) {
Collection<Data> asData = new ArrayList<>(tmpAnchors.size()); this.dataManager.updateAnchors(tmpAnchors, null);
this.dataManager.saveBatch(asData);
for (Anchor anchor : tmpAnchors) { for (Anchor anchor : tmpAnchors) {
((AnchorImpl) anchor).deInit(this.plugin); ((AnchorImpl) anchor).deInit(this.plugin);
@ -235,19 +229,27 @@ public class AnchorManagerImpl implements AnchorManager {
throw new IllegalStateException(ERR_WORLD_NOT_READY); throw new IllegalStateException(ERR_WORLD_NOT_READY);
} }
Anchor anchor = new AnchorImpl(dataManager.getNextId("anchors"), owner, loc, ticks); this.dataManager.insertAnchorAsync(loc, Objects.requireNonNull(owner), ticks, (ex, anchor) -> {
this.dataManager.save(anchor); if (ex != null) {
Bukkit.getScheduler().runTask(this.plugin, () -> { //TODO: Do we need to run this sync, or we are already on the main thread? if (callback != null) {
Block block = loc.getBlock(); callback.accept(ex, null);
block.setType(Settings.MATERIAL.getMaterial().parseMaterial()); } else {
Utils.logException(this.plugin, ex, "SQLite");
}
} else {
Bukkit.getScheduler().runTask(this.plugin, () -> {
Block block = loc.getBlock();
block.setType(Settings.MATERIAL.getMaterial().parseMaterial());
this.anchors.computeIfAbsent(anchor.getWorld(), key -> new HashSet<>()) this.anchors.computeIfAbsent(anchor.getWorld(), key -> new HashSet<>())
.add(anchor); .add(anchor);
updateHologram(anchor); updateHologram(anchor);
if (callback != null) { if (callback != null) {
callback.accept(null, anchor); callback.accept(null, anchor);
}
});
} }
}); });
} }
@ -290,7 +292,7 @@ public class AnchorManagerImpl implements AnchorManager {
anchor.getLocation().add(.5, .5, .5), 100, .5, .5, .5); anchor.getLocation().add(.5, .5, .5), 100, .5, .5, .5);
((AnchorImpl) anchor).deInit(this.plugin); ((AnchorImpl) anchor).deInit(this.plugin);
this.dataManager.delete(anchor); this.dataManager.deleteAnchorAsync(anchor);
} }
/* Anchor access */ /* Anchor access */

View File

@ -30,7 +30,6 @@ import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
@ -39,6 +38,8 @@ public final class EpicAnchors extends SongodaPlugin {
private GuiManager guiManager; private GuiManager guiManager;
private AnchorManagerImpl anchorManager; private AnchorManagerImpl anchorManager;
private DataManager dataManager;
@Override @Override
public void onPluginLoad() { public void onPluginLoad() {
} }
@ -48,15 +49,13 @@ public final class EpicAnchors extends SongodaPlugin {
SongodaCore.registerPlugin(this, 31, XMaterial.END_PORTAL_FRAME); SongodaCore.registerPlugin(this, 31, XMaterial.END_PORTAL_FRAME);
// Initialize database // Initialize database
// this.getLogger().info("Initializing SQLite..."); this.getLogger().info("Initializing SQLite...");
// DatabaseConnector dbCon = new SQLiteConnector(this); DatabaseConnector dbCon = new SQLiteConnector(this);
// this.dataManager = new DataManager(dbCon, this); this.dataManager = new DataManager(dbCon, this);
// AnchorMigration anchorMigration = new AnchorMigration(dbCon, this.dataManager, new _1_InitialMigration()); AnchorMigration anchorMigration = new AnchorMigration(dbCon, this.dataManager, new _1_InitialMigration());
// anchorMigration.runMigrations(); anchorMigration.runMigrations();
// anchorMigration.migrateLegacyData(this);
initDatabase(Arrays.asList(new _1_InitialMigration(), new AnchorMigration()));
anchorMigration.migrateLegacyData(this);
this.anchorManager = new AnchorManagerImpl(this, this.dataManager); this.anchorManager = new AnchorManagerImpl(this, this.dataManager);
EpicAnchorsApi.initApi(this.anchorManager); EpicAnchorsApi.initApi(this.anchorManager);
@ -101,7 +100,7 @@ public final class EpicAnchors extends SongodaPlugin {
if (this.dataManager != null) { if (this.dataManager != null) {
this.anchorManager.deInitAll(); this.anchorManager.deInitAll();
this.dataManager.shutdown(); this.dataManager.close();
} }
// Remove all holograms // Remove all holograms

View File

@ -1,5 +1,6 @@
package com.craftaro.epicanchors.files; package com.craftaro.epicanchors.files;
import com.craftaro.core.database.DataManagerAbstract;
import com.craftaro.core.database.DatabaseConnector; import com.craftaro.core.database.DatabaseConnector;
import com.craftaro.epicanchors.AnchorImpl; import com.craftaro.epicanchors.AnchorImpl;
import com.craftaro.epicanchors.api.Anchor; import com.craftaro.epicanchors.api.Anchor;
@ -27,71 +28,248 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class DataManager { public class DataManager extends DataManagerAbstract {
private final ExecutorService thread = Executors.newSingleThreadExecutor();
private final String anchorTable;
public DataManager(DatabaseConnector databaseConnector, Plugin plugin) {
super(databaseConnector, plugin);
// public void updateAnchors(Collection<Anchor> anchors, UpdateCallback callback) { this.anchorTable = getTableName(super.getTablePrefix(), "anchors");
// this.databaseConnector.connect((con) -> { }
// con.setAutoCommit(false);
// public void close() {
// SQLException err = null; if (!this.thread.isShutdown()) {
// this.thread.shutdown();
// for (Anchor anchor : anchors) {
// try (PreparedStatement ps = con.prepareStatement("UPDATE " + this.anchorTable + try {
// " SET ticks_left =? WHERE id =?;")) { if (!this.thread.awaitTermination(60, TimeUnit.SECONDS)) {
// ps.setInt(1, anchor.getTicksLeft()); // Try stopping the thread forcefully (there is basically no hope left for the data)
// ps.setInt(2, anchor.getDbId()); this.thread.shutdownNow();
// }
// ps.executeUpdate(); } catch (InterruptedException ex) {
// } catch (SQLException ex) { Utils.logException(super.plugin, ex);
// err = ex; }
// break;
// } this.databaseConnector.closeConnection();
// } }
// }
// if (err == null) {
// con.commit(); public void exists(@NotNull String worldName, int x, int y, int z, @NotNull Callback<Boolean> callback) {
// this.databaseConnector.connect((con) -> {
// resolveUpdateCallback(callback, null); try (PreparedStatement ps = con.prepareStatement("SELECT id FROM " + this.anchorTable +
// } else { " WHERE world_name =? AND x =? AND y =? AND z=?;")) {
// con.rollback(); ps.setString(1, worldName);
// ps.setInt(2, x);
// resolveUpdateCallback(callback, err); ps.setInt(3, y);
// } ps.setInt(4, z);
//
// con.setAutoCommit(true); ResultSet rs = ps.executeQuery();
// });
// } callback.accept(null, rs.next());
// } catch (Exception ex) {
// public void deleteAnchorAsync(Anchor anchor) { resolveCallback(callback, ex);
// deleteAnchorAsync(anchor, null); }
// } });
// }
// public void deleteAnchorAsync(Anchor anchor, UpdateCallback callback) {
// this.thread.execute(() -> public void getAnchors(@Nullable World world, @NotNull Callback<List<Anchor>> callback) {
// this.databaseConnector.connect((con) -> { List<Anchor> result = new ArrayList<>();
// try (PreparedStatement ps = con.prepareStatement("DELETE FROM " + this.anchorTable +
// " WHERE id =?;")) { this.databaseConnector.connect((con) -> {
// ps.setInt(1, anchor.getDbId()); try (PreparedStatement ps = con.prepareStatement("SELECT * FROM " + this.anchorTable +
// (world != null ? " WHERE world_name =?" : "") + ";")) {
// ps.executeUpdate(); if (world != null) {
// ps.setString(1, world.getName());
// resolveUpdateCallback(callback, null); }
// } catch (Exception ex) {
// resolveUpdateCallback(callback, ex); ResultSet rs = ps.executeQuery();
// }
// }) while (rs.next()) {
// ); result.add(extractAnchor(rs));
// } }
//
// public static String getTableName(String prefix, String name) { callback.accept(null, result);
// String result = prefix + name; } catch (Exception ex) {
// resolveCallback(callback, ex);
// if (!result.matches("[a-z0-9_]+")) { }
// throw new IllegalStateException("The generated table name '" + result + "' contains invalid characters"); });
// } }
//
// return result; public void insertAnchorAsync(Location loc, UUID owner, int ticks, Callback<Anchor> callback) {
// } this.thread.execute(() -> insertAnchor(loc, owner, ticks, callback));
}
public void insertAnchor(Location loc, UUID owner, int ticks, Callback<Anchor> callback) {
this.databaseConnector.connect((con) -> {
try (PreparedStatement ps = con.prepareStatement("INSERT INTO " + this.anchorTable +
"(owner, world_name,x,y,z, ticks_left) VALUES (?,?,?,?,?, ?);");// Future SQLite version might support 'RETURNING *'
PreparedStatement psFetch = con.prepareStatement("SELECT * FROM " + this.anchorTable +
" WHERE world_name =? AND x =? AND y =? AND z=?;")) {
ps.setString(1, owner != null ? owner.toString() : null);
ps.setString(2, Objects.requireNonNull(loc.getWorld()).getName());
psFetch.setString(1, Objects.requireNonNull(loc.getWorld()).getName());
ps.setInt(3, loc.getBlockX());
psFetch.setInt(2, loc.getBlockX());
ps.setInt(4, loc.getBlockY());
psFetch.setInt(3, loc.getBlockY());
ps.setInt(5, loc.getBlockZ());
psFetch.setInt(4, loc.getBlockZ());
ps.setInt(6, ticks);
ps.executeUpdate();
if (callback != null) {
ResultSet rs = psFetch.executeQuery();
rs.next();
callback.accept(null, extractAnchor(rs));
}
} catch (Exception ex) {
resolveCallback(callback, ex);
}
});
}
public void migrateAnchor(List<AnchorMigration.LegacyAnchorEntry> anchorEntries, UpdateCallback callback) {
this.databaseConnector.connect((con) -> {
con.setAutoCommit(false);
SQLException err = null;
try (PreparedStatement ps = con.prepareStatement("INSERT INTO " + this.anchorTable +
"(world_name,x,y,z, ticks_left) VALUES (?,?,?,?, ?);")) {
for (AnchorMigration.LegacyAnchorEntry entry : anchorEntries) {
ps.setString(1, entry.worldName);
ps.setInt(2, entry.x);
ps.setInt(3, entry.y);
ps.setInt(4, entry.z);
ps.setInt(5, entry.ticksLeft);
ps.addBatch();
}
int[] batchRes = ps.executeBatch();
for (int i : batchRes) {
if (i < 0 && i != Statement.SUCCESS_NO_INFO) {
throw new AssertionError("Batch-INSERT failed for at least one statement with code " + i);
}
}
} catch (SQLException ex) {
err = ex;
}
if (err == null) {
con.commit();
resolveUpdateCallback(callback, null);
} else {
con.rollback();
resolveUpdateCallback(callback, err);
}
con.setAutoCommit(true);
});
}
public void updateAnchors(Collection<Anchor> 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;
}
private Anchor extractAnchor(ResultSet rs) throws SQLException {
String ownerStr = rs.getString("owner");
return new AnchorImpl(rs.getInt("id"),
ownerStr != null ? UUID.fromString(ownerStr) : null,
new Location(Bukkit.getWorld(rs.getString("world_name")),
rs.getInt("x"),
rs.getInt("y"),
rs.getInt("z")),
rs.getInt("ticks_left"));
}
private void resolveUpdateCallback(@Nullable UpdateCallback callback, @Nullable Exception ex) {
if (callback != null) {
callback.accept(ex);
} else if (ex != null) {
Utils.logException(this.plugin, ex, "SQLite");
}
}
private void resolveCallback(@Nullable Callback<?> callback, @NotNull Exception ex) {
if (callback != null) {
callback.accept(ex, null);
} else {
Utils.logException(this.plugin, ex, "SQLite");
}
}
} }

View File

@ -3,32 +3,31 @@ package com.craftaro.epicanchors.files.migration;
import com.craftaro.core.configuration.Config; import com.craftaro.core.configuration.Config;
import com.craftaro.core.configuration.ConfigSection; import com.craftaro.core.configuration.ConfigSection;
import com.craftaro.core.database.DataMigration; import com.craftaro.core.database.DataMigration;
import com.craftaro.core.database.DataMigrationManager;
import com.craftaro.core.database.DatabaseConnector; import com.craftaro.core.database.DatabaseConnector;
import com.craftaro.epicanchors.EpicAnchors;
import com.craftaro.epicanchors.files.DataManager; import com.craftaro.epicanchors.files.DataManager;
import com.craftaro.epicanchors.utils.DataHelper;
import com.craftaro.epicanchors.utils.ThreadSync; import com.craftaro.epicanchors.utils.ThreadSync;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.sql.Connection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level; import java.util.logging.Level;
public class AnchorMigration extends DataMigration { public class AnchorMigration extends DataMigrationManager {
private final DataManager dataManager;
public AnchorMigration() { public AnchorMigration(DatabaseConnector databaseConnector, DataManager dataManager, DataMigration... migrations) {
super(2); super(databaseConnector, dataManager, migrations);
this.dataManager = dataManager;
} }
@Override public void migrateLegacyData(Plugin plugin) {
public void migrate(DatabaseConnector databaseConnector, String tablePrefix) {
Plugin plugin = EpicAnchors.getPlugin(EpicAnchors.class);
long start = System.nanoTime(); long start = System.nanoTime();
AtomicBoolean abortMigration = new AtomicBoolean(false); AtomicBoolean abortMigration = new AtomicBoolean(false);
@ -68,7 +67,7 @@ public class AnchorMigration extends DataMigration {
int z = Location.locToBlock(Double.parseDouble(locArgs[3])); int z = Location.locToBlock(Double.parseDouble(locArgs[3]));
int finalTicksLeft = ticksLeft; int finalTicksLeft = ticksLeft;
DataHelper.exists(worldName, x, y, z, (ex, anchorExists) -> { this.dataManager.exists(worldName, x, y, z, (ex, anchorExists) -> {
if (ex == null) { if (ex == null) {
if (anchorExists) { if (anchorExists) {
cfgSection.set(locationStr, null); cfgSection.set(locationStr, null);
@ -95,7 +94,7 @@ public class AnchorMigration extends DataMigration {
if (!abortMigration.get()) { if (!abortMigration.get()) {
int finalMigratedAnchors = migratedAnchors; int finalMigratedAnchors = migratedAnchors;
DataHelper.migrateAnchor(anchorQueue, ex -> { this.dataManager.migrateAnchor(anchorQueue, ex -> {
long end = System.nanoTime(); long end = System.nanoTime();
if (ex == null) { if (ex == null) {

View File

@ -1,8 +1,6 @@
package com.craftaro.epicanchors.files.migration; package com.craftaro.epicanchors.files.migration;
import com.craftaro.core.database.DataMigration; import com.craftaro.core.database.DataMigration;
import com.craftaro.core.database.DatabaseConnector;
import com.craftaro.epicanchors.EpicAnchors;
import com.craftaro.epicanchors.files.DataManager; import com.craftaro.epicanchors.files.DataManager;
import java.sql.Connection; import java.sql.Connection;
@ -15,9 +13,9 @@ public class _1_InitialMigration extends DataMigration {
} }
@Override @Override
public void migrate(DatabaseConnector databaseConnector, String tablePrefix) throws SQLException { public void migrate(Connection connection, String tablePrefix) throws SQLException {
try (Statement statement = databaseConnector.getConnection().createStatement()) { try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE " + EpicAnchors.getPlugin(EpicAnchors.class).getDataManager().getTablePrefix() + "anchors (" + statement.execute("CREATE TABLE " + DataManager.getTableName(tablePrefix, "anchors") + "(" +
"id INTEGER NOT NULL," + "id INTEGER NOT NULL," +
"world_name TEXT NOT NULL," + "world_name TEXT NOT NULL," +
"x INTEGER NOT NULL," + "x INTEGER NOT NULL," +

View File

@ -49,7 +49,7 @@ public class AnchorListener implements Listener {
this.plugin.getAnchorManager().createAnchor(e.getBlock().getLocation(), e.getPlayer().getUniqueId(), ticksLeft, this.plugin.getAnchorManager().createAnchor(e.getBlock().getLocation(), e.getPlayer().getUniqueId(), ticksLeft,
(ex, result) -> { (ex, result) -> {
if (ex != null) { if (ex != null) {
Utils.logException(this.plugin, ex, "SQL"); Utils.logException(this.plugin, ex, "SQLite");
e.getPlayer().sendMessage("Error creating anchor!"); // TODO e.getPlayer().sendMessage("Error creating anchor!"); // TODO
Bukkit.getScheduler().runTask(this.plugin, () -> { Bukkit.getScheduler().runTask(this.plugin, () -> {

View File

@ -1,101 +0,0 @@
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;
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.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 {
public static void exists(@NotNull String worldName, int x, int y, int z, @NotNull Callback<Boolean> callback) {
DataManager dataManager = EpicAnchors.getPlugin(EpicAnchors.class).getDataManager();
dataManager.getDatabaseConnector().connectDSL(dslContext -> {
try {
@NotNull Result<Record1<Object>> result = dslContext.select(DSL.field("id")).from(DSL.table(dataManager.getTablePrefix() + "anchors"))
.where(DSL.field("world_name").eq(worldName))
.and(DSL.field("x").eq(x))
.and(DSL.field("y").eq(y))
.and(DSL.field("z").eq(z)).fetch();
callback.accept(null, result.size() > 0);
} catch (Exception ex) {
resolveCallback(callback, ex);
}
});
}
public static void getAnchors(@Nullable World world, @NotNull Callback<List<Anchor>> callback) {
try {
callback.accept(null, EpicAnchors.getPlugin(EpicAnchors.class).getDataManager().loadBatch(AnchorImpl.class, "anchors"));
} catch (Exception ex) {
resolveCallback(callback, ex);
}
}
private static void resolveUpdateCallback(@Nullable UpdateCallback callback, @Nullable Exception ex) {
if (callback != null) {
callback.accept(ex);
} else if (ex != null) {
Utils.logException(EpicAnchors.getPlugin(EpicAnchors.class), ex, "SQL");
}
}
private static void resolveCallback(@Nullable Callback<?> callback, @NotNull Exception ex) {
if (callback != null) {
callback.accept(ex, null);
} else {
Utils.logException(EpicAnchors.getPlugin(EpicAnchors.class), ex, "SQL");
}
}
public static void migrateAnchor(List<AnchorMigration.LegacyAnchorEntry> anchorQueue, UpdateCallback callback) {
DataManager dataManager = EpicAnchors.getPlugin(EpicAnchors.class).getDataManager();
//recreate it with Jooq
dataManager.getDatabaseConnector().connectDSL(dslContext -> {
Connection connection = dslContext.configuration().connectionProvider().acquire();
connection.setAutoCommit(false);
try {
List<Query> queries = new ArrayList<>();
for (AnchorMigration.LegacyAnchorEntry entry : anchorQueue) {
queries.add(dslContext.insertInto(DSL.table(dataManager.getTablePrefix() + "anchors"))
.columns(
DSL.field("world_name"),
DSL.field("x"),
DSL.field("y"),
DSL.field("z"),
DSL.field("ticks_left"))
.values(entry.worldName, entry.x, entry.y, entry.z, entry.ticksLeft));
}
dslContext.batch(queries).execute();
connection.commit();
} catch (Exception ex) {
connection.rollback();
resolveUpdateCallback(callback, ex);
} finally {
connection.setAutoCommit(true);
dslContext.configuration().connectionProvider().release(connection);
}
});
}
}