wip: java config looking good
This commit is contained in:
@@ -15,13 +15,11 @@
|
||||
*/
|
||||
package org.springframework.cassandra.config;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* Keyspace attributes are used for manipulation around keyspace at the startup. Auto property defines the way how to do
|
||||
* this. Other attributes used to ensure or update keyspace settings.
|
||||
* Keyspace attributes.
|
||||
*
|
||||
* @author Alex Shvid
|
||||
* @author Matthew T. Adams
|
||||
*/
|
||||
public class KeyspaceAttributes {
|
||||
|
||||
@@ -29,49 +27,10 @@ public class KeyspaceAttributes {
|
||||
public static final int DEFAULT_REPLICATION_FACTOR = 1;
|
||||
public static final boolean DEFAULT_DURABLE_WRITES = true;
|
||||
|
||||
/*
|
||||
* auto possible values:
|
||||
* validate: validate the keyspace, makes no changes.
|
||||
* update: update the keyspace.
|
||||
* create: creates the keyspace, destroying previous data.
|
||||
* create-drop: drop the keyspace at the end of the session.
|
||||
*/
|
||||
public static final String AUTO_VALIDATE = "validate";
|
||||
public static final String AUTO_UPDATE = "update";
|
||||
public static final String AUTO_CREATE = "create";
|
||||
public static final String AUTO_CREATE_DROP = "create-drop";
|
||||
|
||||
private String auto = AUTO_VALIDATE;
|
||||
private String replicationStrategy = DEFAULT_REPLICATION_STRATEGY;
|
||||
private int replicationFactor = DEFAULT_REPLICATION_FACTOR;
|
||||
private boolean durableWrites = DEFAULT_DURABLE_WRITES;
|
||||
|
||||
private Collection<TableAttributes> tables;
|
||||
|
||||
public String getAuto() {
|
||||
return auto;
|
||||
}
|
||||
|
||||
public void setAuto(String auto) {
|
||||
this.auto = auto;
|
||||
}
|
||||
|
||||
public boolean isValidate() {
|
||||
return AUTO_VALIDATE.equals(auto);
|
||||
}
|
||||
|
||||
public boolean isUpdate() {
|
||||
return AUTO_UPDATE.equals(auto);
|
||||
}
|
||||
|
||||
public boolean isCreate() {
|
||||
return AUTO_CREATE.equals(auto);
|
||||
}
|
||||
|
||||
public boolean isCreateDrop() {
|
||||
return AUTO_CREATE_DROP.equals(auto);
|
||||
}
|
||||
|
||||
public String getReplicationStrategy() {
|
||||
return replicationStrategy;
|
||||
}
|
||||
@@ -95,13 +54,4 @@ public class KeyspaceAttributes {
|
||||
public void setDurableWrites(boolean durableWrites) {
|
||||
this.durableWrites = durableWrites;
|
||||
}
|
||||
|
||||
public Collection<TableAttributes> getTables() {
|
||||
return tables;
|
||||
}
|
||||
|
||||
public void setTables(Collection<TableAttributes> tables) {
|
||||
this.tables = tables;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,9 +16,10 @@
|
||||
package org.springframework.cassandra.config;
|
||||
|
||||
/**
|
||||
* Pooling options POJO. Can be remote or local.
|
||||
* Pooling options.
|
||||
*
|
||||
* @author Alex Shvid
|
||||
* @author Matthew T. Adams
|
||||
*/
|
||||
public class PoolingOptionsConfig {
|
||||
|
||||
@@ -58,5 +59,4 @@ public class PoolingOptionsConfig {
|
||||
public void setMaxConnections(Integer maxConnections) {
|
||||
this.maxConnections = maxConnections;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,9 +16,10 @@
|
||||
package org.springframework.cassandra.config;
|
||||
|
||||
/**
|
||||
* Socket options POJO. Uses to configure Netty.
|
||||
* Socket options.
|
||||
*
|
||||
* @author Alex Shvid
|
||||
* @author Matthew T. Adams
|
||||
*/
|
||||
public class SocketOptionsConfig {
|
||||
|
||||
@@ -85,5 +86,4 @@ public class SocketOptionsConfig {
|
||||
public void setSendBufferSize(Integer sendBufferSize) {
|
||||
this.sendBufferSize = sendBufferSize;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ public abstract class AbstractCassandraConfiguration {
|
||||
/**
|
||||
* The name of the keyspace to connect to. If {@literal null} or empty, then the system keyspace will be used.
|
||||
*/
|
||||
protected abstract String getKeyspaceName();
|
||||
protected abstract String getKeyspace();
|
||||
|
||||
/**
|
||||
* The {@link Cluster} instance to connect to. Must not be null.
|
||||
@@ -51,7 +51,7 @@ public abstract class AbstractCassandraConfiguration {
|
||||
*/
|
||||
@Bean
|
||||
public Session session() {
|
||||
String keyspace = getKeyspaceName();
|
||||
String keyspace = getKeyspace();
|
||||
if (StringUtils.hasText(keyspace)) {
|
||||
return cluster().connect(keyspace);
|
||||
} else {
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.springframework.cassandra.test.integration.config;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.apache.cassandra.exceptions.ConfigurationException;
|
||||
import org.apache.thrift.transport.TTransportException;
|
||||
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.datastax.driver.core.Session;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public abstract class AbstractIntegrationTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void startCassandra() throws ConfigurationException, TTransportException, IOException {
|
||||
EmbeddedCassandraServerHelper.startEmbeddedCassandra("cassandra.yaml");
|
||||
}
|
||||
|
||||
@Inject
|
||||
public Session session;
|
||||
|
||||
@Before
|
||||
public void assertSession() {
|
||||
IntegrationTestUtils.assertSession(session);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.springframework.cassandra.test.integration.config;
|
||||
|
||||
import org.springframework.cassandra.config.java.AbstractCassandraConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.datastax.driver.core.Cluster;
|
||||
import com.datastax.driver.core.Cluster.Builder;
|
||||
|
||||
@Configuration
|
||||
public abstract class AbstractIntegrationTestConfiguration extends AbstractCassandraConfiguration {
|
||||
|
||||
@Override
|
||||
public Cluster cluster() {
|
||||
Builder builder = Cluster.builder();
|
||||
|
||||
builder.addContactPoint("localhost").withPort(9042);
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package org.springframework.cassandra.test.integration.config;
|
||||
|
||||
import org.springframework.cassandra.config.KeyspaceAttributes;
|
||||
import org.springframework.cassandra.config.PoolingOptionsConfig;
|
||||
import org.springframework.cassandra.config.SocketOptionsConfig;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.datastax.driver.core.KeyspaceMetadata;
|
||||
import com.datastax.driver.core.Session;
|
||||
|
||||
@Configuration
|
||||
public abstract class AbstractKeyspaceCreatingConfiguration extends AbstractIntegrationTestConfiguration {
|
||||
|
||||
@Override
|
||||
public Session session() {
|
||||
|
||||
createKeyspaceIfNecessary();
|
||||
|
||||
return super.session();
|
||||
}
|
||||
|
||||
protected void createKeyspaceIfNecessary() {
|
||||
String keyspace = getKeyspace();
|
||||
if (!StringUtils.hasText(keyspace)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Session system = cluster().connect();
|
||||
KeyspaceMetadata kmd = system.getCluster().getMetadata().getKeyspace(keyspace);
|
||||
if (kmd != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: use KeyspaceBuilder to build keyspace with attributes & options
|
||||
|
||||
system.execute("CREATE KEYSPACE " + keyspace
|
||||
+ " WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };");
|
||||
system.shutdown();
|
||||
}
|
||||
|
||||
protected KeyspaceAttributes getKeyspaceAttributes() {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected PoolingOptionsConfig getLocalPoolingOptionsConfig() {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected PoolingOptionsConfig getRemotePoolingOptionsConfig() {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected SocketOptionsConfig getSocketOptionsConfig() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,12 @@
|
||||
package org.springframework.cassandra.test.integration.config;
|
||||
|
||||
import org.springframework.cassandra.config.java.AbstractCassandraConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.datastax.driver.core.Cluster;
|
||||
import com.datastax.driver.core.Cluster.Builder;
|
||||
|
||||
@Configuration
|
||||
public class Config extends AbstractCassandraConfiguration {
|
||||
|
||||
public Cluster cluster;
|
||||
public class Config extends AbstractIntegrationTestConfiguration {
|
||||
|
||||
@Override
|
||||
protected String getKeyspaceName() {
|
||||
protected String getKeyspace() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Cluster cluster() {
|
||||
Builder builder = Cluster.builder();
|
||||
|
||||
builder.addContactPoint("localhost").withPort(9042);
|
||||
|
||||
return cluster = builder.build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,40 +1,15 @@
|
||||
package org.springframework.cassandra.test.integration.config;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.apache.cassandra.exceptions.ConfigurationException;
|
||||
import org.apache.thrift.transport.TTransportException;
|
||||
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.datastax.driver.core.Session;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = Config.class)
|
||||
public class ConfigTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void startCassandra() throws ConfigurationException, TTransportException, IOException {
|
||||
EmbeddedCassandraServerHelper.startEmbeddedCassandra("cassandra.yaml");
|
||||
}
|
||||
|
||||
@Inject
|
||||
Session session;
|
||||
public class ConfigTest extends AbstractIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
assertNotNull(session);
|
||||
|
||||
session
|
||||
.execute("CREATE KEYSPACE testy WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };");
|
||||
session.execute("USE testy");
|
||||
.execute("CREATE KEYSPACE ConfigTest WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };");
|
||||
session.execute("USE ConfigTest");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.springframework.cassandra.test.integration.config;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import com.datastax.driver.core.Session;
|
||||
|
||||
public class IntegrationTestUtils {
|
||||
|
||||
public static void assertSession(Session session) {
|
||||
assertNotNull(session);
|
||||
}
|
||||
|
||||
public static void assertKeyspaceExists(String keyspace, Session session) {
|
||||
assertNotNull(session.getCluster().getMetadata().getKeyspace(KeyspaceCreatingConfig.KEYSPACE));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package org.springframework.cassandra.test.integration.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class KeyspaceCreatingConfig extends AbstractKeyspaceCreatingConfiguration {
|
||||
|
||||
public static final String KEYSPACE = "kcc";
|
||||
|
||||
@Override
|
||||
protected String getKeyspace() {
|
||||
return KEYSPACE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.springframework.cassandra.test.integration.config;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
@ContextConfiguration(classes = KeyspaceCreatingConfig.class)
|
||||
public class KeyspaceCreatingConfigTest extends AbstractIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
IntegrationTestUtils.assertKeyspaceExists(KeyspaceCreatingConfig.KEYSPACE, session);
|
||||
}
|
||||
}
|
||||
@@ -339,7 +339,7 @@ native_transport_port: 9042
|
||||
# transport is used. They are similar to rpc_min_threads and rpc_max_threads,
|
||||
# though the defaults differ slightly.
|
||||
# native_transport_min_threads: 16
|
||||
native_transport_max_threads: 64
|
||||
native_transport_max_threads: 48
|
||||
|
||||
# Whether to start the thrift rpc server.
|
||||
start_rpc: true
|
||||
|
||||
@@ -65,7 +65,7 @@ public abstract class AbstractSpringDataCassandraConfiguration extends AbstractC
|
||||
*/
|
||||
@Bean
|
||||
public SpringDataKeyspace keyspace() throws Exception {
|
||||
return new SpringDataKeyspace(getKeyspaceName(), session(), converter());
|
||||
return new SpringDataKeyspace(getKeyspace(), session(), converter());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -28,7 +28,7 @@ public class TestConfig extends AbstractSpringDataCassandraConfiguration {
|
||||
* @see org.springframework.data.cassandra.config.AbstractCassandraConfiguration#getKeyspaceName()
|
||||
*/
|
||||
@Override
|
||||
protected String getKeyspaceName() {
|
||||
protected String getKeyspace() {
|
||||
return keyspace;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user