Search in sources :

Example 1 with H2Persistence

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

the class TestPersistenceJdbc method setup.

@BeforeAll
public void setup() {
    // setup hikari
    HikariConfig config = JdbcHelper.configureHikari("jdbc:h2:mem:test;mode=mysql", "org.h2.Driver");
    // create collection
    this.collection = PersistenceCollection.of(UserRepository.class);
    // prepare persistence backend
    this.persistence = new DocumentPersistence(new H2Persistence(PersistencePath.of("storage"), config), JsonSimpleConfigurer::new);
    this.persistence.registerCollection(this.collection);
    // create repository instance
    this.repository = RepositoryDeclaration.of(UserRepository.class).newProxy(this.persistence, this.collection, TestPersistenceJdbc.class.getClassLoader());
}
Also used : DocumentPersistence(eu.okaeri.persistence.document.DocumentPersistence) UserRepository(eu.okaeri.persistencetestjdbc.basic.repository.UserRepository) H2Persistence(eu.okaeri.persistence.jdbc.H2Persistence) HikariConfig(com.zaxxer.hikari.HikariConfig) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 2 with H2Persistence

use of eu.okaeri.persistence.jdbc.H2Persistence 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 3 with H2Persistence

use of eu.okaeri.persistence.jdbc.H2Persistence 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)

Example 4 with H2Persistence

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

the class TestRefsJdbc method setup.

@BeforeAll
public void setup() {
    // setup hikari
    HikariConfig config = JdbcHelper.configureHikari("jdbc:h2:mem:test;mode=mysql", "org.h2.Driver");
    // create collections
    this.bookCollection = PersistenceCollection.of(BookRepository.class);
    this.authorCollection = PersistenceCollection.of(AuthorRepository.class);
    // prepare persistence backend
    this.persistence = new DocumentPersistence(new H2Persistence(PersistencePath.of("storage"), config), JsonSimpleConfigurer::new);
    this.persistence.registerCollection(this.bookCollection);
    this.persistence.registerCollection(this.authorCollection);
    // create repository instance
    this.bookRepository = RepositoryDeclaration.of(BookRepository.class).newProxy(this.persistence, this.bookCollection, TestRefsJdbc.class.getClassLoader());
    this.authorRepository = RepositoryDeclaration.of(AuthorRepository.class).newProxy(this.persistence, this.authorCollection, TestRefsJdbc.class.getClassLoader());
}
Also used : BookRepository(eu.okaeri.persistencetestjdbc.refs.repository.BookRepository) DocumentPersistence(eu.okaeri.persistence.document.DocumentPersistence) AuthorRepository(eu.okaeri.persistencetestjdbc.refs.repository.AuthorRepository) H2Persistence(eu.okaeri.persistence.jdbc.H2Persistence) HikariConfig(com.zaxxer.hikari.HikariConfig) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

HikariConfig (com.zaxxer.hikari.HikariConfig)4 DocumentPersistence (eu.okaeri.persistence.document.DocumentPersistence)4 H2Persistence (eu.okaeri.persistence.jdbc.H2Persistence)4 MongoClient (com.mongodb.MongoClient)2 MongoClientURI (com.mongodb.MongoClientURI)2 JsonSimpleConfigurer (eu.okaeri.configs.json.simple.JsonSimpleConfigurer)2 PersistencePath (eu.okaeri.persistence.PersistencePath)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 BeforeAll (org.junit.jupiter.api.BeforeAll)2 YamlSnakeYamlConfigurer (eu.okaeri.configs.yaml.snakeyaml.YamlSnakeYamlConfigurer)1 FlatPersistence (eu.okaeri.persistence.flat.FlatPersistence)1 UserRepository (eu.okaeri.persistencetestjdbc.basic.repository.UserRepository)1 AuthorRepository (eu.okaeri.persistencetestjdbc.refs.repository.AuthorRepository)1 BookRepository (eu.okaeri.persistencetestjdbc.refs.repository.BookRepository)1