mirror of
https://github.com/songoda/EpicAnchors.git
synced 2024-10-31 23:59:35 +01:00
Revert "Updates to new database system"
This reverts commit f3c8778ad2
.
This commit is contained in:
parent
b4c5282990
commit
f3377b08f9
@ -1,6 +1,5 @@
|
||||
package com.craftaro.epicanchors.api;
|
||||
|
||||
import com.craftaro.core.database.Data;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
@ -8,7 +7,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public interface Anchor extends Data {
|
||||
public interface Anchor {
|
||||
int getDbId();
|
||||
|
||||
UUID getOwner();
|
||||
|
@ -1,7 +1,5 @@
|
||||
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.utils.WorldUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
@ -12,8 +10,6 @@ import org.bukkit.plugin.Plugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
@ -25,15 +21,6 @@ public class AnchorImpl implements Anchor {
|
||||
private final Location location;
|
||||
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) {
|
||||
if (dbId <= 0) throw new IllegalArgumentException("Invalid value for dbId");
|
||||
if (ticksLeft <= 0 && ticksLeft != -1) throw new IllegalArgumentException("Invalid value for ticksLeft");
|
||||
@ -150,29 +137,4 @@ public class AnchorImpl implements Anchor {
|
||||
public boolean isInfinite() {
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
@ -3,8 +3,6 @@ package com.craftaro.epicanchors;
|
||||
import com.craftaro.core.SongodaPlugin;
|
||||
import com.craftaro.core.compatibility.CompatibleMaterial;
|
||||
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.third_party.com.cryptomorin.xseries.XMaterial;
|
||||
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.AnchorAccessCheck;
|
||||
import com.craftaro.epicanchors.api.AnchorManager;
|
||||
import com.craftaro.epicanchors.files.DataManager;
|
||||
import com.craftaro.epicanchors.files.Settings;
|
||||
import com.craftaro.epicanchors.utils.Callback;
|
||||
import com.craftaro.epicanchors.utils.DataHelper;
|
||||
import com.craftaro.epicanchors.utils.UpdateCallback;
|
||||
import com.craftaro.epicanchors.utils.Utils;
|
||||
import org.bukkit.Bukkit;
|
||||
@ -33,7 +31,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
@ -65,8 +62,7 @@ public class AnchorManagerImpl implements AnchorManager {
|
||||
|
||||
protected void saveAll() {
|
||||
for (Set<Anchor> anchorSet : this.anchors.values()) {
|
||||
Collection<Data> asData = new ArrayList<>(anchorSet.size());
|
||||
this.dataManager.saveBatch(asData);
|
||||
this.dataManager.updateAnchors(anchorSet, null);
|
||||
}
|
||||
}
|
||||
|
||||
@ -87,7 +83,7 @@ public class AnchorManagerImpl implements AnchorManager {
|
||||
|
||||
long start = System.nanoTime();
|
||||
|
||||
DataHelper.getAnchors(world, (ex, result) -> {
|
||||
this.dataManager.getAnchors(world, (ex, result) -> {
|
||||
if (ex == null) {
|
||||
this.anchors.computeIfAbsent(world, key -> new HashSet<>());
|
||||
|
||||
@ -118,10 +114,8 @@ public class AnchorManagerImpl implements AnchorManager {
|
||||
protected void deInitAnchors(@NotNull World world) {
|
||||
Set<Anchor> tmpAnchors = this.anchors.remove(world);
|
||||
|
||||
|
||||
if (tmpAnchors != null) {
|
||||
Collection<Data> asData = new ArrayList<>(tmpAnchors.size());
|
||||
this.dataManager.saveBatch(asData);
|
||||
this.dataManager.updateAnchors(tmpAnchors, null);
|
||||
|
||||
for (Anchor anchor : tmpAnchors) {
|
||||
((AnchorImpl) anchor).deInit(this.plugin);
|
||||
@ -235,9 +229,15 @@ public class AnchorManagerImpl implements AnchorManager {
|
||||
throw new IllegalStateException(ERR_WORLD_NOT_READY);
|
||||
}
|
||||
|
||||
Anchor anchor = new AnchorImpl(dataManager.getNextId("anchors"), owner, loc, ticks);
|
||||
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?
|
||||
this.dataManager.insertAnchorAsync(loc, Objects.requireNonNull(owner), ticks, (ex, anchor) -> {
|
||||
if (ex != null) {
|
||||
if (callback != null) {
|
||||
callback.accept(ex, null);
|
||||
} else {
|
||||
Utils.logException(this.plugin, ex, "SQLite");
|
||||
}
|
||||
} else {
|
||||
Bukkit.getScheduler().runTask(this.plugin, () -> {
|
||||
Block block = loc.getBlock();
|
||||
block.setType(Settings.MATERIAL.getMaterial().parseMaterial());
|
||||
|
||||
@ -251,6 +251,8 @@ public class AnchorManagerImpl implements AnchorManager {
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroyAnchor(@NotNull Anchor anchor) {
|
||||
@ -290,7 +292,7 @@ public class AnchorManagerImpl implements AnchorManager {
|
||||
anchor.getLocation().add(.5, .5, .5), 100, .5, .5, .5);
|
||||
|
||||
((AnchorImpl) anchor).deInit(this.plugin);
|
||||
this.dataManager.delete(anchor);
|
||||
this.dataManager.deleteAnchorAsync(anchor);
|
||||
}
|
||||
|
||||
/* Anchor access */
|
||||
|
@ -30,7 +30,6 @@ import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
@ -39,6 +38,8 @@ public final class EpicAnchors extends SongodaPlugin {
|
||||
private GuiManager guiManager;
|
||||
private AnchorManagerImpl anchorManager;
|
||||
|
||||
private DataManager dataManager;
|
||||
|
||||
@Override
|
||||
public void onPluginLoad() {
|
||||
}
|
||||
@ -48,15 +49,13 @@ public final class EpicAnchors extends SongodaPlugin {
|
||||
SongodaCore.registerPlugin(this, 31, XMaterial.END_PORTAL_FRAME);
|
||||
|
||||
// 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.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);
|
||||
this.anchorManager = new AnchorManagerImpl(this, this.dataManager);
|
||||
EpicAnchorsApi.initApi(this.anchorManager);
|
||||
|
||||
@ -101,7 +100,7 @@ public final class EpicAnchors extends SongodaPlugin {
|
||||
if (this.dataManager != null) {
|
||||
this.anchorManager.deInitAll();
|
||||
|
||||
this.dataManager.shutdown();
|
||||
this.dataManager.close();
|
||||
}
|
||||
|
||||
// Remove all holograms
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.craftaro.epicanchors.files;
|
||||
|
||||
import com.craftaro.core.database.DataManagerAbstract;
|
||||
import com.craftaro.core.database.DatabaseConnector;
|
||||
import com.craftaro.epicanchors.AnchorImpl;
|
||||
import com.craftaro.epicanchors.api.Anchor;
|
||||
@ -27,71 +28,248 @@ import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
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.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;
|
||||
// }
|
||||
this.anchorTable = getTableName(super.getTablePrefix(), "anchors");
|
||||
}
|
||||
|
||||
public void close() {
|
||||
if (!this.thread.isShutdown()) {
|
||||
this.thread.shutdown();
|
||||
|
||||
try {
|
||||
if (!this.thread.awaitTermination(60, TimeUnit.SECONDS)) {
|
||||
// Try stopping the thread forcefully (there is basically no hope left for the data)
|
||||
this.thread.shutdownNow();
|
||||
}
|
||||
} catch (InterruptedException ex) {
|
||||
Utils.logException(super.plugin, ex);
|
||||
}
|
||||
|
||||
this.databaseConnector.closeConnection();
|
||||
}
|
||||
}
|
||||
|
||||
public void exists(@NotNull String worldName, int x, int y, int z, @NotNull Callback<Boolean> callback) {
|
||||
this.databaseConnector.connect((con) -> {
|
||||
try (PreparedStatement ps = con.prepareStatement("SELECT id FROM " + this.anchorTable +
|
||||
" WHERE world_name =? AND x =? AND y =? AND z=?;")) {
|
||||
ps.setString(1, worldName);
|
||||
ps.setInt(2, x);
|
||||
ps.setInt(3, y);
|
||||
ps.setInt(4, z);
|
||||
|
||||
ResultSet rs = ps.executeQuery();
|
||||
|
||||
callback.accept(null, rs.next());
|
||||
} catch (Exception ex) {
|
||||
resolveCallback(callback, ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void getAnchors(@Nullable World world, @NotNull Callback<List<Anchor>> callback) {
|
||||
List<Anchor> result = new ArrayList<>();
|
||||
|
||||
this.databaseConnector.connect((con) -> {
|
||||
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM " + this.anchorTable +
|
||||
(world != null ? " WHERE world_name =?" : "") + ";")) {
|
||||
if (world != null) {
|
||||
ps.setString(1, world.getName());
|
||||
}
|
||||
|
||||
ResultSet rs = ps.executeQuery();
|
||||
|
||||
while (rs.next()) {
|
||||
result.add(extractAnchor(rs));
|
||||
}
|
||||
|
||||
callback.accept(null, result);
|
||||
} catch (Exception ex) {
|
||||
resolveCallback(callback, ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,32 +3,31 @@ package com.craftaro.epicanchors.files.migration;
|
||||
import com.craftaro.core.configuration.Config;
|
||||
import com.craftaro.core.configuration.ConfigSection;
|
||||
import com.craftaro.core.database.DataMigration;
|
||||
import com.craftaro.core.database.DataMigrationManager;
|
||||
import com.craftaro.core.database.DatabaseConnector;
|
||||
import com.craftaro.epicanchors.EpicAnchors;
|
||||
import com.craftaro.epicanchors.files.DataManager;
|
||||
import com.craftaro.epicanchors.utils.DataHelper;
|
||||
import com.craftaro.epicanchors.utils.ThreadSync;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.sql.Connection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class AnchorMigration extends DataMigration {
|
||||
public class AnchorMigration extends DataMigrationManager {
|
||||
private final DataManager dataManager;
|
||||
|
||||
public AnchorMigration() {
|
||||
super(2);
|
||||
public AnchorMigration(DatabaseConnector databaseConnector, DataManager dataManager, DataMigration... migrations) {
|
||||
super(databaseConnector, dataManager, migrations);
|
||||
|
||||
this.dataManager = dataManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void migrate(DatabaseConnector databaseConnector, String tablePrefix) {
|
||||
Plugin plugin = EpicAnchors.getPlugin(EpicAnchors.class);
|
||||
public void migrateLegacyData(Plugin plugin) {
|
||||
long start = System.nanoTime();
|
||||
|
||||
AtomicBoolean abortMigration = new AtomicBoolean(false);
|
||||
@ -68,7 +67,7 @@ public class AnchorMigration extends DataMigration {
|
||||
int z = Location.locToBlock(Double.parseDouble(locArgs[3]));
|
||||
|
||||
int finalTicksLeft = ticksLeft;
|
||||
DataHelper.exists(worldName, x, y, z, (ex, anchorExists) -> {
|
||||
this.dataManager.exists(worldName, x, y, z, (ex, anchorExists) -> {
|
||||
if (ex == null) {
|
||||
if (anchorExists) {
|
||||
cfgSection.set(locationStr, null);
|
||||
@ -95,7 +94,7 @@ public class AnchorMigration extends DataMigration {
|
||||
|
||||
if (!abortMigration.get()) {
|
||||
int finalMigratedAnchors = migratedAnchors;
|
||||
DataHelper.migrateAnchor(anchorQueue, ex -> {
|
||||
this.dataManager.migrateAnchor(anchorQueue, ex -> {
|
||||
long end = System.nanoTime();
|
||||
|
||||
if (ex == null) {
|
||||
|
@ -1,8 +1,6 @@
|
||||
package com.craftaro.epicanchors.files.migration;
|
||||
|
||||
import com.craftaro.core.database.DataMigration;
|
||||
import com.craftaro.core.database.DatabaseConnector;
|
||||
import com.craftaro.epicanchors.EpicAnchors;
|
||||
import com.craftaro.epicanchors.files.DataManager;
|
||||
|
||||
import java.sql.Connection;
|
||||
@ -15,9 +13,9 @@ public class _1_InitialMigration extends DataMigration {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void migrate(DatabaseConnector databaseConnector, String tablePrefix) throws SQLException {
|
||||
try (Statement statement = databaseConnector.getConnection().createStatement()) {
|
||||
statement.execute("CREATE TABLE " + EpicAnchors.getPlugin(EpicAnchors.class).getDataManager().getTablePrefix() + "anchors (" +
|
||||
public void migrate(Connection connection, String tablePrefix) throws SQLException {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
statement.execute("CREATE TABLE " + DataManager.getTableName(tablePrefix, "anchors") + "(" +
|
||||
"id INTEGER NOT NULL," +
|
||||
"world_name TEXT NOT NULL," +
|
||||
"x INTEGER NOT NULL," +
|
||||
|
@ -49,7 +49,7 @@ public class AnchorListener implements Listener {
|
||||
this.plugin.getAnchorManager().createAnchor(e.getBlock().getLocation(), e.getPlayer().getUniqueId(), ticksLeft,
|
||||
(ex, result) -> {
|
||||
if (ex != null) {
|
||||
Utils.logException(this.plugin, ex, "SQL");
|
||||
Utils.logException(this.plugin, ex, "SQLite");
|
||||
e.getPlayer().sendMessage("Error creating anchor!"); // TODO
|
||||
|
||||
Bukkit.getScheduler().runTask(this.plugin, () -> {
|
||||
|
@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user