Permalink
Browse files

support custom connection building to satisfy issue #34

  • Loading branch information...
1 parent d221241 commit 55255123e02778fca04c940b302d4cfe3821b58f @nberardi nberardi committed May 30, 2012
View
@@ -12,7 +12,7 @@ namespace FluentCassandra
public class CassandraContext : IDisposable
{
private readonly IList<IFluentMutationTracker> _trackers;
- private readonly ConnectionBuilder _connectionBuilder;
+ private readonly IConnectionBuilder _connectionBuilder;
private CassandraSession _session;
/// <summary>
@@ -56,7 +56,7 @@ public CassandraContext(CassandraSession session)
///
/// </summary>
/// <param name="connectionBuilder"></param>
- public CassandraContext(ConnectionBuilder connectionBuilder)
+ public CassandraContext(IConnectionBuilder connectionBuilder)
{
ThrowErrors = true;
View
@@ -42,7 +42,7 @@ public CassandraSession(string connectionString)
///
/// </summary>
/// <param name="connectionBuilder"></param>
- public CassandraSession(ConnectionBuilder connectionBuilder)
+ public CassandraSession(IConnectionBuilder connectionBuilder)
: this(ConnectionProviderFactory.Get(connectionBuilder), connectionBuilder.ReadConsistency, connectionBuilder.WriteConsistency) { }
/// <summary>
@@ -67,7 +67,7 @@ public CassandraSession(IConnectionProvider connectionProvider, ConsistencyLevel
/// <summary>
/// The connection builder that is currently in use for this session.
/// </summary>
- public ConnectionBuilder ConnectionBuilder { get; private set; }
+ public IConnectionBuilder ConnectionBuilder { get; private set; }
/// <summary>
///
@@ -23,7 +23,7 @@ public class Connection : IConnection, IDisposable
///
/// </summary>
/// <param name="builder"></param>
- internal Connection(Server server, ConnectionBuilder builder)
+ internal Connection(Server server, IConnectionBuilder builder)
{
_connectionType = builder.ConnectionType;
_bufferSize = builder.BufferSize;
@@ -5,7 +5,7 @@
namespace FluentCassandra.Connections
{
- public class ConnectionBuilder
+ public class ConnectionBuilder : FluentCassandra.Connections.IConnectionBuilder
{
/// <summary>
///
@@ -455,5 +455,10 @@ private string GetConnectionString()
///
/// </summary>
public string ConnectionString { get; private set; }
+
+ /// <summary>
+ /// A unique identifier for the connection builder.
+ /// </summary>
+ public string Uuid { get { return ConnectionString; } }
}
}
@@ -8,7 +8,7 @@ public abstract class ConnectionProvider : IConnectionProvider
///
/// </summary>
/// <param name="builder"></param>
- protected ConnectionProvider(ConnectionBuilder builder)
+ protected ConnectionProvider(IConnectionBuilder builder)
{
ConnectionBuilder = builder;
Servers = new RoundRobinServerManager(builder);
@@ -17,7 +17,7 @@ protected ConnectionProvider(ConnectionBuilder builder)
/// <summary>
///
/// </summary>
- public ConnectionBuilder ConnectionBuilder { get; private set; }
+ public IConnectionBuilder ConnectionBuilder { get; private set; }
/// <summary>
///
@@ -8,23 +8,23 @@ public static class ConnectionProviderFactory
private static readonly object Lock = new object();
private static volatile IDictionary<string, IConnectionProvider> Providers = new Dictionary<string, IConnectionProvider>();
- public static IConnectionProvider Get(ConnectionBuilder connectionBuilder)
+ public static IConnectionProvider Get(IConnectionBuilder connectionBuilder)
{
lock(Lock)
{
IConnectionProvider provider;
- if (!Providers.TryGetValue(connectionBuilder.ConnectionString, out provider))
+ if (!Providers.TryGetValue(connectionBuilder.Uuid, out provider))
{
provider = CreateProvider(connectionBuilder);
- Providers.Add(connectionBuilder.ConnectionString, provider);
+ Providers.Add(connectionBuilder.Uuid, provider);
}
return provider;
}
}
- private static IConnectionProvider CreateProvider(ConnectionBuilder builder)
+ private static IConnectionProvider CreateProvider(IConnectionBuilder builder)
{
if (builder.Pooling)
return new PooledConnectionProvider(builder);
@@ -0,0 +1,31 @@
+using System;
+using Apache.Cassandra;
+using System.Collections.Generic;
+
+namespace FluentCassandra.Connections
+{
+ public interface IConnectionBuilder
+ {
+ string Keyspace { get; }
+ IList<Server> Servers { get; }
+
+ bool Pooling { get; }
+ int MinPoolSize { get; }
+ int MaxPoolSize { get; }
+ TimeSpan ConnectionTimeout { get; }
+ TimeSpan ConnectionLifetime { get; }
+ ConnectionType ConnectionType { get; }
+
+ int BufferSize { get; }
+ ConsistencyLevel ReadConsistency { get; }
+ ConsistencyLevel WriteConsistency { get; }
+
+ string CqlVersion { get; }
+ bool CompressCqlQueries { get; }
+
+ string Username { get; }
+ string Password { get; }
+
+ string Uuid { get; }
+ }
+}
@@ -4,7 +4,7 @@ namespace FluentCassandra.Connections
{
public interface IConnectionProvider
{
- ConnectionBuilder ConnectionBuilder { get; }
+ IConnectionBuilder ConnectionBuilder { get; }
IServerManager Servers { get; }
@@ -9,7 +9,7 @@ public class NormalConnectionProvider : ConnectionProvider
///
/// </summary>
/// <param name="builder"></param>
- public NormalConnectionProvider(ConnectionBuilder builder)
+ public NormalConnectionProvider(IConnectionBuilder builder)
: base(builder)
{
if (builder.Servers.Count > 1 && builder.ConnectionTimeout == TimeSpan.Zero)
@@ -16,7 +16,7 @@ public class PooledConnectionProvider : NormalConnectionProvider
///
/// </summary>
/// <param name="builder"></param>
- public PooledConnectionProvider(ConnectionBuilder builder)
+ public PooledConnectionProvider(IConnectionBuilder builder)
: base(builder)
{
MinPoolSize = builder.MinPoolSize;
@@ -12,7 +12,7 @@ public class RoundRobinServerManager : IServerManager
private Queue<Server> _serverQueue;
private HashSet<Server> _blackListed;
- public RoundRobinServerManager(ConnectionBuilder builder)
+ public RoundRobinServerManager(IConnectionBuilder builder)
{
_servers = new List<Server>(builder.Servers);
_serverQueue = new Queue<Server>(_servers);
@@ -111,6 +111,7 @@
<Compile Include="Connections\ConnectionProviderFactory.cs" />
<Compile Include="Connections\ConnectionType.cs" />
<Compile Include="Connections\CqlVersion.cs" />
+ <Compile Include="Connections\IConnectionBuilder.cs" />
<Compile Include="Connections\NormalConnectionProvider.cs" />
<Compile Include="Connections\IConnection.cs" />
<Compile Include="Connections\IConnectionProvider.cs" />

0 comments on commit 5525512

Please sign in to comment.