Search in sources :

Example 1 with MariaDbPersistence

use of eu.okaeri.persistence.jdbc.MariaDbPersistence in project okaeri-platform by OkaeriPoland.

the class ExamplePlugin method configurePersistence.

// built-in persistence utils
// easy storage for e.g. player properties
// see persistence/PlayerPersistence for details
@Bean("persistence")
public DocumentPersistence configurePersistence(@Inject("dataFolder") File dataFolder, Plugin plugin, TestConfig config) {
    // jdbc drivers may require initialization for jdbc urls to work
    try {
        Class.forName("org.mariadb.jdbc.Driver");
    } catch (ClassNotFoundException ignored) {
    }
    try {
        Class.forName("org.h2.Driver");
    } catch (ClassNotFoundException ignored) {
    }
    // remember that if plugin is not intended to have shared state
    // between multiple instances you must allow users to set persistence's
    // basePath manually or add some other possibility to differ keys
    // otherwise plugin usage would be limited to one instance per one redis
    PersistencePath basePath = PersistencePath.of(config.getStorage().getPrefix());
    // multiple backends are possible with an easy switch
    switch(config.getStorage().getBackend()) {
        case FLAT:
            // same as: new DocumentPersistence(new FlatPersistence(new File(dataFolder, "storage"), ".yml"), YamlBukkitConfigurer::new, new SerdesBukkit())
            return YamlBungeePersistence.of(new File(dataFolder, "storage"));
        case REDIS:
            // construct redis client based on your needs, e.g. using config
            RedisURI redisUri = RedisURI.create(config.getStorage().getUri());
            RedisClient redisClient = RedisClient.create(redisUri);
            // on the redis side thanks to cjson available in lua scripting
            return new DocumentPersistence(new RedisPersistence(basePath, redisClient), JsonSimpleConfigurer::new);
        case MONGO:
            // construct mongo client based on your needs, e.g. using config
            MongoClientURI mongoUri = new MongoClientURI(config.getStorage().getUri());
            MongoClient mongoClient = new MongoClient(mongoUri);
            // validate if uri contains database
            if (mongoUri.getDatabase() == null) {
                throw new IllegalArgumentException("Mongo URI needs to specify the database: " + mongoUri.getURI());
            }
            // it is REQUIRED to use json configurer for the mongo backend
            return new DocumentPersistence(new MongoPersistence(basePath, mongoClient, mongoUri.getDatabase()), JsonSimpleConfigurer::new);
        case MYSQL:
            // setup hikari based on your needs, e.g. using config
            HikariConfig mariadbHikari = new HikariConfig();
            mariadbHikari.setJdbcUrl(config.getStorage().getUri());
            // it is REQUIRED to use json configurer for the mariadb backend
            return new DocumentPersistence(new MariaDbPersistence(basePath, mariadbHikari), JsonSimpleConfigurer::new);
        case H2:
            // setup hikari based on your needs, e.g. using config
            HikariConfig jdbcHikari = new HikariConfig();
            jdbcHikari.setJdbcUrl(config.getStorage().getUri());
            // it is HIGHLY recommended to use json configurer for the jdbc backend
            return new DocumentPersistence(new H2Persistence(basePath, jdbcHikari), JsonSimpleConfigurer::new);
        default:
            throw new RuntimeException("unsupported storage backend: " + config.getStorage().getBackend());
    }
}
Also used : DocumentPersistence(eu.okaeri.persistence.document.DocumentPersistence) RedisPersistence(eu.okaeri.persistence.redis.RedisPersistence) RedisURI(io.lettuce.core.RedisURI) MongoClientURI(com.mongodb.MongoClientURI) H2Persistence(eu.okaeri.persistence.jdbc.H2Persistence) PersistencePath(eu.okaeri.persistence.PersistencePath) HikariConfig(com.zaxxer.hikari.HikariConfig) RedisClient(io.lettuce.core.RedisClient) MongoClient(com.mongodb.MongoClient) MariaDbPersistence(eu.okaeri.persistence.jdbc.MariaDbPersistence) MongoPersistence(eu.okaeri.persistence.mongo.MongoPersistence) File(java.io.File) JsonSimpleConfigurer(eu.okaeri.configs.json.simple.JsonSimpleConfigurer) Bean(eu.okaeri.platform.core.annotation.Bean)

Example 2 with MariaDbPersistence

use of eu.okaeri.persistence.jdbc.MariaDbPersistence in project okaeri-platform by OkaeriPoland.

the class ExampleWebApplication method configurePersistence.

