package com.amebame.triton.config;

import org.apache.commons.lang.StringUtils;

import com.netflix.astyanax.connectionpool.NodeDiscoveryType;
import com.netflix.astyanax.connectionpool.impl.ConnectionPoolConfigurationImpl;
import com.netflix.astyanax.connectionpool.impl.ConnectionPoolType;
import com.netflix.astyanax.impl.AstyanaxConfigurationImpl;
import com.netflix.astyanax.model.ConsistencyLevel;

/**
 * Configuration for the cassandra cluter
 */
public class TritonCassandraClusterConfiguration {
	
	private String name;
	
	private AstyanaxConfigurationImpl astyanaxConfig;
	
	private ConnectionPoolConfigurationImpl poolConfig;

	public TritonCassandraClusterConfiguration() {
		astyanaxConfig = new AstyanaxConfigurationImpl()
		// set default consistency to QUORUM
		.setDefaultReadConsistencyLevel(ConsistencyLevel.CL_QUORUM)
		.setDefaultWriteConsistencyLevel(ConsistencyLevel.CL_QUORUM)
		.setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE)
		.setConnectionPoolType(ConnectionPoolType.ROUND_ROBIN)
		//.setConnectionPoolType(ConnectionPoolType.TOKEN_AWARE)
		;
		poolConfig = new ConnectionPoolConfigurationImpl("astyanax-pool");
	}

	public AstyanaxConfigurationImpl getAstyanaxConfig() {
		return astyanaxConfig;
	}
	
	public ConnectionPoolConfigurationImpl getPoolConfig() {
		return poolConfig;
	}
	
	/**
	 * Cluster name
	 * @return
	 */
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	/**
	 * Max connections per cassandra node
	 * @param maxConns
	 */
	public TritonCassandraClusterConfiguration setMaxConnsPerHost(int maxConns) {
		poolConfig.setMaxConnsPerHost(maxConns);
		return this;
	}
	
	/**
	 * Set seed hosts
	 * @param seeds
	 */
	public TritonCassandraClusterConfiguration setSeeds(String ... seeds) {
		poolConfig.setSeeds(StringUtils.join(seeds, ','));
		return this;
	}
	
	/**
	 * Set auto discovery
	 */
	public TritonCassandraClusterConfiguration setAutoDiscovery(boolean enable) {
		if (enable) {
			astyanaxConfig.setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE);
		} else {
			astyanaxConfig.setDiscoveryType(NodeDiscoveryType.NONE);
		}
		return this;
	}
}

@@ -933,28 +933,19 @@
 eryType.
-RING_DESCRIB
+NON
 E)%0A%09%09.se
@@ -1000,67 +1000,8 @@
 IN)%0A
-%09%09//.setConnectionPoolType(ConnectionPoolType.TOKEN_AWARE)%0A
 %09%09;%0A
@@ -2083,10 +2083,334 @@
 his;%0A%09%7D%0A
+%09%0A%09/**%0A%09 * Set token aware pool type%0A%09 * @param enable%0A%09 * @return%0A%09 */%0A%09public TritonCassandraClusterConfiguration setTokenAware(boolean enable) %7B%0A%09%09if (enable) %7B%0A%09%09%09astyanaxConfig.setDiscoveryType(NodeDiscoveryType.TOKEN_AWARE);%0A%09%09%7D else %7B%0A%09%09%09astyanaxConfig.setDiscoveryType(NodeDiscoveryType.NONE);%0A%09%09%7D%0A%09%09return this;%0A%09%7D%0A
 %7D%0A

Change default discovery type to RING_DESCRIBE.