mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 14:38:52 +00:00
Refactor cached data away from cache
This commit is contained in:
14 files changed
+166
-373
No files matched your search
@@ -10,11 +10,13 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -24,84 +26,8 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
public abstract class Database {
|
||||
|
||||
/**
|
||||
* Stores data about {@link User}s
|
||||
* <ol>
|
||||
* <li>uuid - Primary key, unique; the user's minecraft account unique ID</li>
|
||||
* <li>username - The user's username. Checked to be updated on login</li>
|
||||
* <li>last_position - References positions table; where the user was when they last teleported</li>
|
||||
* <li>offline_position - References positions table; the user's last online position. Updated on disconnect</li>
|
||||
* <li>respawn_position - References positions table; where the user last updated their spawn point</li>
|
||||
* <li>home_slots - An integer; the number of home slots this user has consumed (bought)</li>
|
||||
* <li>ignoring_requests - A boolean; whether or not this user is ignoring teleport requests</li>
|
||||
* <li>rtp_cooldown - A datetime timestamp; representing when this user can randomly teleport again</li>
|
||||
* </ol>
|
||||
*/
|
||||
protected final String playerTableName;
|
||||
|
||||
/**
|
||||
* Stores data about {@link Position}s
|
||||
* <ol>
|
||||
* <li>id - Primary key, auto-increment; the id representing the position</li>
|
||||
* <li>x - The double-precision x-coordinate of the position</li>
|
||||
* <li>y - The double-precision y-coordinate of the position</li>
|
||||
* <li>z - The double-precision z-coordinate of the position</li>
|
||||
* <li>yaw - The float-precision yaw facing directional value of the position</li>
|
||||
* <li>pitch - The float-precision pitch facing directional value of the position</li>
|
||||
* <li>world_name - String file name of the world the position is on</li>
|
||||
* <li>world_uuid - String uuid of the world the position is on</li>
|
||||
* <li>server_name - String name of the server the position world is on</li>
|
||||
* </ol>
|
||||
*/
|
||||
protected final String positionsTableName;
|
||||
|
||||
/**
|
||||
* Stores {@link SavedPosition}s, including metadata about them
|
||||
* <ol>
|
||||
* <li>id - Primary key, auto-increment; The id representing the position meta</li>
|
||||
* <li>position_id - References the positions table; the position location data of this saved position</li>
|
||||
* <li>name - The name string of the position represented by this metadata</li>
|
||||
* <li>description - A description string of the position represented by this metadata</li>
|
||||
* <li>timestamp - A datetime timestamp representing when the position this represents was created</li>
|
||||
* </ol>
|
||||
*/
|
||||
protected final String savedPositionsTableName;
|
||||
|
||||
/**
|
||||
* Stores {@link Home} data
|
||||
* <ol>
|
||||
* <li>uuid - Primary key, unique; The unique id of the home</li>
|
||||
* <li>saved_position id - References the saved position table; The id of the home saved position data</li>
|
||||
* <li>owner_uuid - References the players table; the uuid of the person who set the home</li>
|
||||
* <li>is_public - Boolean value; represents if the home is set to public</li>
|
||||
* </ol>
|
||||
*/
|
||||
protected final String homesTableName;
|
||||
|
||||
/**
|
||||
* Stores {@link Warp} data
|
||||
* <ol>
|
||||
* <li>uuid - Primary key, unique; The unique id of the warp</li>
|
||||
* <li>saved_position id - References the saved position table; The id of the warp saved position data</li>
|
||||
* </ol>
|
||||
*/
|
||||
protected final String warpsTableName;
|
||||
|
||||
/**
|
||||
* Stores data about current cross-server teleports being executed by {@link User}s
|
||||
* <ol>
|
||||
* <li>player_uuid - Primary key, unique, references player table; represents the unique id of a teleporting player</li>
|
||||
* <li>destination_id - References positions table; the destination of the teleporting player</li>
|
||||
* </ol>
|
||||
*/
|
||||
protected final String teleportsTableName;
|
||||
|
||||
/**
|
||||
* Instance of the implementing plugin
|
||||
*/
|
||||
protected final HuskHomes plugin;
|
||||
|
||||
|
||||
/**
|
||||
* Loads SQL table creation schema statements from a resource file as a string array
|
||||
*
|
||||
@@ -124,43 +50,27 @@ public abstract class Database {
|
||||
*/
|
||||
protected final String formatStatementTables(@NotNull String sql) {
|
||||
return sql
|
||||
.replaceAll("%positions_table%", positionsTableName)
|
||||
.replaceAll("%players_table%", playerTableName)
|
||||
.replaceAll("%teleports_table%", teleportsTableName)
|
||||
.replaceAll("%saved_positions_table%", savedPositionsTableName)
|
||||
.replaceAll("%homes_table%", homesTableName)
|
||||
.replaceAll("%warps_table%", warpsTableName);
|
||||
.replaceAll("%positions_table%", plugin.getSettings().getTableName(Table.POSITION_DATA))
|
||||
.replaceAll("%players_table%", plugin.getSettings().getTableName(Table.PLAYER_DATA))
|
||||
.replaceAll("%teleports_table%", plugin.getSettings().getTableName(Table.TELEPORT_DATA))
|
||||
.replaceAll("%saved_positions_table%", plugin.getSettings().getTableName(Table.SAVED_POSITION_DATA))
|
||||
.replaceAll("%homes_table%", plugin.getSettings().getTableName(Table.HOME_DATA))
|
||||
.replaceAll("%warps_table%", plugin.getSettings().getTableName(Table.WARP_DATA));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a database instance, pulling table names from the plugin config
|
||||
*
|
||||
* @param implementor the implementing plugin instance
|
||||
* @param plugin the implementing plugin instance
|
||||
*/
|
||||
protected Database(@NotNull HuskHomes implementor) {
|
||||
this.plugin = implementor;
|
||||
this.playerTableName = implementor.getSettings().getTableName(Table.PLAYER_DATA);
|
||||
this.positionsTableName = implementor.getSettings().getTableName(Table.POSITION_DATA);
|
||||
this.savedPositionsTableName = implementor.getSettings().getTableName(Table.SAVED_POSITION_DATA);
|
||||
this.homesTableName = implementor.getSettings().getTableName(Table.HOME_DATA);
|
||||
this.warpsTableName = implementor.getSettings().getTableName(Table.WARP_DATA);
|
||||
this.teleportsTableName = implementor.getSettings().getTableName(Table.TELEPORT_DATA);
|
||||
protected Database(@NotNull HuskHomes plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the database and ensure tables are present; create tables if they do not exist.
|
||||
*
|
||||
* @return {@code true} if done successfully
|
||||
*/
|
||||
public abstract boolean initialize();
|
||||
|
||||
/**
|
||||
* Execute a MySQL script file read as an InputStream
|
||||
*
|
||||
* @param inputStream The input stream to read the script from
|
||||
*/
|
||||
public abstract void runScript(@NotNull InputStream inputStream,
|
||||
@NotNull Map<String, String> replacements);
|
||||
public abstract void initialize() throws IllegalStateException;
|
||||
|
||||
/**
|
||||
* <b>(Internal use only)</b> - Sets a position to the position table in the database
|
||||
|
||||
@@ -11,8 +11,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
import java.util.logging.Level;
|
||||
@@ -23,35 +21,11 @@ import java.util.logging.Level;
|
||||
@SuppressWarnings("DuplicatedCode")
|
||||
public class MySqlDatabase extends Database {
|
||||
|
||||
public String host;
|
||||
public int port;
|
||||
public String database;
|
||||
public String username;
|
||||
public String password;
|
||||
public String connectionParameters;
|
||||
|
||||
public int connectionPoolSize;
|
||||
public int connectionPoolIdle;
|
||||
public long connectionPoolLifetime;
|
||||
public long connectionPoolKeepAlive;
|
||||
public long connectionPoolTimeout;
|
||||
|
||||
private static final String DATA_POOL_NAME = "HuskHomesHikariPool";
|
||||
private HikariDataSource dataSource;
|
||||
|
||||
public MySqlDatabase(@NotNull HuskHomes plugin) {
|
||||
super(plugin);
|
||||
this.host = plugin.getSettings().getMySqlHost();
|
||||
this.port = plugin.getSettings().getMySqlPort();
|
||||
this.database = plugin.getSettings().getMySqlDatabase();
|
||||
this.username = plugin.getSettings().getMySqlUsername();
|
||||
this.password = plugin.getSettings().getMySqlPassword();
|
||||
this.connectionParameters = plugin.getSettings().getMySqlConnectionParameters();
|
||||
this.connectionPoolSize = plugin.getSettings().getMySqlConnectionPoolSize();
|
||||
this.connectionPoolIdle = plugin.getSettings().getMySqlConnectionPoolIdle();
|
||||
this.connectionPoolLifetime = plugin.getSettings().getMySqlConnectionPoolLifetime();
|
||||
this.connectionPoolKeepAlive = plugin.getSettings().getMySqlConnectionPoolKeepAlive();
|
||||
this.connectionPoolTimeout = plugin.getSettings().getMySqlConnectionPoolTimeout();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,75 +39,58 @@ public class MySqlDatabase extends Database {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean initialize() {
|
||||
try {
|
||||
// Create jdbc driver connection url
|
||||
final String jdbcUrl = "jdbc:mysql://" + host + ":" + port + "/" + database + connectionParameters;
|
||||
dataSource = new HikariDataSource();
|
||||
dataSource.setJdbcUrl(jdbcUrl);
|
||||
public void initialize() throws IllegalStateException {
|
||||
// Initialize the Hikari pooled connection
|
||||
dataSource = new HikariDataSource();
|
||||
dataSource.setJdbcUrl("jdbc:mysql://" +
|
||||
plugin.getSettings().getMySqlHost() +
|
||||
":" +
|
||||
plugin.getSettings().getMySqlPort() +
|
||||
"/" +
|
||||
plugin.getSettings().getMySqlDatabase() +
|
||||
plugin.getSettings().getMySqlConnectionParameters());
|
||||
|
||||
// Authenticate
|
||||
dataSource.setUsername(username);
|
||||
dataSource.setPassword(password);
|
||||
// Authenticate with the database
|
||||
dataSource.setUsername(plugin.getSettings().getMySqlUsername());
|
||||
dataSource.setPassword(plugin.getSettings().getMySqlPassword());
|
||||
|
||||
// Set connection pool options
|
||||
dataSource.setMaximumPoolSize(connectionPoolSize);
|
||||
dataSource.setMinimumIdle(connectionPoolIdle);
|
||||
dataSource.setMaxLifetime(connectionPoolLifetime);
|
||||
dataSource.setKeepaliveTime(connectionPoolKeepAlive);
|
||||
dataSource.setConnectionTimeout(connectionPoolTimeout);
|
||||
dataSource.setPoolName(DATA_POOL_NAME);
|
||||
// Set connection pool options
|
||||
dataSource.setMaximumPoolSize(plugin.getSettings().getMySqlConnectionPoolSize());
|
||||
dataSource.setMinimumIdle(plugin.getSettings().getMySqlConnectionPoolIdle());
|
||||
dataSource.setMaxLifetime(plugin.getSettings().getMySqlConnectionPoolLifetime());
|
||||
dataSource.setKeepaliveTime(plugin.getSettings().getMySqlConnectionPoolKeepAlive());
|
||||
dataSource.setConnectionTimeout(plugin.getSettings().getMySqlConnectionPoolTimeout());
|
||||
dataSource.setPoolName(DATA_POOL_NAME);
|
||||
|
||||
// Set additional connection pool properties
|
||||
dataSource.setDataSourceProperties(new Properties() {{
|
||||
put("cachePrepStmts", "true");
|
||||
put("prepStmtCacheSize", "250");
|
||||
put("prepStmtCacheSqlLimit", "2048");
|
||||
put("useServerPrepStmts", "true");
|
||||
put("useLocalSessionState", "true");
|
||||
put("useLocalTransactionState", "true");
|
||||
put("rewriteBatchedStatements", "true");
|
||||
put("cacheResultSetMetadata", "true");
|
||||
put("cacheServerConfiguration", "true");
|
||||
put("elideSetAutoCommits", "true");
|
||||
put("maintainTimeStats", "false");
|
||||
}});
|
||||
// Set additional connection pool properties
|
||||
dataSource.setDataSourceProperties(new Properties() {{
|
||||
put("cachePrepStmts", "true");
|
||||
put("prepStmtCacheSize", "250");
|
||||
put("prepStmtCacheSqlLimit", "2048");
|
||||
put("useServerPrepStmts", "true");
|
||||
put("useLocalSessionState", "true");
|
||||
put("useLocalTransactionState", "true");
|
||||
put("rewriteBatchedStatements", "true");
|
||||
put("cacheResultSetMetadata", "true");
|
||||
put("cacheServerConfiguration", "true");
|
||||
put("elideSetAutoCommits", "true");
|
||||
put("maintainTimeStats", "false");
|
||||
}});
|
||||
|
||||
// Prepare database schema; make tables if they don't exist
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
// Load database schema CREATE statements from schema file
|
||||
final String[] databaseSchema = getSchemaStatements("database/mysql_schema.sql");
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
for (String tableCreationStatement : databaseSchema) {
|
||||
statement.execute(tableCreationStatement);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} catch (SQLException | IOException e) {
|
||||
plugin.log(Level.SEVERE, "An error occurred creating tables on the MySQL database: ", e);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
plugin.log(Level.SEVERE, "An unhandled exception occurred during database setup!", e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runScript(@NotNull InputStream inputStream, @NotNull Map<String, String> replacements) {
|
||||
try (Connection connection = getConnection()) {
|
||||
final String[] scriptString;
|
||||
scriptString = new String[]{new String(inputStream.readAllBytes(), StandardCharsets.UTF_8)};
|
||||
replacements.forEach((key, value) -> scriptString[0] = scriptString[0].replaceAll(key, value));
|
||||
connection.setAutoCommit(false);
|
||||
// Prepare database schema; make tables if they don't exist
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
final String[] databaseSchema = getSchemaStatements("database/mysql_schema.sql");
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
for (String statementString : scriptString[0].split(";")) {
|
||||
statement.addBatch(statementString);
|
||||
for (String tableCreationStatement : databaseSchema) {
|
||||
statement.execute(tableCreationStatement);
|
||||
}
|
||||
statement.executeBatch();
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException("Failed to create database tables. Please ensure you are running MySQL v8.0+ " +
|
||||
"and that your connecting user account has privileges to create tables.", e);
|
||||
}
|
||||
} catch (IOException | SQLException e) {
|
||||
plugin.log(Level.SEVERE, "An error occurred running a script on the MySQL database: ", e);
|
||||
throw new RuntimeException(e);
|
||||
} catch (SQLException | IOException e) {
|
||||
throw new IllegalStateException("Failed to establish a connection to the MySQL database. " +
|
||||
"Please check the supplied database credentials in the config file", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,8 +12,6 @@ import org.sqlite.SQLiteConfig;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
import java.util.logging.Level;
|
||||
@@ -82,50 +80,21 @@ public class SqLiteDatabase extends Database {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean initialize() {
|
||||
public void initialize() throws IllegalStateException {
|
||||
// Set up the connection
|
||||
setConnection();
|
||||
|
||||
// Prepare database schema; make tables if they don't exist
|
||||
try {
|
||||
// Set up the connection
|
||||
setConnection();
|
||||
|
||||
// Prepare database schema; make tables if they don't exist
|
||||
try {
|
||||
// Load database schema CREATE statements from schema file
|
||||
final String[] databaseSchema = getSchemaStatements("database/sqlite_schema.sql");
|
||||
try (Statement statement = getConnection().createStatement()) {
|
||||
for (String tableCreationStatement : databaseSchema) {
|
||||
statement.execute(tableCreationStatement);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} catch (SQLException | IOException e) {
|
||||
plugin.log(Level.SEVERE, "An error occurred creating tables on the SQLite database: ", e);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
plugin.log(Level.SEVERE, "An unhandled exception occurred during database setup!", e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runScript(@NotNull InputStream inputStream, @NotNull Map<String, String> replacements) {
|
||||
try {
|
||||
final String[] scriptString;
|
||||
scriptString = new String[]{new String(inputStream.readAllBytes(), StandardCharsets.UTF_8)};
|
||||
replacements.forEach((key, value) -> scriptString[0] = scriptString[0].replaceAll(key, value));
|
||||
final boolean autoCommit = getConnection().getAutoCommit();
|
||||
|
||||
// Execute batched SQLite script
|
||||
getConnection().setAutoCommit(false);
|
||||
// Load database schema CREATE statements from schema file
|
||||
final String[] databaseSchema = getSchemaStatements("database/sqlite_schema.sql");
|
||||
try (Statement statement = getConnection().createStatement()) {
|
||||
for (String statementString : scriptString[0].split(";")) {
|
||||
statement.addBatch(statementString);
|
||||
for (String tableCreationStatement : databaseSchema) {
|
||||
statement.execute(tableCreationStatement);
|
||||
}
|
||||
statement.executeBatch();
|
||||
}
|
||||
getConnection().setAutoCommit(autoCommit);
|
||||
} catch (IOException | SQLException e) {
|
||||
plugin.log(Level.SEVERE, "An exception occurred running script on the SQLite database", e);
|
||||
throw new RuntimeException(e);
|
||||
} catch (SQLException | IOException e) {
|
||||
throw new IllegalStateException("Failed to initialize the SQLite database", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user