More storage refactoring

This commit is contained in:
Luck 2017-10-22 20:09:53 +01:00
parent ca8cc91f37
commit a3a7198777
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
30 changed files with 259 additions and 222 deletions

View File

@ -71,7 +71,7 @@ import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.storage.Storage;
import me.lucko.luckperms.common.storage.StorageFactory;
import me.lucko.luckperms.common.storage.StorageType;
import me.lucko.luckperms.common.storage.backing.file.FileWatcher;
import me.lucko.luckperms.common.storage.dao.file.FileWatcher;
import me.lucko.luckperms.common.tasks.CacheHousekeepingTask;
import me.lucko.luckperms.common.tasks.ExpireTemporaryTask;
import me.lucko.luckperms.common.tasks.UpdateTask;

View File

@ -67,7 +67,7 @@ import me.lucko.luckperms.common.plugin.LuckPermsScheduler;
import me.lucko.luckperms.common.storage.Storage;
import me.lucko.luckperms.common.storage.StorageFactory;
import me.lucko.luckperms.common.storage.StorageType;
import me.lucko.luckperms.common.storage.backing.file.FileWatcher;
import me.lucko.luckperms.common.storage.dao.file.FileWatcher;
import me.lucko.luckperms.common.tasks.CacheHousekeepingTask;
import me.lucko.luckperms.common.tasks.ExpireTemporaryTask;
import me.lucko.luckperms.common.tasks.UpdateTask;

View File

@ -47,7 +47,7 @@ import me.lucko.luckperms.common.managers.UserManager;
import me.lucko.luckperms.common.messaging.InternalMessagingService;
import me.lucko.luckperms.common.model.User;
import me.lucko.luckperms.common.storage.Storage;
import me.lucko.luckperms.common.storage.backing.file.FileWatcher;
import me.lucko.luckperms.common.storage.dao.file.FileWatcher;
import me.lucko.luckperms.common.treeview.PermissionVault;
import me.lucko.luckperms.common.utils.UuidCache;
import me.lucko.luckperms.common.verbose.VerboseHandler;

View File

@ -41,7 +41,7 @@ import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.common.model.Track;
import me.lucko.luckperms.common.model.User;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.storage.backing.AbstractDao;
import me.lucko.luckperms.common.storage.dao.AbstractDao;
import me.lucko.luckperms.common.storage.wrappings.BufferedOutputStorage;
import me.lucko.luckperms.common.storage.wrappings.PhasedStorage;

View File

