DATACASS-482 - Refactor KeyspaceActionSpecificationBean to produce KeyspaceActions.
Remove indirection via MultiLevelSetFlattenerFactoryBean and create a KeyspaceActions wrapper that encapsulates the actual actions. Introduce KeyspaceActionSpecificationFactory for keyspace action creation.
This commit is contained in:
@@ -16,7 +16,10 @@
|
||||
package org.springframework.data.cassandra.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
@@ -52,6 +55,34 @@ import com.datastax.driver.core.policies.SpeculativeExecutionPolicy;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.beans.factory.FactoryBean} for configuring a Cassandra {@link Cluster}.
|
||||
* <p>
|
||||
* This factory bean allows configuration and creation of {@link Cluster} bean. Most options default to {@literal null}.
|
||||
* Unsupported options are configured via {@link ClusterBuilderConfigurer}.
|
||||
* <p/>
|
||||
* The factory bean initializes keyspaces, if configured, accoording to its lifecycle. Keyspaces can be created after
|
||||
* {@link #afterPropertiesSet() initialization} and dropped when this factory is {@link #destroy() destroyed}. Keyspace
|
||||
* actions can be configured via {@link #setKeyspaceActions(List) XML} and {@link #setKeyspaceCreations(List)
|
||||
* programatically}. Additional {@link #getStartupScripts()} and {@link #getShutdownScripts()} are executed after
|
||||
* running keyspace actions.
|
||||
* <p/>
|
||||
* <strong>XML configuration</strong>
|
||||
*
|
||||
* <pre class="code">
|
||||
<cql:cluster contact-points="…"
|
||||
port="${build.cassandra.native_transport_port}" compression="SNAPPY" netty-options-ref="nettyOptions">
|
||||
<cql:local-pooling-options
|
||||
min-simultaneous-requests="26" max-simultaneous-requests="101"
|
||||
core-connections="3" max-connections="9"/>
|
||||
<cql:remote-pooling-options
|
||||
min-simultaneous-requests="25" max-simultaneous-requests="100"
|
||||
core-connections="1" max-connections="2"/>
|
||||
<cql:socket-options connect-timeout-millis="5000"
|
||||
keep-alive="true" reuse-address="true" so-linger="60" tcp-no-delay="true"
|
||||
receive-buffer-size="65536" send-buffer-size="65536"/>
|
||||
<cql:keyspace name="${cassandra.keyspace}" action="CREATE_DROP"
|
||||
durable-writes="true"/>
|
||||
</cass:cluster>
|
||||
* </pre>
|
||||
*
|
||||
* @author Alex Shvid
|
||||
* @author Matthew T. Adams
|
||||
@@ -102,6 +133,7 @@ public class CassandraClusterFactoryBean
|
||||
private List<CreateKeyspaceSpecification> keyspaceCreations = new ArrayList<>();
|
||||
private List<DropKeyspaceSpecification> keyspaceDrops = new ArrayList<>();
|
||||
private Set<KeyspaceActionSpecification> keyspaceSpecifications = new HashSet<>();
|
||||
private List<KeyspaceActions> keyspaceActions = new ArrayList<>();
|
||||
|
||||
private List<String> startupScripts = new ArrayList<>();
|
||||
private List<String> shutdownScripts = new ArrayList<>();
|
||||
@@ -260,7 +292,13 @@ public class CassandraClusterFactoryBean
|
||||
*/
|
||||
private void generateSpecificationsFromFactoryBeans() {
|
||||
|
||||
keyspaceSpecifications.forEach(keyspaceActionSpecification -> {
|
||||
generateSpecifications(keyspaceSpecifications);
|
||||
keyspaceActions.forEach(actions -> generateSpecifications(actions.getActions()));
|
||||
}
|
||||
|
||||
private void generateSpecifications(Collection<KeyspaceActionSpecification> specifications) {
|
||||
|
||||
specifications.forEach(keyspaceActionSpecification -> {
|
||||
|
||||
if (keyspaceActionSpecification instanceof CreateKeyspaceSpecification) {
|
||||
keyspaceCreations.add((CreateKeyspaceSpecification) keyspaceActionSpecification);
|
||||
@@ -272,17 +310,17 @@ public class CassandraClusterFactoryBean
|
||||
});
|
||||
}
|
||||
|
||||
protected void executeSpecsAndScripts(List<? extends KeyspaceActionSpecification> kepspaceActionSpecifications,
|
||||
private void executeSpecsAndScripts(List<? extends KeyspaceActionSpecification> keyspaceActionSpecifications,
|
||||
List<String> scripts, Cluster cluster) {
|
||||
|
||||
if (!CollectionUtils.isEmpty(kepspaceActionSpecifications) || !CollectionUtils.isEmpty(scripts)) {
|
||||
if (!CollectionUtils.isEmpty(keyspaceActionSpecifications) || !CollectionUtils.isEmpty(scripts)) {
|
||||
|
||||
Session session = cluster.connect();
|
||||
|
||||
try {
|
||||
CqlTemplate template = new CqlTemplate(session);
|
||||
|
||||
kepspaceActionSpecifications
|
||||
keyspaceActionSpecifications
|
||||
.forEach(keyspaceActionSpecification -> template.execute(toCql(keyspaceActionSpecification)));
|
||||
|
||||
scripts.forEach(template::execute);
|
||||
@@ -429,6 +467,23 @@ public class CassandraClusterFactoryBean
|
||||
this.metricsEnabled = metricsEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the {@link List} of {@link KeyspaceActions}.
|
||||
*/
|
||||
public List<KeyspaceActions> getKeyspaceActions() {
|
||||
return Collections.unmodifiableList(keyspaceActions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a {@link List} of {@link KeyspaceActions} to be executed on initialization. Keyspace actions may contain create
|
||||
* and drop specifications.
|
||||
*
|
||||
* @param keyspaceActions the {@link List} of {@link KeyspaceActions}.
|
||||
*/
|
||||
public void setKeyspaceActions(List<KeyspaceActions> keyspaceActions) {
|
||||
this.keyspaceActions = new ArrayList<>(keyspaceActions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a {@link List} of {@link CreateKeyspaceSpecification create keyspace specifications} that are executed when
|
||||
* this factory is {@link #afterPropertiesSet() initialized}. {@link CreateKeyspaceSpecification Create keyspace
|
||||
@@ -438,14 +493,14 @@ public class CassandraClusterFactoryBean
|
||||
* @param specifications the {@link List} of {@link CreateKeyspaceSpecification create keyspace specifications}.
|
||||
*/
|
||||
public void setKeyspaceCreations(List<CreateKeyspaceSpecification> specifications) {
|
||||
this.keyspaceCreations = specifications;
|
||||
this.keyspaceCreations = new ArrayList<>(specifications);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {@link List} of {@link CreateKeyspaceSpecification create keyspace specifications}.
|
||||
*/
|
||||
public List<CreateKeyspaceSpecification> getKeyspaceCreations() {
|
||||
return keyspaceCreations;
|
||||
return Collections.unmodifiableList(keyspaceCreations);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -456,14 +511,14 @@ public class CassandraClusterFactoryBean
|
||||
* @param specifications the {@link List} of {@link DropKeyspaceSpecification drop keyspace specifications}.
|
||||
*/
|
||||
public void setKeyspaceDrops(List<DropKeyspaceSpecification> specifications) {
|
||||
this.keyspaceDrops = specifications;
|
||||
this.keyspaceDrops = new ArrayList<>(specifications);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the {@link List} of {@link DropKeyspaceSpecification drop keyspace specifications}.
|
||||
*/
|
||||
public List<DropKeyspaceSpecification> getKeyspaceDrops() {
|
||||
return keyspaceDrops;
|
||||
return Collections.unmodifiableList(keyspaceDrops);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -474,14 +529,14 @@ public class CassandraClusterFactoryBean
|
||||
* @param scripts the scripts to execute on startup
|
||||
*/
|
||||
public void setStartupScripts(List<String> scripts) {
|
||||
this.startupScripts = scripts;
|
||||
this.startupScripts = new ArrayList<>(scripts);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the startup scripts
|
||||
*/
|
||||
public List<String> getStartupScripts() {
|
||||
return startupScripts;
|
||||
return Collections.unmodifiableList(startupScripts);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -492,28 +547,28 @@ public class CassandraClusterFactoryBean
|
||||
* @param scripts the scripts to execute on shutdown
|
||||
*/
|
||||
public void setShutdownScripts(List<String> scripts) {
|
||||
this.shutdownScripts = scripts;
|
||||
this.shutdownScripts = new ArrayList<>(scripts);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the shutdown scripts
|
||||
*/
|
||||
public List<String> getShutdownScripts() {
|
||||
return shutdownScripts;
|
||||
return Collections.unmodifiableList(shutdownScripts);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param keyspaceSpecifications The {@link KeyspaceActionSpecification} to set.
|
||||
*/
|
||||
public void setKeyspaceSpecifications(Set<KeyspaceActionSpecification> keyspaceSpecifications) {
|
||||
this.keyspaceSpecifications = keyspaceSpecifications;
|
||||
this.keyspaceSpecifications = new LinkedHashSet<>(keyspaceSpecifications);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the {@link KeyspaceActionSpecification} associated with this factory.
|
||||
*/
|
||||
public Set<KeyspaceActionSpecification> getKeyspaceSpecifications() {
|
||||
return keyspaceSpecifications;
|
||||
return Collections.unmodifiableSet(keyspaceSpecifications);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,10 +16,11 @@
|
||||
package org.springframework.data.cassandra.config;
|
||||
|
||||
/**
|
||||
* Available actions for Keyspace Specifications
|
||||
* Available actions for Keyspace Specifications.
|
||||
*
|
||||
* @author David Webb
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public enum KeyspaceAction {
|
||||
CREATE, CREATE_DROP, ALTER
|
||||
NONE, CREATE, CREATE_DROP, ALTER
|
||||
}
|
||||
|
||||
@@ -0,0 +1,186 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.cassandra.config;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.data.cassandra.core.cql.KeyspaceIdentifier;
|
||||
import org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification;
|
||||
import org.springframework.data.cassandra.core.cql.keyspace.DataCenterReplication;
|
||||
import org.springframework.data.cassandra.core.cql.keyspace.DefaultOption;
|
||||
import org.springframework.data.cassandra.core.cql.keyspace.DropKeyspaceSpecification;
|
||||
import org.springframework.data.cassandra.core.cql.keyspace.KeyspaceOption;
|
||||
import org.springframework.data.cassandra.core.cql.keyspace.KeyspaceOption.ReplicationStrategy;
|
||||
import org.springframework.data.cassandra.core.cql.keyspace.Option;
|
||||
|
||||
/**
|
||||
* Factory to create {@link CreateKeyspaceSpecification} and {@link DropKeyspaceSpecification}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @since 2.0
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
class KeyspaceActionSpecificationFactory {
|
||||
|
||||
private final KeyspaceIdentifier name;
|
||||
|
||||
private final List<DataCenterReplication> replications;
|
||||
|
||||
private final ReplicationStrategy replicationStrategy;
|
||||
|
||||
private final long replicationFactor;
|
||||
|
||||
private final boolean durableWrites;
|
||||
|
||||
/**
|
||||
* Create a new {@link KeyspaceActionSpecificationFactoryBuilder} to configure a new
|
||||
* {@link KeyspaceActionSpecificationFactory}.
|
||||
*
|
||||
* @param keyspaceName must not be {@literal null} or empty.
|
||||
* @return the new {@link KeyspaceActionSpecificationFactoryBuilder} for {@code keyspaceName}.
|
||||
*/
|
||||
public static KeyspaceActionSpecificationFactoryBuilder builder(String keyspaceName) {
|
||||
return builder(KeyspaceIdentifier.of(keyspaceName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link KeyspaceActionSpecificationFactoryBuilder} to configure a new
|
||||
* {@link KeyspaceActionSpecificationFactory}.
|
||||
*
|
||||
* @param keyspaceName must not be {@literal null} or empty.
|
||||
* @return the new {@link KeyspaceActionSpecificationFactoryBuilder} for {@code keyspaceName}.
|
||||
*/
|
||||
public static KeyspaceActionSpecificationFactoryBuilder builder(KeyspaceIdentifier keyspaceName) {
|
||||
return new KeyspaceActionSpecificationFactoryBuilder(keyspaceName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a {@link CreateKeyspaceSpecification} for the keyspace.
|
||||
*
|
||||
* @param ifNotExists {@literal true} to include {@code IF NOT EXISTS} rendering in the create statement.
|
||||
* @return the {@link CreateKeyspaceSpecification}.
|
||||
*/
|
||||
public CreateKeyspaceSpecification create(boolean ifNotExists) {
|
||||
|
||||
CreateKeyspaceSpecification create = CreateKeyspaceSpecification.createKeyspace(name).ifNotExists(ifNotExists)
|
||||
.with(KeyspaceOption.DURABLE_WRITES, durableWrites);
|
||||
|
||||
Map<Option, Object> replicationStrategyMap = new HashMap<>();
|
||||
replicationStrategyMap.put(new DefaultOption("class", String.class, true, false, true),
|
||||
replicationStrategy.getValue());
|
||||
|
||||
if (replicationStrategy == ReplicationStrategy.SIMPLE_STRATEGY) {
|
||||
replicationStrategyMap.put(new DefaultOption("replication_factor", Long.class, true, false, false),
|
||||
replicationFactor);
|
||||
}
|
||||
|
||||
if (replicationStrategy == ReplicationStrategy.NETWORK_TOPOLOGY_STRATEGY) {
|
||||
for (DataCenterReplication datacenter : replications) {
|
||||
replicationStrategyMap.put(new DefaultOption(datacenter.getDataCenter(), Long.class, true, false, false),
|
||||
datacenter.getReplicationFactor());
|
||||
}
|
||||
}
|
||||
|
||||
create.with(KeyspaceOption.REPLICATION, replicationStrategyMap);
|
||||
|
||||
return create;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a {@link DropKeyspaceSpecification} for the keyspace.
|
||||
*
|
||||
* @param ifExists {@literal true} to include {@code IF EXISTS} rendering in the drop statement.
|
||||
* @return the {@link DropKeyspaceSpecification}.
|
||||
*/
|
||||
public DropKeyspaceSpecification drop(boolean ifExists) {
|
||||
return DropKeyspaceSpecification.dropKeyspace(name).ifExists(ifExists);
|
||||
}
|
||||
|
||||
static class KeyspaceActionSpecificationFactoryBuilder {
|
||||
|
||||
private final KeyspaceIdentifier name;
|
||||
|
||||
private final List<DataCenterReplication> replications = new ArrayList<>();
|
||||
|
||||
private ReplicationStrategy replicationStrategy = ReplicationStrategy.SIMPLE_STRATEGY;
|
||||
|
||||
private long replicationFactor;
|
||||
|
||||
private boolean durableWrites = false;
|
||||
|
||||
private KeyspaceActionSpecificationFactoryBuilder(KeyspaceIdentifier name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure simple replication scheme for the keyspace action factory.
|
||||
*
|
||||
* @param replicationFactor the replication factor.
|
||||
* @return this.
|
||||
*/
|
||||
KeyspaceActionSpecificationFactoryBuilder simpleReplication(int replicationFactor) {
|
||||
|
||||
this.replicationFactor = replicationFactor;
|
||||
|
||||
return replicationStrategy(ReplicationStrategy.SIMPLE_STRATEGY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure datacenter replication scheme for the keyspace action factory.
|
||||
*
|
||||
* @param replication the replication configuration.
|
||||
* @return this.
|
||||
*/
|
||||
KeyspaceActionSpecificationFactoryBuilder withDataCenter(DataCenterReplication replication) {
|
||||
|
||||
replicationStrategy(ReplicationStrategy.NETWORK_TOPOLOGY_STRATEGY);
|
||||
this.replications.add(replication);
|
||||
return this;
|
||||
}
|
||||
|
||||
private KeyspaceActionSpecificationFactoryBuilder replicationStrategy(ReplicationStrategy strategy) {
|
||||
|
||||
this.replicationStrategy = strategy;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure durable writes for the keyspace action factory.
|
||||
*
|
||||
* @param durableWrites {@literal true} to enable durable writes.
|
||||
* @return this.
|
||||
*/
|
||||
KeyspaceActionSpecificationFactoryBuilder durableWrites(boolean durableWrites) {
|
||||
|
||||
this.durableWrites = durableWrites;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a new {@link KeyspaceActionSpecificationFactory}.
|
||||
*/
|
||||
public KeyspaceActionSpecificationFactory build() {
|
||||
|
||||
return new KeyspaceActionSpecificationFactory(name, new ArrayList<>(replications), replicationStrategy,
|
||||
replicationFactor, durableWrites);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2017 the original author or authors.
|
||||
* Copyright 2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,23 +15,16 @@
|
||||
*/
|
||||
package org.springframework.data.cassandra.config;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification;
|
||||
import org.springframework.data.cassandra.core.cql.keyspace.DefaultOption;
|
||||
import org.springframework.data.cassandra.core.cql.keyspace.DropKeyspaceSpecification;
|
||||
import org.springframework.data.cassandra.config.KeyspaceActionSpecificationFactory.KeyspaceActionSpecificationFactoryBuilder;
|
||||
import org.springframework.data.cassandra.core.cql.keyspace.DataCenterReplication;
|
||||
import org.springframework.data.cassandra.core.cql.keyspace.KeyspaceActionSpecification;
|
||||
import org.springframework.data.cassandra.core.cql.keyspace.KeyspaceOption;
|
||||
import org.springframework.data.cassandra.core.cql.keyspace.KeyspaceOption.ReplicationStrategy;
|
||||
import org.springframework.data.cassandra.core.cql.keyspace.Option;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -41,39 +34,27 @@ import org.springframework.util.Assert;
|
||||
* {@link KeyspaceActionSpecification} required to satisfy the configuration action.
|
||||
*
|
||||
* @author David Webb
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class KeyspaceActionSpecificationFactoryBean
|
||||
implements FactoryBean<Set<KeyspaceActionSpecification>>, InitializingBean, DisposableBean {
|
||||
public class KeyspaceActionSpecificationFactoryBean implements FactoryBean<KeyspaceActions>, InitializingBean {
|
||||
|
||||
private @Nullable KeyspaceAction action;
|
||||
private KeyspaceAction action = KeyspaceAction.NONE;
|
||||
|
||||
private @Nullable String name;
|
||||
|
||||
private List<String> networkTopologyDataCenters = new LinkedList<>();
|
||||
|
||||
private List<String> networkTopologyReplicationFactors = new LinkedList<>();
|
||||
private @Nullable ReplicationStrategy replicationStrategy;
|
||||
|
||||
private long replicationFactor;
|
||||
private ReplicationStrategy replicationStrategy = ReplicationStrategy.SIMPLE_STRATEGY;
|
||||
|
||||
private int replicationFactor;
|
||||
|
||||
private boolean durableWrites = false;
|
||||
|
||||
private boolean ifNotExists = false;
|
||||
|
||||
private Set<KeyspaceActionSpecification> specs = new HashSet<>();
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.beans.factory.DisposableBean#destroy()
|
||||
*/
|
||||
@Override
|
||||
public void destroy() {
|
||||
action = null;
|
||||
name = null;
|
||||
networkTopologyDataCenters = new LinkedList<>();
|
||||
networkTopologyReplicationFactors = new LinkedList<>();
|
||||
replicationStrategy = null;
|
||||
specs = new HashSet<>();
|
||||
}
|
||||
private @Nullable KeyspaceActions actions;
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
|
||||
@@ -84,65 +65,45 @@ public class KeyspaceActionSpecificationFactoryBean
|
||||
Assert.hasText(name, "Keyspace Name is required for a Keyspace Action");
|
||||
Assert.notNull(action, "Keyspace Action is required for a Keyspace Action");
|
||||
|
||||
switch (action) {
|
||||
case CREATE_DROP:
|
||||
specs.add(generateDropKeyspaceSpecification());
|
||||
case CREATE:
|
||||
// Assert.notNull(replicationStrategy, "Replication Strategy is required to create a Keyspace");
|
||||
specs.add(generateCreateKeyspaceSpecification());
|
||||
break;
|
||||
case ALTER:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a {@link CreateKeyspaceSpecification} for the keyspace.
|
||||
*
|
||||
* @return The {@link CreateKeyspaceSpecification}
|
||||
*/
|
||||
private CreateKeyspaceSpecification generateCreateKeyspaceSpecification() {
|
||||
|
||||
CreateKeyspaceSpecification create = CreateKeyspaceSpecification.createKeyspace(name).ifNotExists(ifNotExists)
|
||||
.with(KeyspaceOption.DURABLE_WRITES, durableWrites);
|
||||
|
||||
Map<Option, Object> replicationStrategyMap = new HashMap<>();
|
||||
replicationStrategyMap.put(new DefaultOption("class", String.class, true, false, true),
|
||||
replicationStrategy.getValue());
|
||||
KeyspaceActionSpecificationFactoryBuilder builder = KeyspaceActionSpecificationFactory.builder(name)
|
||||
.durableWrites(durableWrites);
|
||||
|
||||
if (replicationStrategy == ReplicationStrategy.SIMPLE_STRATEGY) {
|
||||
replicationStrategyMap.put(new DefaultOption("replication_factor", Long.class, true, false, false),
|
||||
replicationFactor);
|
||||
builder.simpleReplication(replicationFactor);
|
||||
}
|
||||
|
||||
if (replicationStrategy == ReplicationStrategy.NETWORK_TOPOLOGY_STRATEGY) {
|
||||
|
||||
int i = 0;
|
||||
for (String datacenter : networkTopologyDataCenters) {
|
||||
replicationStrategyMap.put(new DefaultOption(datacenter, Long.class, true, false, false),
|
||||
networkTopologyReplicationFactors.get(i++));
|
||||
builder.withDataCenter(
|
||||
DataCenterReplication.of(datacenter, Integer.parseInt(networkTopologyReplicationFactors.get(i++))));
|
||||
}
|
||||
}
|
||||
|
||||
create.with(KeyspaceOption.REPLICATION, replicationStrategyMap);
|
||||
KeyspaceActionSpecificationFactory factory = builder.build();
|
||||
|
||||
return create;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a {@link DropKeyspaceSpecification} for the keyspace.
|
||||
*
|
||||
* @return The {@link DropKeyspaceSpecification}
|
||||
*/
|
||||
private DropKeyspaceSpecification generateDropKeyspaceSpecification() {
|
||||
return DropKeyspaceSpecification.dropKeyspace(getName());
|
||||
switch (action) {
|
||||
case NONE:
|
||||
this.actions = new KeyspaceActions();
|
||||
break;
|
||||
case CREATE_DROP:
|
||||
this.actions = new KeyspaceActions(factory.create(ifNotExists), factory.drop(ifNotExists));
|
||||
break;
|
||||
case CREATE:
|
||||
this.actions = new KeyspaceActions(factory.create(ifNotExists));
|
||||
break;
|
||||
default:
|
||||
throw new IllegalStateException(String.format("KeyspaceAction %s not supported", action));
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.beans.factory.FactoryBean#getObject()
|
||||
*/
|
||||
@Override
|
||||
public Set<KeyspaceActionSpecification> getObject() {
|
||||
return specs;
|
||||
public KeyspaceActions getObject() {
|
||||
return actions;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -272,7 +233,7 @@ public class KeyspaceActionSpecificationFactoryBean
|
||||
/**
|
||||
* @param replicationFactor The replicationFactor to set.
|
||||
*/
|
||||
public void setReplicationFactor(long replicationFactor) {
|
||||
public void setReplicationFactor(int replicationFactor) {
|
||||
this.replicationFactor = replicationFactor;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.cassandra.config;
|
||||
|
||||
import lombok.Value;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.cassandra.core.cql.keyspace.KeyspaceActionSpecification;
|
||||
|
||||
/**
|
||||
* Collection of {@link KeyspaceActionSpecification}s. Wraps none, one or multiple keyspace actions (creates, drops).
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @since 2.0
|
||||
*/
|
||||
@Value
|
||||
public class KeyspaceActions {
|
||||
|
||||
private final List<KeyspaceActionSpecification> actions;
|
||||
|
||||
public KeyspaceActions(KeyspaceActionSpecification... actions) {
|
||||
this(Arrays.asList(actions));
|
||||
}
|
||||
|
||||
public KeyspaceActions(List<KeyspaceActionSpecification> actions) {
|
||||
this.actions = actions;
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.cassandra.config;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
|
||||
/**
|
||||
* Given Set of Sets where all child Sets contain the same class, then a single level Set of <T> is generated.
|
||||
*
|
||||
* @author David Webb
|
||||
* @param <T> TODO: Remove me
|
||||
*/
|
||||
public class MultiLevelSetFlattenerFactoryBean<T> implements FactoryBean<Set<T>> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(MultiLevelSetFlattenerFactoryBean.class);
|
||||
|
||||
private Set<Set<T>> multiLevelSet;
|
||||
|
||||
@Override
|
||||
public Set<T> getObject() throws Exception {
|
||||
Set<T> set = new HashSet<>();
|
||||
|
||||
multiLevelSet.stream().flatMap(Collection::stream).forEach(t -> {
|
||||
log.debug(t.toString());
|
||||
log.debug("Set contains -> " + set.contains(t));
|
||||
set.add(t);
|
||||
});
|
||||
|
||||
return set;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return Set.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Returns the multiLevelSet.
|
||||
*/
|
||||
public Set<Set<T>> getMultiLevelSet() {
|
||||
return multiLevelSet;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param multiLevelSet The multiLevelSet to set.
|
||||
*/
|
||||
public void setMultiLevelSet(Set<Set<T>> multiLevelSet) {
|
||||
this.multiLevelSet = multiLevelSet;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -55,11 +55,22 @@ public final class KeyspaceIdentifier implements Comparable<KeyspaceIdentifier>
|
||||
|
||||
/**
|
||||
* Factory method for {@link KeyspaceIdentifier}. Convenient if imported statically.
|
||||
*
|
||||
* @deprecated since 2.0, use {@link #of(CharSequence)}.
|
||||
*/
|
||||
public static KeyspaceIdentifier ksId(CharSequence identifier) {
|
||||
return new KeyspaceIdentifier(identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method for {@link KeyspaceIdentifier}. Convenient if imported statically.
|
||||
*
|
||||
* @since 2.0
|
||||
*/
|
||||
public static KeyspaceIdentifier of(CharSequence identifier) {
|
||||
return new KeyspaceIdentifier(identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if the given {@link CharSequence} is a legal keyspace identifier.
|
||||
*/
|
||||
@@ -114,7 +125,7 @@ public final class KeyspaceIdentifier implements Comparable<KeyspaceIdentifier>
|
||||
}
|
||||
|
||||
KeyspaceIdentifier other = (that instanceof KeyspaceIdentifier) ? (KeyspaceIdentifier) that
|
||||
: ksId((CharSequence) that);
|
||||
: of((CharSequence) that);
|
||||
|
||||
return this.identifier.equals(other.identifier);
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ public class AlterKeyspaceSpecification extends KeyspaceOptionsSpecification<Alt
|
||||
* @return a new {@link AlterKeyspaceSpecification}.
|
||||
*/
|
||||
public static AlterKeyspaceSpecification alterKeyspace(String name) {
|
||||
return alterKeyspace(KeyspaceIdentifier.ksId(name));
|
||||
return alterKeyspace(KeyspaceIdentifier.of(name));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -45,7 +45,7 @@ public class CreateKeyspaceSpecification extends KeyspaceOptionsSpecification<Cr
|
||||
* @return a new {@link CreateKeyspaceSpecification}.
|
||||
*/
|
||||
public static CreateKeyspaceSpecification createKeyspace(String name) {
|
||||
return new CreateKeyspaceSpecification(KeyspaceIdentifier.ksId(name));
|
||||
return new CreateKeyspaceSpecification(KeyspaceIdentifier.of(name));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -40,7 +40,7 @@ public class DropKeyspaceSpecification extends KeyspaceActionSpecification {
|
||||
* @return a new {@link DropKeyspaceSpecification}.
|
||||
*/
|
||||
public static DropKeyspaceSpecification dropKeyspace(String name) {
|
||||
return new DropKeyspaceSpecification(KeyspaceIdentifier.ksId(name));
|
||||
return new DropKeyspaceSpecification(KeyspaceIdentifier.of(name));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user