From 5567b1dad8131c985dfd0f0c2cb86ab4d70838c9 Mon Sep 17 00:00:00 2001 From: Luck Date: Wed, 5 Apr 2017 21:23:18 +0100 Subject: [PATCH] Use the mariadb driver by default --- .../common/dependencies/Dependency.java | 1 + .../dependencies/DependencyManager.java | 3 +- .../common/storage/StorageFactory.java | 5 +- .../luckperms/common/storage/StorageType.java | 21 +++++---- .../common/storage/backing/YAMLBacking.java | 5 +- .../backing/sqlprovider/MySQLProvider.java | 47 ++++++++++++------- .../common/utils/HikariSupplier.java | 2 +- 7 files changed, 55 insertions(+), 29 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/dependencies/Dependency.java b/common/src/main/java/me/lucko/luckperms/common/dependencies/Dependency.java index 9a9a87ba3..d726edc64 100644 --- a/common/src/main/java/me/lucko/luckperms/common/dependencies/Dependency.java +++ b/common/src/main/java/me/lucko/luckperms/common/dependencies/Dependency.java @@ -30,6 +30,7 @@ import lombok.Getter; public enum Dependency { CAFFEINE("https://repo1.maven.org/maven2/com/github/ben-manes/caffeine/caffeine/2.4.0/caffeine-2.4.0.jar", "2.4.0"), + MARIADB_DRIVER("https://repo1.maven.org/maven2/org/mariadb/jdbc/mariadb-java-client/1.5.9/mariadb-java-client-1.5.9.jar", "1.5.9"), MYSQL_DRIVER("https://repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6.jar", "5.1.6"), POSTGRESQL_DRIVER("https://repo1.maven.org/maven2/org/postgresql/postgresql/9.4.1212/postgresql-9.4.1212.jar", "9.4.1212"), H2_DRIVER("https://repo1.maven.org/maven2/com/h2database/h2/1.4.193/h2-1.4.193.jar", "1.4.193"), diff --git a/common/src/main/java/me/lucko/luckperms/common/dependencies/DependencyManager.java b/common/src/main/java/me/lucko/luckperms/common/dependencies/DependencyManager.java index e6fa6add2..974ebbd95 100644 --- a/common/src/main/java/me/lucko/luckperms/common/dependencies/DependencyManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/dependencies/DependencyManager.java @@ -60,7 +60,8 @@ public class DependencyManager { .put(StorageType.JSON, ImmutableList.of()) .put(StorageType.YAML, ImmutableList.of()) .put(StorageType.MONGODB, ImmutableList.of(Dependency.MONGODB_DRIVER)) - .put(StorageType.MYSQL, ImmutableList.of(Dependency.MYSQL_DRIVER, Dependency.SLF4J_API, Dependency.SLF4J_SIMPLE, Dependency.HIKARI)) + .put(StorageType.MYSQL, ImmutableList.of(Dependency.MARIADB_DRIVER, Dependency.SLF4J_API, Dependency.SLF4J_SIMPLE, Dependency.HIKARI)) + .put(StorageType.MYSQL_LEGACY, ImmutableList.of(Dependency.MYSQL_DRIVER, Dependency.SLF4J_API, Dependency.SLF4J_SIMPLE, Dependency.HIKARI)) .put(StorageType.POSTGRESQL, ImmutableList.of(Dependency.POSTGRESQL_DRIVER, Dependency.SLF4J_API, Dependency.SLF4J_SIMPLE, Dependency.HIKARI)) .put(StorageType.SQLITE, ImmutableList.of(Dependency.SQLITE_DRIVER)) .put(StorageType.H2, ImmutableList.of(Dependency.H2_DRIVER)) diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/StorageFactory.java b/common/src/main/java/me/lucko/luckperms/common/storage/StorageFactory.java index 4a6b78794..cc9bbf70a 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/StorageFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/StorageFactory.java @@ -107,6 +107,7 @@ public class StorageFactory { type = defaultMethod; } + plugin.getLog().info("Using " + type.getName() + " storage."); storage = makeInstance(type, plugin); } @@ -122,7 +123,9 @@ public class StorageFactory { private static AbstractBacking makeBacking(StorageType method, LuckPermsPlugin plugin) { switch (method) { case MYSQL: - return new SQLBacking(plugin, new MySQLProvider(plugin.getConfiguration().get(ConfigKeys.DATABASE_VALUES)), plugin.getConfiguration().get(ConfigKeys.SQL_TABLE_PREFIX)); + return new SQLBacking(plugin, new MySQLProvider("MySQL", "org.mariadb.jdbc.MySQLDataSource", plugin.getConfiguration().get(ConfigKeys.DATABASE_VALUES)), plugin.getConfiguration().get(ConfigKeys.SQL_TABLE_PREFIX)); + case MYSQL_LEGACY: + return new SQLBacking(plugin, new MySQLProvider("MySQL-Legacy", "com.mysql.jdbc.jdbc2.optional.MysqlDataSource", plugin.getConfiguration().get(ConfigKeys.DATABASE_VALUES)), plugin.getConfiguration().get(ConfigKeys.SQL_TABLE_PREFIX)); case SQLITE: return new SQLBacking(plugin, new SQLiteProvider(new File(plugin.getDataDirectory(), "luckperms-sqlite.db")), plugin.getConfiguration().get(ConfigKeys.SQL_TABLE_PREFIX)); case H2: diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/StorageType.java b/common/src/main/java/me/lucko/luckperms/common/storage/StorageType.java index 4ca8b0831..057c38e8f 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/StorageType.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/StorageType.java @@ -30,18 +30,23 @@ import java.util.List; public enum StorageType { - JSON("json", "flatfile"), - YAML("yaml", "yml"), - MONGODB("mongodb"), - MYSQL("mysql"), - POSTGRESQL("postgresql"), - SQLITE("sqlite"), - H2("h2"); + JSON("JSON", "json", "flatfile"), + YAML("YAML", "yaml", "yml"), + MONGODB("MongoDB", "mongodb"), + MYSQL("MySQL", "mysql"), + MYSQL_LEGACY("MySQL-Legacy", "mysql-legacy"), + POSTGRESQL("PostgreSQL", "postgresql"), + SQLITE("SQLite", "sqlite"), + H2("H2", "h2"); + + @Getter + private final String name; @Getter private final List identifiers; - StorageType(String... identifiers) { + StorageType(String name, String... identifiers) { + this.name = name; this.identifiers = ImmutableList.copyOf(identifiers); } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/YAMLBacking.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/YAMLBacking.java index 1d3529a0f..2f898d79f 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/YAMLBacking.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/YAMLBacking.java @@ -28,6 +28,7 @@ import com.google.common.collect.Iterables; import me.lucko.luckperms.api.HeldPermission; import me.lucko.luckperms.api.Node; +import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.common.core.NodeModel; import me.lucko.luckperms.common.core.UserIdentifier; import me.lucko.luckperms.common.core.model.Group; @@ -498,7 +499,7 @@ public class YAMLBacking extends FlatfileBacking { context = map.build(); } - nodes.add(NodeModel.of(permission, value, server, world, expiry, context)); + nodes.add(NodeModel.of(permission, value, server, world, expiry, ImmutableContextSet.fromMultimap(context))); } } @@ -526,7 +527,7 @@ public class YAMLBacking extends FlatfileBacking { if (!node.getContexts().isEmpty()) { Map context = new HashMap<>(); - Map> map = node.getContexts().asMap(); + Map> map = node.getContexts().toMultimap().asMap(); for (Map.Entry> e : map.entrySet()) { List vals = new ArrayList<>(e.getValue()); diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/sqlprovider/MySQLProvider.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/sqlprovider/MySQLProvider.java index 091328f5c..63a5cf3d6 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/sqlprovider/MySQLProvider.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/sqlprovider/MySQLProvider.java @@ -34,11 +34,13 @@ import java.util.concurrent.TimeUnit; public class MySQLProvider extends SQLProvider { private final DatastoreConfiguration configuration; + private final String driverClass; private HikariDataSource hikari; - public MySQLProvider(DatastoreConfiguration configuration) { - super("MySQL"); + public MySQLProvider(String name, String driverClass, DatastoreConfiguration configuration) { + super(name); this.configuration = configuration; + this.driverClass = driverClass; } @Override @@ -57,23 +59,36 @@ public class MySQLProvider extends SQLProvider { config.setMaximumPoolSize(configuration.getPoolSize()); config.setPoolName("luckperms"); - config.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource"); + config.setDataSourceClassName(driverClass); config.addDataSourceProperty("serverName", address); config.addDataSourceProperty("port", port); config.addDataSourceProperty("databaseName", database); - config.addDataSourceProperty("user", username); - config.addDataSourceProperty("password", password); - config.addDataSourceProperty("cachePrepStmts", "true"); - config.addDataSourceProperty("prepStmtCacheSize", "250"); - config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); - config.addDataSourceProperty("useServerPrepStmts", "true"); - config.addDataSourceProperty("cacheCallableStmts", "true"); - config.addDataSourceProperty("alwaysSendSetIsolation", "false"); - config.addDataSourceProperty("cacheServerConfiguration", "true"); - config.addDataSourceProperty("elideSetAutoCommits", "true"); - config.addDataSourceProperty("useLocalSessionState", "true"); - config.addDataSourceProperty("characterEncoding", "utf8"); - config.addDataSourceProperty("useUnicode", "true"); + config.setUsername(username); + config.setPassword(password); + + if (getName().toLowerCase().endsWith("legacy")) { + + // doesn't exist on the MariaDB driver + config.addDataSourceProperty("cachePrepStmts", "true"); + config.addDataSourceProperty("alwaysSendSetIsolation", "false"); + config.addDataSourceProperty("cacheServerConfiguration", "true"); + config.addDataSourceProperty("elideSetAutoCommits", "true"); + config.addDataSourceProperty("useLocalSessionState", "true"); + + // already set as default on mariadb + config.addDataSourceProperty("useServerPrepStmts", "true"); + config.addDataSourceProperty("prepStmtCacheSize", "250"); + config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); + config.addDataSourceProperty("cacheCallableStmts", "true"); + + // make sure unicode characters can be used. + config.addDataSourceProperty("characterEncoding", "utf8"); + config.addDataSourceProperty("useUnicode", "true"); + } else { + // hack for mariadb. this will call #setProperties on the datasource, which will append these options + // onto the connections. + config.addDataSourceProperty("properties", "useUnicode=true;characterEncoding=utf8"); + } // We will wait for 15 seconds to get a connection from the pool. // Default is 30, but it shouldn't be taking that long. diff --git a/common/src/main/java/me/lucko/luckperms/common/utils/HikariSupplier.java b/common/src/main/java/me/lucko/luckperms/common/utils/HikariSupplier.java index c0dca01ef..ea73e00a9 100644 --- a/common/src/main/java/me/lucko/luckperms/common/utils/HikariSupplier.java +++ b/common/src/main/java/me/lucko/luckperms/common/utils/HikariSupplier.java @@ -43,7 +43,7 @@ public class HikariSupplier implements AutoCloseable { hikari = new HikariDataSource(); hikari.setPoolName(poolName); hikari.setMaximumPoolSize(2); - hikari.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource"); + hikari.setDataSourceClassName("org.mariadb.jdbc.Driver"); hikari.addDataSourceProperty("serverName", address.split(":")[0]); hikari.addDataSourceProperty("port", address.split(":")[1]); hikari.addDataSourceProperty("databaseName", database);