Search in sources :

Example 11 with DatabaseConfig

use of com.scalar.db.config.DatabaseConfig in project scalardb by scalar-labs.

the class JdbcUtilsTest method initDataSource_NonTransactional_ShouldReturnProperDataSource.

@Test
public void initDataSource_NonTransactional_ShouldReturnProperDataSource() throws SQLException {
    // Arrange
    Properties properties = new Properties();
    properties.setProperty(DatabaseConfig.CONTACT_POINTS, "jdbc:mysql://localhost:3306/");
    properties.setProperty(DatabaseConfig.USERNAME, "root");
    properties.setProperty(DatabaseConfig.PASSWORD, "mysql");
    properties.setProperty(DatabaseConfig.STORAGE, "jdbc");
    properties.setProperty(JdbcConfig.ISOLATION_LEVEL, "SERIALIZABLE");
    properties.setProperty(JdbcConfig.CONNECTION_POOL_MIN_IDLE, "10");
    properties.setProperty(JdbcConfig.CONNECTION_POOL_MAX_IDLE, "20");
    properties.setProperty(JdbcConfig.CONNECTION_POOL_MAX_TOTAL, "30");
    properties.setProperty(JdbcConfig.PREPARED_STATEMENTS_POOL_ENABLED, "true");
    properties.setProperty(JdbcConfig.PREPARED_STATEMENTS_POOL_MAX_OPEN, "100");
    JdbcConfig config = new JdbcConfig(new DatabaseConfig(properties));
    // Act
    BasicDataSource dataSource = JdbcUtils.initDataSource(config);
    // Assert
    assertThat(dataSource.getUrl()).isEqualTo("jdbc:mysql://localhost:3306/");
    assertThat(dataSource.getDriver().getClass().getName()).isEqualTo("com.mysql.cj.jdbc.Driver");
    assertThat(dataSource.getUsername()).isEqualTo("root");
    assertThat(dataSource.getPassword()).isEqualTo("mysql");
    assertThat(dataSource.getDefaultAutoCommit()).isEqualTo(null);
    assertThat(dataSource.getAutoCommitOnReturn()).isEqualTo(true);
    assertThat(dataSource.getDefaultTransactionIsolation()).isEqualTo(Connection.TRANSACTION_SERIALIZABLE);
    assertThat(dataSource.getMinIdle()).isEqualTo(10);
    assertThat(dataSource.getMaxIdle()).isEqualTo(20);
    assertThat(dataSource.getMaxTotal()).isEqualTo(30);
    assertThat(dataSource.isPoolPreparedStatements()).isEqualTo(true);
    assertThat(dataSource.getMaxOpenPreparedStatements()).isEqualTo(100);
    dataSource.close();
}
Also used : Properties(java.util.Properties) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) DatabaseConfig(com.scalar.db.config.DatabaseConfig) Test(org.junit.jupiter.api.Test)

Example 12 with DatabaseConfig

use of com.scalar.db.config.DatabaseConfig in project scalardb by scalar-labs.

the class JdbcUtilsTest method initDataSource_Transactional_ShouldReturnProperDataSource.

@Test
public void initDataSource_Transactional_ShouldReturnProperDataSource() throws SQLException {
    // Arrange
    Properties properties = new Properties();
    properties.setProperty(DatabaseConfig.CONTACT_POINTS, "jdbc:postgresql://localhost:5432/");
    properties.setProperty(DatabaseConfig.USERNAME, "user");
    properties.setProperty(DatabaseConfig.PASSWORD, "postgres");
    properties.setProperty(DatabaseConfig.STORAGE, "jdbc");
    properties.setProperty(JdbcConfig.ISOLATION_LEVEL, "READ_COMMITTED");
    properties.setProperty(JdbcConfig.CONNECTION_POOL_MIN_IDLE, "30");
    properties.setProperty(JdbcConfig.CONNECTION_POOL_MAX_IDLE, "40");
    properties.setProperty(JdbcConfig.CONNECTION_POOL_MAX_TOTAL, "50");
    properties.setProperty(JdbcConfig.PREPARED_STATEMENTS_POOL_ENABLED, "true");
    properties.setProperty(JdbcConfig.PREPARED_STATEMENTS_POOL_MAX_OPEN, "200");
    JdbcConfig config = new JdbcConfig(new DatabaseConfig(properties));
    // Act
    BasicDataSource dataSource = JdbcUtils.initDataSource(config, true);
    // Assert
    assertThat(dataSource.getUrl()).isEqualTo("jdbc:postgresql://localhost:5432/");
    assertThat(dataSource.getDriver().getClass().getName()).isEqualTo("org.postgresql.Driver");
    assertThat(dataSource.getUsername()).isEqualTo("user");
    assertThat(dataSource.getPassword()).isEqualTo("postgres");
    assertThat(dataSource.getDefaultAutoCommit()).isEqualTo(false);
    assertThat(dataSource.getAutoCommitOnReturn()).isEqualTo(false);
    assertThat(dataSource.getDefaultTransactionIsolation()).isEqualTo(Connection.TRANSACTION_READ_COMMITTED);
    assertThat(dataSource.getMinIdle()).isEqualTo(30);
    assertThat(dataSource.getMaxIdle()).isEqualTo(40);
    assertThat(dataSource.getMaxTotal()).isEqualTo(50);
    assertThat(dataSource.isPoolPreparedStatements()).isEqualTo(true);
    assertThat(dataSource.getMaxOpenPreparedStatements()).isEqualTo(200);
    dataSource.close();
}
Also used : Properties(java.util.Properties) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) DatabaseConfig(com.scalar.db.config.DatabaseConfig) Test(org.junit.jupiter.api.Test)