// built-in persistence utils
// easy storage for e.g. player properties
// see persistence/PlayerPersistence for details
@Bean(value = "persistence", preload = true)
public DocumentPersistence configurePersistence(@Inject("dataFolder") File dataFolder, TestConfig config) {
    // jdbc drivers may require initialization for jdbc urls to work
    try {
        Class.forName("org.mariadb.jdbc.Driver");
    } catch (ClassNotFoundException ignored) {
    }
    try {
        Class.forName("org.h2.Driver");
    } catch (ClassNotFoundException ignored) {
    }
    // remember that if plugin is not intended to have shared state
    // between multiple instances you must allow users to set persistence's
    // basePath manually or add some other possibility to differ keys
    // otherwise plugin usage would be limited to one instance per one redis
    PersistencePath basePath = PersistencePath.of(config.getStorage().getPrefix());
    // multiple backends are possible with an easy switch
    switch(config.getStorage().getBackend()) {
        case FLAT:
            // specify custom child dir in dataFolder or other custom location
            File storagePath = new File(config.getStorage().getUri());
            return new DocumentPersistence(new FlatPersistence(storagePath, ".yml"), YamlSnakeYamlConfigurer::new, new SerdesWeb());
        case REDIS:
            // construct redis client based on your needs, e.g. using config
            RedisURI redisUri = RedisURI.create(config.getStorage().getUri());
            RedisClient redisClient = RedisClient.create(redisUri);
            // on the redis side thanks to cjson available in lua scripting
            return new DocumentPersistence(new RedisPersistence(basePath, redisClient), JsonSimpleConfigurer::new, new SerdesWeb());
        case MONGO:
            // construct mongo client based on your needs, e.g. using config
            MongoClientURI mongoUri = new MongoClientURI(config.getStorage().getUri());
            MongoClient mongoClient = new MongoClient(mongoUri);
            // validate if uri contains database
            if (mongoUri.getDatabase() == null) {
                throw new IllegalArgumentException("Mongo URI needs to specify the database: " + mongoUri.getURI());
            }
            // it is REQUIRED to use json configurer for the mongo backend
            return new DocumentPersistence(new MongoPersistence(basePath, mongoClient, mongoUri.getDatabase()), JsonSimpleConfigurer::new, new SerdesWeb());
        case MYSQL:
            // setup hikari based on your needs, e.g. using config
            HikariConfig mariadbHikari = new HikariConfig();
            mariadbHikari.setJdbcUrl(config.getStorage().getUri());
            // it is REQUIRED to use json configurer for the mariadb backend
            return new DocumentPersistence(new MariaDbPersistence(basePath, mariadbHikari), JsonSimpleConfigurer::new, new SerdesWeb());
        case H2:
            // setup hikari based on your needs, e.g. using config
            HikariConfig jdbcHikari = new HikariConfig();
            jdbcHikari.setJdbcUrl(config.getStorage().getUri());
            // it is HIGHLY recommended to use json configurer for the jdbc backend
            return new DocumentPersistence(new H2Persistence(basePath, jdbcHikari), JsonSimpleConfigurer::new, new SerdesWeb());
        default:
            throw new RuntimeException("unsupported storage backend: " + config.getStorage().getBackend());
    }
}
Also used : DocumentPersistence(eu.okaeri.persistence.document.DocumentPersistence) FlatPersistence(eu.okaeri.persistence.flat.FlatPersistence) RedisPersistence(eu.okaeri.persistence.redis.RedisPersistence) RedisURI(io.lettuce.core.RedisURI) MongoClientURI(com.mongodb.MongoClientURI) H2Persistence(eu.okaeri.persistence.jdbc.H2Persistence) PersistencePath(eu.okaeri.persistence.PersistencePath) HikariConfig(com.zaxxer.hikari.HikariConfig) RedisClient(io.lettuce.core.RedisClient) MongoClient(com.mongodb.MongoClient) MariaDbPersistence(eu.okaeri.persistence.jdbc.MariaDbPersistence) SerdesWeb(eu.okaeri.platform.web.meta.serdes.SerdesWeb) MongoPersistence(eu.okaeri.persistence.mongo.MongoPersistence) File(java.io.File) JsonSimpleConfigurer(eu.okaeri.configs.json.simple.JsonSimpleConfigurer) YamlSnakeYamlConfigurer(eu.okaeri.configs.yaml.snakeyaml.YamlSnakeYamlConfigurer) Bean(eu.okaeri.platform.core.annotation.Bean)

Aggregations

MongoClient (com.mongodb.MongoClient)2 MongoClientURI (com.mongodb.MongoClientURI)2 HikariConfig (com.zaxxer.hikari.HikariConfig)2 JsonSimpleConfigurer (eu.okaeri.configs.json.simple.JsonSimpleConfigurer)2 PersistencePath (eu.okaeri.persistence.PersistencePath)2 DocumentPersistence (eu.okaeri.persistence.document.DocumentPersistence)2 H2Persistence (eu.okaeri.persistence.jdbc.H2Persistence)2 MariaDbPersistence (eu.okaeri.persistence.jdbc.MariaDbPersistence)2 MongoPersistence (eu.okaeri.persistence.mongo.MongoPersistence)2 RedisPersistence (eu.okaeri.persistence.redis.RedisPersistence)2 Bean (eu.okaeri.platform.core.annotation.Bean)2 RedisClient (io.lettuce.core.RedisClient)2 RedisURI (io.lettuce.core.RedisURI)2 File (java.io.File)2 YamlSnakeYamlConfigurer (eu.okaeri.configs.yaml.snakeyaml.YamlSnakeYamlConfigurer)1 FlatPersistence (eu.okaeri.persistence.flat.FlatPersistence)1 SerdesWeb (eu.okaeri.platform.web.meta.serdes.SerdesWeb)1