Search in sources :

Example 1 with CacheDataSource

use of fr.xephi.authme.datasource.CacheDataSource in project AuthMeReloaded by AuthMe.

the class MySqlDefaultChangerTest method shouldLeaveMySqlFieldToNullOnInitialization.

@Test
public void shouldLeaveMySqlFieldToNullOnInitialization() {
    // given
    DataSource dataSource = mock(DataSource.class);
    PlayerCache playerCache = mock(PlayerCache.class);
    CacheDataSource cacheDataSource = new CacheDataSource(dataSource, playerCache);
    MySqlDefaultChanger defaultChanger = createDefaultChanger(cacheDataSource);
    // when
    defaultChanger.setMySqlField();
    // then
    assertThat(ReflectionTestUtils.getFieldValue(MySqlDefaultChanger.class, defaultChanger, "mySql"), nullValue());
}
Also used : CacheDataSource(fr.xephi.authme.datasource.CacheDataSource) PlayerCache(fr.xephi.authme.data.auth.PlayerCache) CacheDataSource(fr.xephi.authme.datasource.CacheDataSource) DataSource(fr.xephi.authme.datasource.DataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) Test(org.junit.Test)

Example 2 with CacheDataSource

use of fr.xephi.authme.datasource.CacheDataSource in project AuthMeReloaded by AuthMe.

the class DataSourceProvider method createDataSource.

/**
 * Sets up the data source.
 *
 * @return the constructed data source
 * @throws SQLException when initialization of a SQL data source failed
 */
private DataSource createDataSource() throws SQLException {
    DataSourceType dataSourceType = settings.getProperty(DatabaseSettings.BACKEND);
    DataSource dataSource;
    switch(dataSourceType) {
        case MYSQL:
            dataSource = new MySQL(settings, mySqlExtensionsFactory);
            break;
        case POSTGRESQL:
            dataSource = new PostgreSqlDataSource(settings, mySqlExtensionsFactory);
            break;
        case SQLITE:
            dataSource = new SQLite(settings, dataFolder);
            break;
        default:
            throw new UnsupportedOperationException("Unknown data source type '" + dataSourceType + "'");
    }
    if (settings.getProperty(DatabaseSettings.USE_CACHING)) {
        dataSource = new CacheDataSource(dataSource, playerCache);
    }
    if (DataSourceType.SQLITE.equals(dataSourceType)) {
        checkDataSourceSize(dataSource);
    }
    return dataSource;
}
Also used : CacheDataSource(fr.xephi.authme.datasource.CacheDataSource) DataSourceType(fr.xephi.authme.datasource.DataSourceType) MySQL(fr.xephi.authme.datasource.MySQL) SQLite(fr.xephi.authme.datasource.SQLite) PostgreSqlDataSource(fr.xephi.authme.datasource.PostgreSqlDataSource) CacheDataSource(fr.xephi.authme.datasource.CacheDataSource) DataSource(fr.xephi.authme.datasource.DataSource) PostgreSqlDataSource(fr.xephi.authme.datasource.PostgreSqlDataSource)

Example 3 with CacheDataSource

use of fr.xephi.authme.datasource.CacheDataSource in project AuthMeReloaded by AuthMe.

the class DebugSectionUtilsTest method shouldUnwrapCacheDataSource.

@Test
public void shouldUnwrapCacheDataSource() {
    // given
    DataSource source = mock(DataSource.class);
    PlayerCache playerCache = mock(PlayerCache.class);
    CacheDataSource cacheDataSource = new CacheDataSource(source, playerCache);
    // when
    DataSource result = DebugSectionUtils.unwrapSourceFromCacheDataSource(cacheDataSource);
    // then
    assertThat(result, equalTo(source));
}
Also used : CacheDataSource(fr.xephi.authme.datasource.CacheDataSource) PlayerCache(fr.xephi.authme.data.auth.PlayerCache) CacheDataSource(fr.xephi.authme.datasource.CacheDataSource) DataSource(fr.xephi.authme.datasource.DataSource) Test(org.junit.Test)

Example 4 with CacheDataSource

use of fr.xephi.authme.datasource.CacheDataSource in project AuthMeReloaded by AuthMe.

the class DataStatistics method outputDatabaseStats.

private void outputDatabaseStats(CommandSender sender) {
    sender.sendMessage("Total players in DB: " + dataSource.getAccountsRegistered());
    if (dataSource instanceof CacheDataSource) {
        CacheDataSource cacheDataSource = (CacheDataSource) this.dataSource;
        sender.sendMessage("Cached PlayerAuth objects: " + cacheDataSource.getCachedAuths().size());
    }
}
Also used : CacheDataSource(fr.xephi.authme.datasource.CacheDataSource)

Example 5 with CacheDataSource

use of fr.xephi.authme.datasource.CacheDataSource in project AuthMeReloaded by AuthMe.

the class DebugSectionUtils method unwrapSourceFromCacheDataSource.

/**
 * Unwraps the "cache data source" and returns the underlying source. Returns the
 * same as the input argument otherwise.
 *
 * @param dataSource the data source to unwrap if applicable
 * @return the non-cache data source
 */
static DataSource unwrapSourceFromCacheDataSource(DataSource dataSource) {
    if (dataSource instanceof CacheDataSource) {
        try {
            Field source = CacheDataSource.class.getDeclaredField("source");
            source.setAccessible(true);
            return (DataSource) source.get(dataSource);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            logger.logException("Could not get source of CacheDataSource:", e);
            return null;
        }
    }
    return dataSource;
}
Also used : Field(java.lang.reflect.Field) CacheDataSource(fr.xephi.authme.datasource.CacheDataSource) CacheDataSource(fr.xephi.authme.datasource.CacheDataSource) DataSource(fr.xephi.authme.datasource.DataSource)

Aggregations

CacheDataSource (fr.xephi.authme.datasource.CacheDataSource)6 DataSource (fr.xephi.authme.datasource.DataSource)4 Test (org.junit.Test)3 PlayerCache (fr.xephi.authme.data.auth.PlayerCache)2 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 DataSourceType (fr.xephi.authme.datasource.DataSourceType)1 MySQL (fr.xephi.authme.datasource.MySQL)1 PostgreSqlDataSource (fr.xephi.authme.datasource.PostgreSqlDataSource)1 SQLite (fr.xephi.authme.datasource.SQLite)1 Field (java.lang.reflect.Field)1 Optional (java.util.Optional)1 CommandSender (org.bukkit.command.CommandSender)1