@ -31,16 +31,18 @@ import com.google.common.collect.ImmutableSet;
import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.storage.backing.AbstractDao;
import me.lucko.luckperms.common.storage.backing.file.HoconDao;
import me.lucko.luckperms.common.storage.backing.file.JsonDao;
import me.lucko.luckperms.common.storage.backing.file.YamlDao;
import me.lucko.luckperms.common.storage.backing.mongodb.MongoDao;
import me.lucko.luckperms.common.storage.backing.sql.SqlDao;
import me.lucko.luckperms.common.storage.backing.sql.provider.file.H2ConnectionFactory;
import me.lucko.luckperms.common.storage.backing.sql.provider.file.SQLiteConnectionFactory;
import me.lucko.luckperms.common.storage.backing.sql.provider.remote.MySqlConnectionFactory;
import me.lucko.luckperms.common.storage.backing.sql.provider.remote.PostgreConnectionFactory;
import me.lucko.luckperms.common.storage.dao.AbstractDao;
import me.lucko.luckperms.common.storage.dao.SplitStorageDao;
import me.lucko.luckperms.common.storage.dao.file.HoconDao;
import me.lucko.luckperms.common.storage.dao.file.JsonDao;
import me.lucko.luckperms.common.storage.dao.file.YamlDao;
import me.lucko.luckperms.common.storage.dao.mongodb.MongoDao;
import me.lucko.luckperms.common.storage.dao.sql.SqlDao;
import me.lucko.luckperms.common.storage.dao.sql.connection.file.H2ConnectionFactory;
import me.lucko.luckperms.common.storage.dao.sql.connection.file.SQLiteConnectionFactory;
import me.lucko.luckperms.common.storage.dao.sql.connection.hikari.MariaDbConnectionFactory;
import me.lucko.luckperms.common.storage.dao.sql.connection.hikari.MySqlConnectionFactory;
import me.lucko.luckperms.common.storage.dao.sql.connection.hikari.PostgreConnectionFactory;
import me.lucko.luckperms.common.utils.ImmutableCollectors;
import java.io.File;
@ -121,16 +123,12 @@ public class StorageFactory {
private static AbstractDao makeDao(StorageType method, LuckPermsPlugin plugin) {
switch (method) {
case MARIADB:
return new SqlDao(plugin, new MySqlConnectionFactory(
"MariaDB",
"org.mariadb.jdbc.MySQLDataSource",
return new SqlDao(plugin, new MariaDbConnectionFactory(
plugin.getConfiguration().get(ConfigKeys.DATABASE_VALUES)),
plugin.getConfiguration().get(ConfigKeys.SQL_TABLE_PREFIX)
);
case MYSQL:
return new SqlDao(plugin, new MySqlConnectionFactory(
"MySQL",
"com.mysql.jdbc.jdbc2.optional.MysqlDataSource",
plugin.getConfiguration().get(ConfigKeys.DATABASE_VALUES)),
plugin.getConfiguration().get(ConfigKeys.SQL_TABLE_PREFIX)
);

View File

@ -1,153 +0,0 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.storage.backing.sql.provider.remote;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import me.lucko.luckperms.common.storage.DatastoreConfiguration;
import me.lucko.luckperms.common.storage.backing.sql.provider.AbstractConnectionFactory;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class MySqlConnectionFactory extends AbstractConnectionFactory {
private final DatastoreConfiguration configuration;
private final String driverClass;
private HikariDataSource hikari;
public MySqlConnectionFactory(String name, String driverClass, DatastoreConfiguration configuration) {
super(name);
this.configuration = configuration;
this.driverClass = driverClass;
}
@Override
public void init() {
HikariConfig config = new HikariConfig();
String address = configuration.getAddress();
String[] addressSplit = address.split(":");
address = addressSplit[0];
String port = addressSplit.length > 1 ? addressSplit[1] : "3306";
String database = configuration.getDatabase();
String username = configuration.getUsername();
String password = configuration.getPassword();
config.setMaximumPoolSize(configuration.getPoolSize());
config.setPoolName("luckperms");
config.setDataSourceClassName(driverClass);
config.addDataSourceProperty("serverName", address);
config.addDataSourceProperty("port", port);
config.addDataSourceProperty("databaseName", database);
config.setUsername(username);
config.setPassword(password);
if (!getName().toLowerCase().equals("mariadb")) {
// 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.
config.setConnectionTimeout(TimeUnit.SECONDS.toMillis(15)); // 15000
// If a connection is not returned within 10 seconds, it's probably safe to assume it's been leaked.
config.setLeakDetectionThreshold(TimeUnit.SECONDS.toMillis(10)); // 10000
// The drivers are really old in some of the older Spigot binaries, so Connection#isValid doesn't work.
config.setConnectionTestQuery("/* LuckPerms ping */ SELECT 1");
hikari = new HikariDataSource(config);
}
@Override
public void shutdown() throws Exception {
if (hikari != null) {
hikari.close();
}
}
@Override
public Map<String, String> getMeta() {
Map<String, String> ret = new LinkedHashMap<>();
boolean success = true;
long start = System.currentTimeMillis();
try (Connection c = hikari.getConnection()) {
try (Statement s = c.createStatement()) {
s.execute("/* ping */ SELECT 1");
}
} catch (SQLException e) {
success = false;
}
long duration = System.currentTimeMillis() - start;
if (success) {
ret.put("Ping", "&a" + duration + "ms");
ret.put("Connected", "true");
} else {
ret.put("Connected", "false");
}
return ret;
}
@Override
public Connection getConnection() throws SQLException {
Connection connection = hikari.getConnection();
if (connection == null) {
throw new SQLException("Unable to get a connection from the pool.");
}
return connection;
}
}

View File

@ -23,7 +23,7 @@
* SOFTWARE.
*/
package me.lucko.luckperms.common.storage.backing;
package me.lucko.luckperms.common.storage.dao;
import lombok.AccessLevel;
import lombok.Getter;

View File

@ -23,7 +23,7 @@
* SOFTWARE.
*/
package me.lucko.luckperms.common.storage;
package me.lucko.luckperms.common.storage.dao;
import com.google.common.collect.ImmutableMap;
@ -35,7 +35,6 @@ import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.common.model.Track;
import me.lucko.luckperms.common.model.User;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.storage.backing.AbstractDao;
import java.util.LinkedHashMap;
import java.util.List;
@ -47,7 +46,7 @@ public class SplitStorageDao extends AbstractDao {
private final Map<String, AbstractDao> backing;
private final Map<String, String> types;
protected SplitStorageDao(LuckPermsPlugin plugin, Map<String, AbstractDao> backing, Map<String, String> types) {
public SplitStorageDao(LuckPermsPlugin plugin, Map<String, AbstractDao> backing, Map<String, String> types) {
super(plugin, "Split Storage");
this.backing = ImmutableMap.copyOf(backing);
this.types = ImmutableMap.copyOf(types);

View File

@ -23,7 +23,7 @@
* SOFTWARE.
*/
package me.lucko.luckperms.common.storage.backing.file;
package me.lucko.luckperms.common.storage.dao.file;
import lombok.Getter;
@ -50,9 +50,9 @@ import me.lucko.luckperms.common.node.NodeHeldPermission;
import me.lucko.luckperms.common.node.NodeModel;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.references.UserIdentifier;
import me.lucko.luckperms.common.storage.backing.AbstractDao;
import me.lucko.luckperms.common.storage.backing.legacy.LegacyJsonMigration;
import me.lucko.luckperms.common.storage.backing.legacy.LegacyYamlMigration;
import me.lucko.luckperms.common.storage.dao.AbstractDao;
import me.lucko.luckperms.common.storage.dao.legacy.LegacyJsonMigration;
import me.lucko.luckperms.common.storage.dao.legacy.LegacyYamlMigration;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.SimpleConfigurationNode;

View File

@ -23,7 +23,7 @@
* SOFTWARE.
*/
package me.lucko.luckperms.common.storage.backing.file;
package me.lucko.luckperms.common.storage.dao.file;
import com.google.common.base.Splitter;
import com.google.common.collect.Maps;

View File

@ -23,7 +23,7 @@
* SOFTWARE.
*/
package me.lucko.luckperms.common.storage.backing.file;
package me.lucko.luckperms.common.storage.dao.file;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

View File

@ -23,7 +23,7 @@
* SOFTWARE.
*/
package me.lucko.luckperms.common.storage.backing.file;
package me.lucko.luckperms.common.storage.dao.file;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;

View File

@ -23,7 +23,7 @@
* SOFTWARE.
*/
package me.lucko.luckperms.common.storage.backing.file;
package me.lucko.luckperms.common.storage.dao.file;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;

View File

@ -23,7 +23,7 @@
* SOFTWARE.
*/
package me.lucko.luckperms.common.storage.backing.file;
package me.lucko.luckperms.common.storage.dao.file;
public enum StorageLocation {

View File

@ -23,7 +23,7 @@
* SOFTWARE.
*/
package me.lucko.luckperms.common.storage.backing.file;
package me.lucko.luckperms.common.storage.dao.file;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;

View File

@ -23,7 +23,7 @@
* SOFTWARE.
*/
package me.lucko.luckperms.common.storage.backing.legacy;
package me.lucko.luckperms.common.storage.dao.legacy;
import lombok.RequiredArgsConstructor;
@ -38,7 +38,7 @@ import me.lucko.luckperms.common.contexts.ContextSetJsonSerializer;
import me.lucko.luckperms.common.node.NodeFactory;
import me.lucko.luckperms.common.node.NodeModel;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.storage.backing.file.JsonDao;
import me.lucko.luckperms.common.storage.dao.file.JsonDao;
import java.io.BufferedReader;
import java.io.BufferedWriter;

View File

@ -23,7 +23,7 @@
* SOFTWARE.
*/
package me.lucko.luckperms.common.storage.backing.legacy;
package me.lucko.luckperms.common.storage.dao.legacy;
import lombok.RequiredArgsConstructor;
@ -33,7 +33,7 @@ import com.google.gson.reflect.TypeToken;
import me.lucko.luckperms.common.contexts.ContextSetJsonSerializer;
import me.lucko.luckperms.common.node.NodeFactory;
import me.lucko.luckperms.common.node.NodeModel;
import me.lucko.luckperms.common.storage.backing.sql.SqlDao;
import me.lucko.luckperms.common.storage.dao.sql.SqlDao;
import java.lang.reflect.Type;
import java.sql.Connection;

View File

@ -23,14 +23,14 @@
* SOFTWARE.
*/
package me.lucko.luckperms.common.storage.backing.legacy;
package me.lucko.luckperms.common.storage.dao.legacy;
import lombok.RequiredArgsConstructor;
import me.lucko.luckperms.common.node.NodeFactory;
import me.lucko.luckperms.common.node.NodeModel;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.storage.backing.file.YamlDao;
import me.lucko.luckperms.common.storage.dao.file.YamlDao;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

View File

@ -23,7 +23,7 @@
* SOFTWARE.
*/
package me.lucko.luckperms.common.storage.backing.mongodb;
package me.lucko.luckperms.common.storage.dao.mongodb;
import lombok.Getter;
@ -53,7 +53,7 @@ import me.lucko.luckperms.common.node.NodeModel;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.references.UserIdentifier;
import me.lucko.luckperms.common.storage.DatastoreConfiguration;
import me.lucko.luckperms.common.storage.backing.AbstractDao;
import me.lucko.luckperms.common.storage.dao.AbstractDao;
import org.bson.Document;

View File

@ -23,7 +23,7 @@
* SOFTWARE.
*/
package me.lucko.luckperms.common.storage.backing.sql;
package me.lucko.luckperms.common.storage.dao.sql;
import lombok.Getter;
@ -49,9 +49,9 @@ import me.lucko.luckperms.common.node.NodeHeldPermission;
import me.lucko.luckperms.common.node.NodeModel;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.references.UserIdentifier;
import me.lucko.luckperms.common.storage.backing.AbstractDao;
import me.lucko.luckperms.common.storage.backing.legacy.LegacySqlMigration;
import me.lucko.luckperms.common.storage.backing.sql.provider.AbstractConnectionFactory;
import me.lucko.luckperms.common.storage.dao.AbstractDao;
import me.lucko.luckperms.common.storage.dao.legacy.LegacySqlMigration;
import me.lucko.luckperms.common.storage.dao.sql.connection.AbstractConnectionFactory;
import java.io.BufferedReader;
import java.io.InputStream;

View File

@ -23,7 +23,7 @@
* SOFTWARE.
*/
package me.lucko.luckperms.common.storage.backing.sql.provider;
package me.lucko.luckperms.common.storage.dao.sql.connection;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

View File

@ -23,9 +23,9 @@
* SOFTWARE.
*/
package me.lucko.luckperms.common.storage.backing.sql.provider.file;
package me.lucko.luckperms.common.storage.dao.sql.connection.file;
import me.lucko.luckperms.common.storage.backing.sql.provider.AbstractConnectionFactory;
import me.lucko.luckperms.common.storage.dao.sql.connection.AbstractConnectionFactory;
import java.io.File;
import java.sql.Connection;

View File

@ -23,7 +23,7 @@
* SOFTWARE.
*/
package me.lucko.luckperms.common.storage.backing.sql.provider.file;
package me.lucko.luckperms.common.storage.dao.sql.connection.file;
import java.io.File;
import java.util.LinkedHashMap;

View File

@ -23,7 +23,7 @@
* SOFTWARE.
*/
package me.lucko.luckperms.common.storage.backing.sql.provider.file;
package me.lucko.luckperms.common.storage.dao.sql.connection.file;
import lombok.AllArgsConstructor;
import lombok.experimental.Delegate;

View File

@ -23,7 +23,7 @@
* SOFTWARE.
*/
package me.lucko.luckperms.common.storage.backing.sql.provider.file;
package me.lucko.luckperms.common.storage.dao.sql.connection.file;
import java.io.File;
import java.util.LinkedHashMap;

View File

@ -23,13 +23,13 @@
* SOFTWARE.
*/
package me.lucko.luckperms.common.storage.backing.sql.provider.remote;
package me.lucko.luckperms.common.storage.dao.sql.connection.hikari;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import me.lucko.luckperms.common.storage.DatastoreConfiguration;
import me.lucko.luckperms.common.storage.backing.sql.provider.AbstractConnectionFactory;
import me.lucko.luckperms.common.storage.dao.sql.connection.AbstractConnectionFactory;
import java.sql.Connection;
import java.sql.SQLException;
@ -38,38 +38,46 @@ import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class PostgreConnectionFactory extends AbstractConnectionFactory {
public abstract class HikariConnectionFactory extends AbstractConnectionFactory {
private final DatastoreConfiguration configuration;
protected final DatastoreConfiguration configuration;
private HikariDataSource hikari;
public PostgreConnectionFactory(DatastoreConfiguration configuration) {
super("PostgreSQL");
public HikariConnectionFactory(String name, DatastoreConfiguration configuration) {
super(name);
this.configuration = configuration;
}
@Override
public void init() {
HikariConfig config = new HikariConfig();
protected abstract String getDriverClass();
protected abstract void appendProperties(HikariConfig config);
protected void appendConfigurationInfo(HikariConfig config) {
String address = configuration.getAddress();
String[] addressSplit = address.split(":");
address = addressSplit[0];
String port = addressSplit.length > 1 ? addressSplit[1] : "5432";
String port = addressSplit.length > 1 ? addressSplit[1] : "3306";
String database = configuration.getDatabase();
String username = configuration.getUsername();
String password = configuration.getPassword();
config.setMaximumPoolSize(configuration.getPoolSize());
config.setPoolName("luckperms");
config.setDataSourceClassName("org.postgresql.ds.PGSimpleDataSource");
config.setDataSourceClassName(getDriverClass());
config.addDataSourceProperty("serverName", address);
config.addDataSourceProperty("portNumber", port);
config.addDataSourceProperty("port", port);
config.addDataSourceProperty("databaseName", database);
config.addDataSourceProperty("user", username);
config.addDataSourceProperty("password", password);
config.setUsername(username);
config.setPassword(password);
}
@Override
public void init() {
HikariConfig config = new HikariConfig();
config.setPoolName("luckperms");
appendConfigurationInfo(config);
appendProperties(config);
// We will wait for 15 seconds to get a connection from the pool.
// Default is 30, but it shouldn't be taking that long.
@ -78,7 +86,7 @@ public class PostgreConnectionFactory extends AbstractConnectionFactory {
// If a connection is not returned within 10 seconds, it's probably safe to assume it's been leaked.
config.setLeakDetectionThreshold(TimeUnit.SECONDS.toMillis(10)); // 10000
// Just in-case the driver isn't JDBC4+
// The drivers are really old in some of the older Spigot binaries, so Connection#isValid doesn't work.
config.setConnectionTestQuery("/* LuckPerms ping */ SELECT 1");
hikari = new HikariDataSource(config);

View File

@ -0,0 +1,58 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.storage.dao.sql.connection.hikari;
import com.zaxxer.hikari.HikariConfig;
import me.lucko.luckperms.common.storage.DatastoreConfiguration;
public class MariaDbConnectionFactory extends HikariConnectionFactory {
public MariaDbConnectionFactory(DatastoreConfiguration configuration) {
super("MariaDB", configuration);
}
@Override
protected String getDriverClass() {
return classExists("org.mariadb.jdbc.MariaDbDataSource") ? "org.mariadb.jdbc.MariaDbDataSource" : "org.mariadb.jdbc.MySQLDataSource";
}
@Override
protected void appendProperties(HikariConfig config) {
// kinda hacky. this will call #setProperties on the datasource, which will append these options
// onto the connections.
config.addDataSourceProperty("properties", "useUnicode=true;characterEncoding=utf8");
}
private static boolean classExists(String clazz) {
try {
Class.forName(clazz);
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
}

View File

@ -0,0 +1,60 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.storage.dao.sql.connection.hikari;
import com.zaxxer.hikari.HikariConfig;
import me.lucko.luckperms.common.storage.DatastoreConfiguration;
public class MySqlConnectionFactory extends HikariConnectionFactory {
public MySqlConnectionFactory(DatastoreConfiguration configuration) {
super("MySQL", configuration);
}
@Override
protected String getDriverClass() {
return "com.mysql.jdbc.jdbc2.optional.MysqlDataSource";
}
@Override
protected void appendProperties(HikariConfig config) {
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("alwaysSendSetIsolation", "false");
config.addDataSourceProperty("cacheServerConfiguration", "true");
config.addDataSourceProperty("elideSetAutoCommits", "true");
config.addDataSourceProperty("useLocalSessionState", "true");
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");
}
}

View File

@ -0,0 +1,67 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.storage.dao.sql.connection.hikari;
import com.zaxxer.hikari.HikariConfig;
import me.lucko.luckperms.common.storage.DatastoreConfiguration;
public class PostgreConnectionFactory extends HikariConnectionFactory {
public PostgreConnectionFactory(DatastoreConfiguration configuration) {
super("PostgreSQL", configuration);
}
@Override
protected String getDriverClass() {
return "org.postgresql.ds.PGSimpleDataSource";
}
@Override
protected void appendProperties(HikariConfig config) {
}
@Override
protected void appendConfigurationInfo(HikariConfig config) {
String address = configuration.getAddress();
String[] addressSplit = address.split(":");
address = addressSplit[0];
String port = addressSplit.length > 1 ? addressSplit[1] : "5432";
String database = configuration.getDatabase();
String username = configuration.getUsername();
String password = configuration.getPassword();
config.setMaximumPoolSize(configuration.getPoolSize());
config.setDataSourceClassName(getDriverClass());
config.addDataSourceProperty("serverName", address);
config.addDataSourceProperty("portNumber", port);
config.addDataSourceProperty("databaseName", database);
config.addDataSourceProperty("user", username);
config.addDataSourceProperty("password", password);
}
}

View File

@ -62,7 +62,7 @@ import me.lucko.luckperms.common.plugin.LuckPermsScheduler;
import me.lucko.luckperms.common.storage.Storage;
import me.lucko.luckperms.common.storage.StorageFactory;
import me.lucko.luckperms.common.storage.StorageType;
import me.lucko.luckperms.common.storage.backing.file.FileWatcher;
import me.lucko.luckperms.common.storage.dao.file.FileWatcher;
import me.lucko.luckperms.common.tasks.CacheHousekeepingTask;
import me.lucko.luckperms.common.tasks.ExpireTemporaryTask;
import me.lucko.luckperms.common.tasks.UpdateTask;