|
|
@@ -1,79 +1,79 @@
|
|
|
-package cz.senslog.analyzer.storage;
|
|
|
-
|
|
|
-import com.zaxxer.hikari.HikariConfig;
|
|
|
-import com.zaxxer.hikari.HikariDataSource;
|
|
|
-import cz.senslog.analyzer.storage.inmemory.InMemoryConnection;
|
|
|
-import cz.senslog.analyzer.storage.inmemory.InMemoryStorageConfig;
|
|
|
-import cz.senslog.analyzer.storage.permanent.PermanentStorageConfig;
|
|
|
-import cz.senslog.analyzer.storage.permanent.PermanentConnection;
|
|
|
-import dagger.Module;
|
|
|
-import dagger.Provides;
|
|
|
-import org.apache.logging.log4j.LogManager;
|
|
|
-import org.apache.logging.log4j.Logger;
|
|
|
-import org.jdbi.v3.core.Jdbi;
|
|
|
-import org.jdbi.v3.jodatime2.JodaTimePlugin;
|
|
|
-import org.jdbi.v3.postgres.PostgresPlugin;
|
|
|
-import org.postgresql.ds.PGSimpleDataSource;
|
|
|
-
|
|
|
-import javax.inject.Singleton;
|
|
|
-
|
|
|
-import static java.lang.String.format;
|
|
|
-
|
|
|
-@Module
|
|
|
-public class ConnectionModule {
|
|
|
-
|
|
|
- private static final Logger logger = LogManager.getLogger(ConnectionModule.class);
|
|
|
-
|
|
|
- private final StorageConfig config;
|
|
|
-
|
|
|
- public static ConnectionModule create(StorageConfig storageConfig) {
|
|
|
- return new ConnectionModule(storageConfig);
|
|
|
- }
|
|
|
-
|
|
|
- private ConnectionModule(StorageConfig config) {
|
|
|
- this.config = config;
|
|
|
- }
|
|
|
-
|
|
|
- private PermanentConnection permanentConnection;
|
|
|
- private InMemoryConnection inMemoryConnection;
|
|
|
-
|
|
|
- @Provides @Singleton
|
|
|
- public PermanentConnection providePermanentStorageConnection() {
|
|
|
- if (permanentConnection == null) {
|
|
|
- PermanentStorageConfig config = this.config.getPermanentStorageConfig();
|
|
|
- logger.info("Creating a connection to the database of the class: {}.", Jdbi.class);
|
|
|
- PGSimpleDataSource ds = new PGSimpleDataSource();
|
|
|
- ds.setUrl(config.getConnectionUrl());
|
|
|
- ds.setPassword(config.getPassword());
|
|
|
- ds.setUser(config.getUsername());
|
|
|
- ds.setLoadBalanceHosts(true);
|
|
|
- HikariConfig hc = new HikariConfig();
|
|
|
- hc.setDataSource(ds);
|
|
|
- hc.setMaximumPoolSize(config.getConnectionPoolSize());
|
|
|
-
|
|
|
- permanentConnection = new PermanentConnection(Jdbi
|
|
|
- .create(new HikariDataSource(hc))
|
|
|
- .installPlugin(new PostgresPlugin())
|
|
|
- .installPlugin(new JodaTimePlugin())
|
|
|
- );
|
|
|
- }
|
|
|
- return permanentConnection;
|
|
|
- }
|
|
|
-
|
|
|
- @Provides @Singleton
|
|
|
- public InMemoryConnection provideInMemoryStorageConnection() {
|
|
|
- if (inMemoryConnection == null) {
|
|
|
- InMemoryStorageConfig config = this.config.getInMemoryStorageConfig();
|
|
|
-
|
|
|
- StringBuilder url = new StringBuilder("jdbc:h2");
|
|
|
- url.append(config.isPersistence() ? ":file" : ":mem");
|
|
|
- url.append(":").append(config.getPath());
|
|
|
- if (!config.getParameters().isEmpty()) {
|
|
|
- url.append(":").append(config.getParameters());
|
|
|
- }
|
|
|
-
|
|
|
- inMemoryConnection = new InMemoryConnection(Jdbi.create(url.toString()));
|
|
|
- }
|
|
|
- return inMemoryConnection;
|
|
|
- }
|
|
|
-}
|
|
|
+package cz.senslog.analyzer.storage;
|
|
|
+
|
|
|
+import com.zaxxer.hikari.HikariConfig;
|
|
|
+import com.zaxxer.hikari.HikariDataSource;
|
|
|
+import cz.senslog.analyzer.storage.inmemory.InMemoryConnection;
|
|
|
+import cz.senslog.analyzer.storage.inmemory.InMemoryStorageConfig;
|
|
|
+import cz.senslog.analyzer.storage.permanent.PermanentStorageConfig;
|
|
|
+import cz.senslog.analyzer.storage.permanent.PermanentConnection;
|
|
|
+import dagger.Module;
|
|
|
+import dagger.Provides;
|
|
|
+import org.apache.logging.log4j.LogManager;
|
|
|
+import org.apache.logging.log4j.Logger;
|
|
|
+import org.jdbi.v3.core.Jdbi;
|
|
|
+import org.jdbi.v3.jodatime2.JodaTimePlugin;
|
|
|
+import org.jdbi.v3.postgres.PostgresPlugin;
|
|
|
+import org.postgresql.ds.PGSimpleDataSource;
|
|
|
+
|
|
|
+import javax.inject.Singleton;
|
|
|
+
|
|
|
+import static java.lang.String.format;
|
|
|
+
|
|
|
+@Module
|
|
|
+public class ConnectionModule {
|
|
|
+
|
|
|
+ private static final Logger logger = LogManager.getLogger(ConnectionModule.class);
|
|
|
+
|
|
|
+ private final StorageConfig config;
|
|
|
+
|
|
|
+ public static ConnectionModule create(StorageConfig storageConfig) {
|
|
|
+ return new ConnectionModule(storageConfig);
|
|
|
+ }
|
|
|
+
|
|
|
+ private ConnectionModule(StorageConfig config) {
|
|
|
+ this.config = config;
|
|
|
+ }
|
|
|
+
|
|
|
+ private PermanentConnection permanentConnection;
|
|
|
+ private InMemoryConnection inMemoryConnection;
|
|
|
+
|
|
|
+ @Provides @Singleton
|
|
|
+ public PermanentConnection providePermanentStorageConnection() {
|
|
|
+ if (permanentConnection == null) {
|
|
|
+ PermanentStorageConfig config = this.config.getPermanentStorageConfig();
|
|
|
+ logger.info("Creating a connection to the database of the class: {}.", Jdbi.class);
|
|
|
+ PGSimpleDataSource ds = new PGSimpleDataSource();
|
|
|
+ ds.setUrl(config.getConnectionUrl());
|
|
|
+ ds.setPassword(config.getPassword());
|
|
|
+ ds.setUser(config.getUsername());
|
|
|
+ ds.setLoadBalanceHosts(true);
|
|
|
+ HikariConfig hc = new HikariConfig();
|
|
|
+ hc.setDataSource(ds);
|
|
|
+ hc.setMaximumPoolSize(config.getConnectionPoolSize());
|
|
|
+
|
|
|
+ permanentConnection = new PermanentConnection(Jdbi
|
|
|
+ .create(new HikariDataSource(hc))
|
|
|
+ .installPlugin(new PostgresPlugin())
|
|
|
+ .installPlugin(new JodaTimePlugin())
|
|
|
+ );
|
|
|
+ }
|
|
|
+ return permanentConnection;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Provides @Singleton
|
|
|
+ public InMemoryConnection provideInMemoryStorageConnection() {
|
|
|
+ if (inMemoryConnection == null) {
|
|
|
+ InMemoryStorageConfig config = this.config.getInMemoryStorageConfig();
|
|
|
+
|
|
|
+ StringBuilder url = new StringBuilder("jdbc:h2");
|
|
|
+ url.append(config.isPersistence() ? ":file" : ":mem");
|
|
|
+ url.append(":").append(config.getPath());
|
|
|
+ if (!config.getParameters().isEmpty()) {
|
|
|
+ url.append(":").append(config.getParameters());
|
|
|
+ }
|
|
|
+
|
|
|
+ inMemoryConnection = new InMemoryConnection(Jdbi.create(url.toString()));
|
|
|
+ }
|
|
|
+ return inMemoryConnection;
|
|
|
+ }
|
|
|
+}
|