Example 13 with DatabaseConfig

use of com.scalar.db.config.DatabaseConfig in project scalardb by scalar-labs.

the class MultiStorageConfigTest method constructor_AllPropertiesGiven_ShouldLoadProperly.

@Test
public void constructor_AllPropertiesGiven_ShouldLoadProperly() {
    // Arrange
    Properties props = new Properties();
    props.setProperty(DatabaseConfig.STORAGE, "multi-storage");
    props.setProperty(MultiStorageConfig.STORAGES, "cassandra,mysql");
    props.setProperty(MultiStorageConfig.STORAGES + ".cassandra.storage", "cassandra");
    props.setProperty(MultiStorageConfig.STORAGES + ".cassandra.contact_points", "localhost");
    props.setProperty(MultiStorageConfig.STORAGES + ".cassandra.contact_port", "7000");
    props.setProperty(MultiStorageConfig.STORAGES + ".cassandra.username", "cassandra");
    props.setProperty(MultiStorageConfig.STORAGES + ".cassandra.password", "cassandra");
    props.setProperty(MultiStorageConfig.STORAGES + ".mysql.storage", "jdbc");
    props.setProperty(MultiStorageConfig.STORAGES + ".mysql.contact_points", "jdbc:mysql://localhost:3306/");
    props.setProperty(MultiStorageConfig.STORAGES + ".mysql.username", "root");
    props.setProperty(MultiStorageConfig.STORAGES + ".mysql.password", "mysql");
    props.setProperty(MultiStorageConfig.TABLE_MAPPING, "user.order:cassandra,user.customer:mysql,coordinator.state:cassandra");
    props.setProperty(MultiStorageConfig.NAMESPACE_MAPPING, "namespace1:cassandra,namespace2:mysql");
    props.setProperty(MultiStorageConfig.DEFAULT_STORAGE, "cassandra");
    // Act
    MultiStorageConfig config = new MultiStorageConfig(new DatabaseConfig(props));
    // Assert
    assertThat(config.getDatabasePropertiesMap().size()).isEqualTo(2);
    assertThat(config.getDatabasePropertiesMap().containsKey("cassandra")).isTrue();
    DatabaseConfig c = new DatabaseConfig(config.getDatabasePropertiesMap().get("cassandra"));
    assertThat(c.getStorageClass()).isEqualTo(Cassandra.class);
    assertThat(c.getContactPoints()).isEqualTo(Collections.singletonList("localhost"));
    assertThat(c.getContactPort()).isEqualTo(7000);
    assertThat(c.getUsername().isPresent()).isTrue();
    assertThat(c.getUsername().get()).isEqualTo("cassandra");
    assertThat(c.getPassword().isPresent()).isTrue();
    assertThat(c.getPassword().get()).isEqualTo("cassandra");
    assertThat(config.getDatabasePropertiesMap().containsKey("mysql")).isTrue();
    c = new DatabaseConfig(config.getDatabasePropertiesMap().get("mysql"));
    assertThat(c.getStorageClass()).isEqualTo(JdbcDatabase.class);
    assertThat(c.getContactPoints()).isEqualTo(Collections.singletonList("jdbc:mysql://localhost:3306/"));
    assertThat(c.getUsername().isPresent()).isTrue();
    assertThat(c.getUsername().get()).isEqualTo("root");
    assertThat(c.getPassword().isPresent()).isTrue();
    assertThat(c.getPassword().get()).isEqualTo("mysql");
    assertThat(config.getTableStorageMap().size()).isEqualTo(3);
    assertThat(config.getTableStorageMap().get("user.order")).isEqualTo("cassandra");
    assertThat(config.getTableStorageMap().get("user.customer")).isEqualTo("mysql");
    assertThat(config.getTableStorageMap().get("coordinator.state")).isEqualTo("cassandra");
    assertThat(config.getNamespaceStorageMap().size()).isEqualTo(2);
    assertThat(config.getNamespaceStorageMap().get("namespace1")).isEqualTo("cassandra");
    assertThat(config.getNamespaceStorageMap().get("namespace2")).isEqualTo("mysql");
    assertThat(config.getDefaultStorage()).isEqualTo("cassandra");
}
Also used : Properties(java.util.Properties) DatabaseConfig(com.scalar.db.config.DatabaseConfig) Test(org.junit.jupiter.api.Test)

Example 14 with DatabaseConfig

use of com.scalar.db.config.DatabaseConfig in project scalardb by scalar-labs.

the class MultiStorageConfigTest method constructor_NestedMultiStorageGiven_ShouldThrowIllegalArgumentException.

