Skip to content

Commit

Permalink
Fixed issues regarding sqlite being locked and other weird potentiall…
Browse files Browse the repository at this point in the history
…y unexpected behaviors (#873)
  • Loading branch information
OmerBenGera committed Jan 25, 2022
1 parent f8c016f commit 975681e
Show file tree
Hide file tree
Showing 16 changed files with 492 additions and 976 deletions.
Expand Up @@ -15,6 +15,7 @@
import com.bgsoftware.superiorskyblock.database.serialization.PlayersDeserializer;
import com.bgsoftware.superiorskyblock.database.sql.SQLDatabaseInitializer;
import com.bgsoftware.superiorskyblock.database.sql.SQLHelper;
import com.bgsoftware.superiorskyblock.database.sql.session.QueryResult;
import com.bgsoftware.superiorskyblock.handler.AbstractHandler;
import com.bgsoftware.superiorskyblock.handler.HandlerLoadException;
import com.bgsoftware.superiorskyblock.island.SPlayerRole;
Expand Down Expand Up @@ -61,15 +62,15 @@ public void loadData() throws HandlerLoadException {

if (!plugin.getFactory().hasCustomDatabaseBridge()) {
SQLDatabaseInitializer.getInstance().createIndexes();
SQLHelper.setJournalMode("MEMORY");
SQLHelper.setJournalMode("MEMORY", QueryResult.EMPTY_QUERY_RESULT);
}

loadPlayers();
loadIslands();
loadGrid();

if (!plugin.getFactory().hasCustomDatabaseBridge()) {
SQLHelper.setJournalMode("DELETE");
SQLHelper.setJournalMode("DELETE", QueryResult.EMPTY_QUERY_RESULT);
}

/*
Expand Down
Expand Up @@ -23,17 +23,18 @@
import com.bgsoftware.superiorskyblock.database.loader.v1.deserializer.RawDeserializer;
import com.bgsoftware.superiorskyblock.database.sql.ResultSetMapBridge;
import com.bgsoftware.superiorskyblock.database.sql.StatementHolder;
import com.bgsoftware.superiorskyblock.database.sql.session.MySQLSession;
import com.bgsoftware.superiorskyblock.database.sql.session.QueryResult;
import com.bgsoftware.superiorskyblock.database.sql.session.SQLSession;
import com.bgsoftware.superiorskyblock.database.sql.session.SQLiteSession;
import com.bgsoftware.superiorskyblock.database.sql.session.impl.MySQLSession;
import com.bgsoftware.superiorskyblock.database.sql.session.impl.SQLiteSession;
import com.bgsoftware.superiorskyblock.island.SPlayerRole;
import com.bgsoftware.superiorskyblock.island.permissions.PlayerPermissionNode;
import com.bgsoftware.superiorskyblock.key.dataset.KeyMap;
import org.bukkit.World;
import org.bukkit.potion.PotionEffectType;

import java.io.File;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -68,51 +69,51 @@ public static void register(DataHandler dataHandler) {
public void loadData() {
SuperiorSkyblockPlugin.log("&a[Database-Converter] Detected old database - starting to convert data...");

session.select("players", "").ifSuccess(resultSet -> {
session.select("players", "", new QueryResult<ResultSet>().onSuccess(resultSet -> {
while (resultSet.next()) {
loadedPlayers.add(loadPlayer(new ResultSetMapBridge(resultSet)));
}
}).ifFail(QueryResult.PRINT_ERROR);
}).onFail(QueryResult.PRINT_ERROR));

SuperiorSkyblockPlugin.log("&a[Database-Converter] Found " + loadedPlayers.size() + " players in the database.");

session.select("islands", "").ifSuccess(resultSet -> {
session.select("islands", "", new QueryResult<ResultSet>().onSuccess(resultSet -> {
while (resultSet.next()) {
loadedIslands.add(loadIsland(new ResultSetMapBridge(resultSet)));
}
}).ifFail(QueryResult.PRINT_ERROR);
}).onFail(QueryResult.PRINT_ERROR));

SuperiorSkyblockPlugin.log("&a[Database-Converter] Found " + loadedIslands.size() + " islands in the database.");

session.select("stackedBlocks", "").ifSuccess(resultSet -> {
session.select("stackedBlocks", "", new QueryResult<ResultSet>().onSuccess(resultSet -> {
while (resultSet.next()) {
loadedBlocks.add(loadStackedBlock(new ResultSetMapBridge(resultSet)));
}
}).ifFail(QueryResult.PRINT_ERROR);
}).onFail(QueryResult.PRINT_ERROR));

SuperiorSkyblockPlugin.log("&a[Database-Converter] Found " + loadedBlocks.size() + " stacked blocks in the database.");

// Ignoring errors as the bankTransactions table may not exist.
AtomicBoolean foundBankTransaction = new AtomicBoolean(false);
session.select("bankTransactions", "").ifSuccess(resultSet -> {
session.select("bankTransactions", "", new QueryResult<ResultSet>().onSuccess(resultSet -> {
foundBankTransaction.set(true);
while (resultSet.next()) {
loadedBankTransactions.add(loadBankTransaction(new ResultSetMapBridge(resultSet)));
}
});
}));

if (foundBankTransaction.get()) {
SuperiorSkyblockPlugin.log("&a[Database-Converter] Found " + loadedBankTransactions.size() + " bank transactions in the database.");
}

session.select("grid", "").ifSuccess(resultSet -> {
session.select("grid", "", new QueryResult<ResultSet>().onSuccess(resultSet -> {
if (resultSet.next()) {
gridAttributes = new GridAttributes()
.setValue(GridAttributes.Field.LAST_ISLAND, resultSet.getString("lastIsland"))
.setValue(GridAttributes.Field.MAX_ISLAND_SIZE, resultSet.getString("maxIslandSize"))
.setValue(GridAttributes.Field.WORLD, resultSet.getString("world"));
}
}).ifFail(QueryResult.PRINT_ERROR);
}).onFail(QueryResult.PRINT_ERROR));

AtomicBoolean failedBackup = new AtomicBoolean(true);

Expand All @@ -131,30 +132,30 @@ public void loadData() {

failedBackup.set(false);

session.renameTable("islands", "bkp_islands").ifFail(error -> {
session.renameTable("islands", "bkp_islands", new QueryResult<Void>().onFail(error -> {
error.printStackTrace();
failedBackup.set(true);
});
}));

session.renameTable("players", "bkp_players").ifFail(error -> {
session.renameTable("players", "bkp_players", new QueryResult<Void>().onFail(error -> {
error.printStackTrace();
failedBackup.set(true);
});
}));

session.renameTable("grid", "bkp_grid").ifFail(error -> {
session.renameTable("grid", "bkp_grid", new QueryResult<Void>().onFail(error -> {
error.printStackTrace();
failedBackup.set(true);
});
}));

session.renameTable("stackedBlocks", "bkp_stackedBlocks").ifFail(error -> {
session.renameTable("stackedBlocks", "bkp_stackedBlocks", new QueryResult<Void>().onFail(error -> {
error.printStackTrace();
failedBackup.set(true);
});
}));

session.renameTable("bankTransactions", "bkp_bankTransactions").ifFail(error -> {
session.renameTable("bankTransactions", "bkp_bankTransactions", new QueryResult<Void>().onFail(error -> {
error.printStackTrace();
failedBackup.set(true);
});
}));
}

if (isRemoteDatabase)
Expand Down Expand Up @@ -194,10 +195,10 @@ private static boolean isDatabaseOldFormat() {

AtomicBoolean isOldFormat = new AtomicBoolean(true);

session.select("stackedBlocks", "").ifFail(error -> {
session.select("stackedBlocks", "", new QueryResult<ResultSet>().onFail(error -> {
session.closeConnection();
isOldFormat.set(false);
});
}));

return isOldFormat.get();
}
Expand Down
Expand Up @@ -7,6 +7,7 @@
import com.bgsoftware.superiorskyblock.database.sql.session.QueryResult;
import com.bgsoftware.superiorskyblock.utils.debug.PluginDebugger;

import java.sql.ResultSet;
import java.util.Map;
import java.util.function.Consumer;

Expand Down Expand Up @@ -40,7 +41,7 @@ private static String getColumnFilter(DatabaseFilter filter) {

@Override
public void loadAllObjects(String table, Consumer<Map<String, Object>> resultConsumer) {
SQLHelper.select(table, "").ifSuccess(resultSet -> {
SQLHelper.select(table, "", new QueryResult<ResultSet>().onSuccess(resultSet -> {
while (resultSet.next()) {
try {
resultConsumer.accept(new ResultSetMapBridge(resultSet));
Expand All @@ -49,7 +50,7 @@ public void loadAllObjects(String table, Consumer<Map<String, Object>> resultCon
PluginDebugger.debug(ex);
}
}
}).ifFail(QueryResult.PRINT_ERROR);
}).onFail(QueryResult.PRINT_ERROR));
}

@Override
Expand Down Expand Up @@ -145,7 +146,7 @@ public void loadObject(String table, DatabaseFilter filter, Consumer<Map<String,
String.format("'%s'", filterPair.getValue()) : filterPair.getValue().toString());
}

SQLHelper.select(table, columnFilter).ifSuccess(resultSet -> {
SQLHelper.select(table, columnFilter, new QueryResult<ResultSet>().onSuccess(resultSet -> {
while (resultSet.next()) {
try {
resultConsumer.accept(new ResultSetMapBridge(resultSet));
Expand All @@ -154,7 +155,7 @@ public void loadObject(String table, DatabaseFilter filter, Consumer<Map<String,
PluginDebugger.debug(ex);
}
}
}).ifFail(QueryResult.PRINT_ERROR);
}).onFail(QueryResult.PRINT_ERROR));
}

@Override
Expand Down
Expand Up @@ -3,8 +3,11 @@
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.objects.Pair;
import com.bgsoftware.superiorskyblock.database.bridge.GridDatabaseBridge;
import com.bgsoftware.superiorskyblock.database.sql.session.QueryResult;
import com.bgsoftware.superiorskyblock.handler.HandlerLoadException;

import java.sql.ResultSet;

public final class SQLDatabaseInitializer {

private static final SQLDatabaseInitializer instance = new SQLDatabaseInitializer();
Expand All @@ -29,8 +32,8 @@ public void init(SuperiorSkyblockPlugin plugin) throws HandlerLoadException {
createBankTransactionsTable();
createStackedBlocksTable();

SQLHelper.select("grid", "")
.ifFail(error -> GridDatabaseBridge.insertGrid(plugin.getGrid()));
SQLHelper.select("grid", "", new QueryResult<ResultSet>()
.onFail(error -> GridDatabaseBridge.insertGrid(plugin.getGrid())));
}

public void createIndexes() {
Expand Down
Expand Up @@ -2,12 +2,12 @@

import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.objects.Pair;
import com.bgsoftware.superiorskyblock.database.sql.session.MariaDBSession;
import com.bgsoftware.superiorskyblock.database.sql.session.MySQLSession;
import com.bgsoftware.superiorskyblock.database.sql.session.PostgreSQLSession;
import com.bgsoftware.superiorskyblock.database.sql.session.QueryResult;
import com.bgsoftware.superiorskyblock.database.sql.session.SQLSession;
import com.bgsoftware.superiorskyblock.database.sql.session.SQLiteSession;
import com.bgsoftware.superiorskyblock.database.sql.session.impl.MariaDBSession;
import com.bgsoftware.superiorskyblock.database.sql.session.impl.MySQLSession;
import com.bgsoftware.superiorskyblock.database.sql.session.impl.PostgreSQLSession;
import com.bgsoftware.superiorskyblock.database.sql.session.impl.SQLiteSession;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand Down Expand Up @@ -62,30 +62,32 @@ public static boolean createConnection(SuperiorSkyblockPlugin plugin) {

public static void createTable(String tableName, Pair<String, String>... columns) {
if (isReady())
globalSession.createTable(tableName, columns);
globalSession.createTable(tableName, columns, QueryResult.EMPTY_VOID_QUERY_RESULT);
}

public static void createIndex(String indexName, String tableName, String... columns) {
if (isReady())
globalSession.createIndex(indexName, tableName, columns);
globalSession.createIndex(indexName, tableName, columns, QueryResult.EMPTY_VOID_QUERY_RESULT);
}

public static void modifyColumnType(String tableName, String columnName, String newType) {
if (isReady())
globalSession.modifyColumnType(tableName, columnName, newType);
globalSession.modifyColumnType(tableName, columnName, newType, QueryResult.EMPTY_VOID_QUERY_RESULT);
}

public static QueryResult<ResultSet> select(String tableName, String filters) {
return isReady() ? globalSession.select(tableName, filters) : QueryResult.RESULT_SET_ERROR;
public static void select(String tableName, String filters, QueryResult<ResultSet> queryResult) {
if (isReady())
globalSession.select(tableName, filters, queryResult);
}

public static void setJournalMode(String jounralMode) {
public static void setJournalMode(String jounralMode, QueryResult<ResultSet> queryResult) {
if (isReady())
globalSession.setJournalMode(jounralMode);
globalSession.setJournalMode(jounralMode, queryResult);
}

public static QueryResult<PreparedStatement> customQuery(String query) {
return isReady() ? globalSession.customQuery(query) : QueryResult.PREPARED_STATEMENT_ERROR;
public static void customQuery(String query, QueryResult<PreparedStatement> queryResult) {
if (isReady())
globalSession.customQuery(query, queryResult);
}

public static void close() {
Expand Down
@@ -1,9 +1,11 @@
package com.bgsoftware.superiorskyblock.database.sql;

import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.database.sql.session.QueryResult;
import com.bgsoftware.superiorskyblock.threads.Executor;
import com.bgsoftware.superiorskyblock.utils.debug.PluginDebugger;

import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -58,7 +60,7 @@ public void executeBatch(boolean async) {

synchronized (mutex.get()) {
PluginDebugger.debug("Action: Database Execute, Query: " + query);
SQLHelper.customQuery(query).ifSuccess(preparedStatement -> {
SQLHelper.customQuery(query, new QueryResult<PreparedStatement>().onSuccess(preparedStatement -> {
SQLHelper.setAutoCommit(false);

for (Map<Integer, Object> values : batches) {
Expand All @@ -76,10 +78,10 @@ public void executeBatch(boolean async) {
}

SQLHelper.setAutoCommit(true);
}).ifFail(error -> {
}).onFail(error -> {
SuperiorSkyblockPlugin.log("&cFailed to execute query " + errorQuery);
error.printStackTrace();
});
}));
}
} finally {
values.clear();
Expand Down Expand Up @@ -107,16 +109,16 @@ public void execute(boolean async) {

synchronized (mutex.get()) {
PluginDebugger.debug("Action: Database Execute, Query: " + query);
SQLHelper.customQuery(query).ifSuccess(preparedStatement -> {
SQLHelper.customQuery(query, new QueryResult<PreparedStatement>().onSuccess(preparedStatement -> {
for (Map.Entry<Integer, Object> entry : values.entrySet()) {
preparedStatement.setObject(entry.getKey(), entry.getValue());
errorQuery.value = errorQuery.value.replaceFirst("\\?", entry.getValue() + "");
}
preparedStatement.executeUpdate();
}).ifFail(error -> {
}).onFail(error -> {
SuperiorSkyblockPlugin.log("&cFailed to execute query " + errorQuery);
error.printStackTrace();
});
}));
}
} finally {
values.clear();
Expand Down

0 comments on commit 975681e

Please sign in to comment.