Add H2 database support, use indexes to optimize queries, add maria-specific schema, bump adventure-fabric (#465)

* Add support for H2, bundle Maria driver, bump fabric adventure

* Bundle and download H2 drivers

* Fixup H2 schema, add indexes to DBs, add maria-specific schema

* docs: Add database type documentation

* Check plugin isn't disabled before task schedule, close #463

* Bump Annotaml to 2.0.4

* docs: Mention Maria in README and Setup
This commit is contained in:
William authored and GitHub committed 2023-07-18 15:03:31 +01:00
1 parent 8d2424a7ef
commit beed438c16
28 files changed
+1443 -50

No files matched your search

+6 -4
View File
@@ -11,7 +11,7 @@ dependencies {
api 'org.apache.commons:commons-text:1.10.0'
api 'de.themoep:minedown-adventure:1.7.2-SNAPSHOT'
api 'com.google.code.gson:gson:2.10.1'
api 'net.william278:Annotaml:2.0.1'
api 'net.william278:annotaml:2.0.4'
api 'net.william278:PagineDown:1.1'
api 'net.william278:DesertWell:2.0.4'
api 'dev.dejvokep:boosted-yaml:1.3.1'
@@ -20,9 +20,11 @@ dependencies {
exclude module: 'slf4j-api'
}
compileOnly 'redis.clients:jedis:' + jedis_version
compileOnly 'org.xerial:sqlite-jdbc:' + sqlite_driver_version
compileOnly 'com.mysql:mysql-connector-j:' + mysql_driver_version
compileOnly "redis.clients:jedis:$jedis_version"
compileOnly "org.xerial:sqlite-jdbc:$sqlite_driver_version"
compileOnly "com.mysql:mysql-connector-j:$mysql_driver_version"
compileOnly "org.mariadb.jdbc:mariadb-java-client:$mariadb_driver_version"
compileOnly "com.h2database:h2:$h2_driver_version"
compileOnly 'org.jetbrains:annotations:24.0.1'
compileOnly 'com.google.guava:guava:32.1.1-jre'
@@ -55,11 +55,11 @@ public class Settings {
private boolean checkForUpdates = true;
// Database settings
@YamlComment("Type of database to use (SQLITE, MYSQL or MARIADB)")
@YamlComment("Type of database to use (SQLITE, H2, MYSQL or MARIADB)")
@YamlKey("database.type")
private Database.Type databaseType = Database.Type.SQLITE;
@YamlComment("Specify credentials here if you are using MYSQL as your database type")
@YamlComment("Specify credentials here if you are using MYSQL or MARIADB as your database type")
@YamlKey("database.mysql.credentials.host")
private String mySqlHost = "localhost";
@@ -81,7 +81,7 @@ public class Settings {
+ "&useUnicode=true"
+ "&characterEncoding=UTF-8";
@YamlComment("MYSQL database Hikari connection pool properties. "
@YamlComment("MYSQL / MARIADB database Hikari connection pool properties. "
+ "Don't modify this unless you know what you're doing!")
@YamlKey("database.mysql.connection_pool.size")
private int mySqlConnectionPoolSize = 12;
@@ -440,7 +440,8 @@ public abstract class Database {
public enum Type {
MYSQL("MySQL"),
MARIADB("MariaDB"),
SQLITE("SQLite");
SQLITE("SQLite"),
H2("H2");
private final String displayName;
File diff suppressed because it is too large. Load diff
@@ -38,16 +38,18 @@ import java.util.*;
import java.util.logging.Level;
/**
* A MySQL implementation of the plugin {@link Database}.
* A MySQL / MariaDB implementation of the plugin {@link Database}.
*/
@SuppressWarnings("DuplicatedCode")
public class MySqlDatabase extends Database {
private static final String DATA_POOL_NAME = "HuskHomesHikariPool";
private final String flavor;
private HikariDataSource dataSource;
public MySqlDatabase(@NotNull HuskHomes plugin) {
super(plugin);
this.flavor = plugin.getSettings().getDatabaseType() == Type.MARIADB ? "mariadb" : "mysql";
}
/**
@@ -64,15 +66,13 @@ public class MySqlDatabase extends Database {
public void initialize() throws IllegalStateException {
// Initialize the Hikari pooled connection
dataSource = new HikariDataSource();
dataSource.setJdbcUrl("jdbc:" + (plugin.getSettings().getDatabaseType() == Type.MARIADB ? "mariadb" : "mysql")
+ "://"
+ plugin.getSettings().getMySqlHost()
+ ":"
+ plugin.getSettings().getMySqlPort()
+ "/"
+ plugin.getSettings().getMySqlDatabase()
+ plugin.getSettings().getMySqlConnectionParameters()
);
dataSource.setJdbcUrl(String.format("jdbc:%s://%s:%s/%s%s",
flavor,
plugin.getSettings().getMySqlHost(),
plugin.getSettings().getMySqlPort(),
plugin.getSettings().getMySqlDatabase(),
plugin.getSettings().getMySqlConnectionParameters()
));
// Authenticate with the database
dataSource.setUsername(plugin.getSettings().getMySqlUsername());
@@ -108,7 +108,7 @@ public class MySqlDatabase extends Database {
// Prepare database schema; make tables if they don't exist
try (Connection connection = dataSource.getConnection()) {
final String[] databaseSchema = getSchemaStatements("database/mysql_schema.sql");
final String[] databaseSchema = getSchemaStatements(String.format("database/%s_schema.sql", flavor));
try (Statement statement = connection.createStatement()) {
for (String tableCreationStatement : databaseSchema) {
statement.execute(tableCreationStatement);
@@ -81,7 +81,7 @@ public class SqLiteDatabase extends Database {
plugin.log(Level.INFO, "Created the SQLite database file");
}
// Specify use of the JDBC SQLite driver
// Specify use of the JDBC SQLite driver for legacy compatibility
Class.forName("org.sqlite.JDBC");
// Set SQLite database properties
@@ -92,10 +92,11 @@ public class SqLiteDatabase extends Database {
// Establish the connection
connection = DriverManager.getConnection(
"jdbc:sqlite:" + databaseFile.getAbsolutePath(), config.toProperties()
String.format("jdbc:sqlite:%s", databaseFile.getAbsolutePath()),
config.toProperties()
);
} catch (IOException e) {
plugin.log(Level.SEVERE, "An exception occurred creating the database file", e);
plugin.log(Level.SEVERE, "An exception occurred creating the SQLite database file", e);
} catch (SQLException e) {
plugin.log(Level.SEVERE, "An SQL exception occurred initializing the SQLite database", e);
} catch (ClassNotFoundException e) {
@@ -0,0 +1,94 @@
/* Create the positions table if it does not exist */
CREATE TABLE IF NOT EXISTS `%positions_table%`
(
`id` INT NOT NULL AUTO_INCREMENT,
`x` DOUBLE NOT NULL,
`y` DOUBLE NOT NULL,
`z` DOUBLE NOT NULL,
`yaw` REAL NOT NULL,
`pitch` REAL NOT NULL,
`world_name` VARCHAR(255) NOT NULL,
`world_uuid` UUID NOT NULL,
`server_name` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`)
);
/* Create the players table if it does not exist */
CREATE TABLE IF NOT EXISTS `%players_table%`
(
`uuid` UUID NOT NULL PRIMARY KEY,
`username` VARCHAR(16) NOT NULL,
`last_position` INT NULL,
`offline_position` INT NULL,
`respawn_position` INT NULL,
`home_slots` INT NOT NULL DEFAULT 0,
`ignoring_requests` BOOLEAN NOT NULL DEFAULT FALSE,
FOREIGN KEY (`last_position`) REFERENCES `%positions_table%` (`id`) ON DELETE SET NULL ON UPDATE NO ACTION,
FOREIGN KEY (`offline_position`) REFERENCES `%positions_table%` (`id`) ON DELETE SET NULL ON UPDATE NO ACTION,
FOREIGN KEY (`respawn_position`) REFERENCES `%positions_table%` (`id`) ON DELETE SET NULL ON UPDATE NO ACTION
);
CREATE INDEX IF NOT EXISTS `%players_table%_username` ON `%players_table%` (`username`);
/* Create the cooldowns table if it does not exist */
CREATE TABLE IF NOT EXISTS `%cooldowns_table%`
(
`id` INT NOT NULL AUTO_INCREMENT,
`player_uuid` UUID NOT NULL,
`type` VARCHAR(255) NOT NULL,
`start_timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`end_timestamp` TIMESTAMP NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`player_uuid`) REFERENCES `%players_table%` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE INDEX IF NOT EXISTS `%cooldowns_table%_player_uuid` ON `%cooldowns_table%` (`player_uuid`);
/* Create the current cross-server teleports table if it does not exist */
CREATE TABLE IF NOT EXISTS `%teleports_table%`
(
`player_uuid` UUID NOT NULL PRIMARY KEY,
`destination_id` INT NOT NULL,
`type` INT NOT NULL DEFAULT 0,
FOREIGN KEY (`player_uuid`) REFERENCES `%players_table%` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (`destination_id`) REFERENCES `%positions_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
);
/* Create the saved positions table if it does not exist */
CREATE TABLE IF NOT EXISTS `%saved_positions_table%`
(
`id` INT NOT NULL AUTO_INCREMENT,
`position_id` INT NOT NULL,
`name` VARCHAR(64) NOT NULL,
`description` VARCHAR(255) NOT NULL,
`tags` CLOB NULL,
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
FOREIGN KEY (`position_id`) REFERENCES `%positions_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
);
CREATE INDEX IF NOT EXISTS `%saved_positions_table%_name` ON `%saved_positions_table%` (`name`);
/* Create the homes table if it does not exist */
CREATE TABLE IF NOT EXISTS `%homes_table%`
(
`uuid` UUID NOT NULL PRIMARY KEY,
`saved_position_id` INT NOT NULL,
`owner_uuid` UUID NOT NULL,
`public` BOOLEAN NOT NULL DEFAULT FALSE,
FOREIGN KEY (`owner_uuid`) REFERENCES `%players_table%` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (`saved_position_id`) REFERENCES `%saved_positions_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
);
CREATE INDEX IF NOT EXISTS `%homes_table%_owner_uuid` ON `%homes_table%` (`owner_uuid`);
/* Create the warps table if it does not exist */
CREATE TABLE IF NOT EXISTS `%warps_table%`
(
`uuid` UUID NOT NULL PRIMARY KEY,
`saved_position_id` INT NOT NULL,
FOREIGN KEY (`saved_position_id`) REFERENCES `%saved_positions_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
);
@@ -0,0 +1,118 @@
-- Set the storage engine
SET DEFAULT_STORAGE_ENGINE = InnoDB;
-- Enable foreign key constraints
SET FOREIGN_KEY_CHECKS = 1;
-- Create the positions table if it does not exist
CREATE TABLE IF NOT EXISTS `%positions_table%`
(
`id` INTEGER NOT NULL AUTO_INCREMENT,
`x` DOUBLE NOT NULL,
`y` DOUBLE NOT NULL,
`z` DOUBLE NOT NULL,
`yaw` FLOAT NOT NULL,
`pitch` FLOAT NOT NULL,
`world_name` VARCHAR(255) NOT NULL,
`world_uuid` CHAR(36) NOT NULL,
`server_name` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
-- Create the players table if it does not exist
CREATE TABLE IF NOT EXISTS `%players_table%`
(
`uuid` CHAR(36) NOT NULL,
`username` VARCHAR(16) NOT NULL,
`last_position` INTEGER DEFAULT NULL,
`offline_position` INTEGER DEFAULT NULL,
`respawn_position` INTEGER DEFAULT NULL,
`home_slots` INTEGER NOT NULL DEFAULT 0,
`ignoring_requests` BOOLEAN NOT NULL DEFAULT FALSE,
PRIMARY KEY (`uuid`),
FOREIGN KEY (`last_position`) REFERENCES `%positions_table%` (`id`) ON DELETE SET NULL ON UPDATE NO ACTION,
FOREIGN KEY (`offline_position`) REFERENCES `%positions_table%` (`id`) ON DELETE SET NULL ON UPDATE NO ACTION,
FOREIGN KEY (`respawn_position`) REFERENCES `%positions_table%` (`id`) ON DELETE SET NULL ON UPDATE NO ACTION
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
CREATE INDEX IF NOT EXISTS `%players_table%_username` ON `%players_table%` (`username`);
-- Create the cooldowns table if it does not exist
CREATE TABLE IF NOT EXISTS `%cooldowns_table%`
(
`id` INTEGER NOT NULL AUTO_INCREMENT,
`player_uuid` CHAR(36) NOT NULL,
`type` VARCHAR(255) NOT NULL,
`start_timestamp` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`end_timestamp` DATETIME NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`player_uuid`) REFERENCES `%players_table%` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
CREATE INDEX IF NOT EXISTS `%cooldowns_table%_player_uuid` ON `%cooldowns_table%` (`player_uuid`);
-- Create the current cross-server teleports table if it does not exist
CREATE TABLE IF NOT EXISTS `%teleports_table%`
(
`player_uuid` CHAR(36) NOT NULL,
`destination_id` INTEGER NOT NULL,
`type` INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (`player_uuid`),
FOREIGN KEY (`player_uuid`) REFERENCES `%players_table%` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (`destination_id`) REFERENCES `%positions_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
-- Create the saved positions table if it does not exist
CREATE TABLE IF NOT EXISTS `%saved_positions_table%`
(
`id` INTEGER NOT NULL AUTO_INCREMENT,
`position_id` INTEGER NOT NULL,
`name` VARCHAR(64) NOT NULL,
`description` VARCHAR(255) NOT NULL,
`tags` MEDIUMTEXT DEFAULT NULL,
`timestamp` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
FOREIGN KEY (`position_id`) REFERENCES `%positions_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
CREATE INDEX IF NOT EXISTS `%saved_positions_table%_name` ON `%saved_positions_table%` (`name`);
-- Create the homes table if it does not exist
CREATE TABLE IF NOT EXISTS `%homes_table%`
(
`uuid` CHAR(36) NOT NULL,
`saved_position_id` INTEGER NOT NULL,
`owner_uuid` CHAR(36) NOT NULL,
`public` BOOLEAN NOT NULL DEFAULT FALSE,
PRIMARY KEY (`uuid`),
FOREIGN KEY (`owner_uuid`) REFERENCES `%players_table%` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (`saved_position_id`) REFERENCES `%saved_positions_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
CREATE INDEX IF NOT EXISTS `%homes_table%_owner_uuid` ON `%homes_table%` (`owner_uuid`);
-- Create the warps table if it does not exist
CREATE TABLE IF NOT EXISTS `%warps_table%`
(
`uuid` CHAR(36) NOT NULL,
`saved_position_id` INTEGER NOT NULL,
PRIMARY KEY (`uuid`),
FOREIGN KEY (`saved_position_id`) REFERENCES `%saved_positions_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
@@ -30,6 +30,7 @@ CREATE TABLE IF NOT EXISTS `%players_table%`
FOREIGN KEY (`offline_position`) REFERENCES `%positions_table%` (`id`) ON DELETE SET NULL ON UPDATE NO ACTION,
FOREIGN KEY (`respawn_position`) REFERENCES `%positions_table%` (`id`) ON DELETE SET NULL ON UPDATE NO ACTION
);
CREATE INDEX IF NOT EXISTS `%players_table%_username` ON `%players_table%` (`username`);
/* Create the cooldowns table if it does not exist */
CREATE TABLE IF NOT EXISTS `%cooldowns_table%`
@@ -43,6 +44,7 @@ CREATE TABLE IF NOT EXISTS `%cooldowns_table%`
PRIMARY KEY (`id`),
FOREIGN KEY (`player_uuid`) REFERENCES `%players_table%` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE INDEX IF NOT EXISTS `%cooldowns_table%_player_uuid` ON `%cooldowns_table%` (`player_uuid`);
/* Create the current cross-server teleports table if it does not exist */
CREATE TABLE IF NOT EXISTS `%teleports_table%`
@@ -69,6 +71,7 @@ CREATE TABLE IF NOT EXISTS `%saved_positions_table%`
PRIMARY KEY (`id`),
FOREIGN KEY (`position_id`) REFERENCES `%positions_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
);
CREATE INDEX IF NOT EXISTS `%saved_positions_table%_name` ON `%saved_positions_table%` (`name`);
/* Create the homes table if it does not exist */
CREATE TABLE IF NOT EXISTS `%homes_table%`
@@ -82,6 +85,7 @@ CREATE TABLE IF NOT EXISTS `%homes_table%`
FOREIGN KEY (`owner_uuid`) REFERENCES `%players_table%` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (`saved_position_id`) REFERENCES `%saved_positions_table%` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
);
CREATE INDEX IF NOT EXISTS `%homes_table%_owner_uuid` ON `%homes_table%` (`owner_uuid`);
/* Create the warps table if it does not exist */
CREATE TABLE IF NOT EXISTS `%warps_table%`