@Test
public void constructor_NestedMultiStorageGiven_ShouldThrowIllegalArgumentException() {
    // Arrange
    Properties props = new Properties();
    props.setProperty(DatabaseConfig.STORAGE, "multi-storage");
    props.setProperty(MultiStorageConfig.STORAGES, "db,mysql");
    // nested
    props.setProperty(MultiStorageConfig.STORAGES + ".db.storage", "multis-torage");
    props.setProperty(MultiStorageConfig.STORAGES + ".db.contact_points", "localhost");
    props.setProperty(MultiStorageConfig.STORAGES + ".db.username", "user");
    props.setProperty(MultiStorageConfig.STORAGES + ".db.password", "pass");
    props.setProperty(MultiStorageConfig.STORAGES + ".mysql.storage", "jdbc");
    props.setProperty(MultiStorageConfig.STORAGES + ".mysql.contact_points", "jdbc:mysql://localhost:3306/");
    props.setProperty(MultiStorageConfig.STORAGES + ".mysql.username", "root");
    props.setProperty(MultiStorageConfig.STORAGES + ".mysql.password", "mysql");
    props.setProperty(MultiStorageConfig.TABLE_MAPPING, "user.order:cassandra,user.customer:mysql,coordinator.state:cassandra");
    props.setProperty(MultiStorageConfig.NAMESPACE_MAPPING, "namespace1:cassandra,namespace2:mysql");
    props.setProperty(MultiStorageConfig.DEFAULT_STORAGE, "cassandra");
    // Act Assert
    assertThatThrownBy(() -> new MultiStorageConfig(new DatabaseConfig(props))).isInstanceOf(IllegalArgumentException.class);
}
Also used : Properties(java.util.Properties) DatabaseConfig(com.scalar.db.config.DatabaseConfig) Test(org.junit.jupiter.api.Test)

Example 15 with DatabaseConfig

use of com.scalar.db.config.DatabaseConfig in project scalardb by scalar-labs.

the class MultiStorageConfigTest method constructor_NonExistentStorageGivenInTableMapping_ShouldThrowIllegalArgumentException.

@Test
public void constructor_NonExistentStorageGivenInTableMapping_ShouldThrowIllegalArgumentException() {
    // Arrange
    Properties props = new Properties();
    props.setProperty(DatabaseConfig.STORAGE, "multi-storage");
    props.setProperty(MultiStorageConfig.STORAGES, "cassandra,mysql");
    props.setProperty(MultiStorageConfig.STORAGES + ".cassandra.storage", "cassandra");
    props.setProperty(MultiStorageConfig.STORAGES + ".cassandra.contact_points", "localhost");
    props.setProperty(MultiStorageConfig.STORAGES + ".cassandra.contact_port", "7000");
    props.setProperty(MultiStorageConfig.STORAGES + ".cassandra.username", "cassandra");
    props.setProperty(MultiStorageConfig.STORAGES + ".cassandra.password", "cassandra");
    props.setProperty(MultiStorageConfig.STORAGES + ".mysql.storage", "jdbc");
    props.setProperty(MultiStorageConfig.STORAGES + ".mysql.contact_points", "jdbc:mysql://localhost:3306/");
    props.setProperty(MultiStorageConfig.STORAGES + ".mysql.username", "root");
    props.setProperty(MultiStorageConfig.STORAGES + ".mysql.password", "mysql");
    props.setProperty(MultiStorageConfig.TABLE_MAPPING, "user.order:cassandra,user.customer:mysql," + // non-existent storage
    "coordinator.state:dynamo");
    props.setProperty(MultiStorageConfig.NAMESPACE_MAPPING, "namespace1:cassandra,namespace2:mysql");
    props.setProperty(MultiStorageConfig.DEFAULT_STORAGE, "cassandra");
    // Act Assert
    assertThatThrownBy(() -> new MultiStorageConfig(new DatabaseConfig(props))).isInstanceOf(IllegalArgumentException.class);
}
Also used : Properties(java.util.Properties) DatabaseConfig(com.scalar.db.config.DatabaseConfig) Test(org.junit.jupiter.api.Test)

Aggregations

DatabaseConfig (com.scalar.db.config.DatabaseConfig)66 Properties (java.util.Properties)60 Test (org.junit.jupiter.api.Test)40 StorageFactory (com.scalar.db.service.StorageFactory)6 BasicDataSource (org.apache.commons.dbcp2.BasicDataSource)4 BeforeAll (org.junit.jupiter.api.BeforeAll)3 ImmutableList (com.google.common.collect.ImmutableList)2 ConsensusCommitAdmin (com.scalar.db.transaction.consensuscommit.ConsensusCommitAdmin)2 File (java.io.File)2 Before (org.junit.Before)2 BeforeClass (org.junit.BeforeClass)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ConfigUtils.getString (com.scalar.db.config.ConfigUtils.getString)1 JdbcConfig (com.scalar.db.storage.jdbc.JdbcConfig)1 ConsensusCommitConfig (com.scalar.db.transaction.consensuscommit.ConsensusCommitConfig)1