Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 246 lines (204 sloc) 6.491 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
using System;
using System.Collections.Generic;
using System.Linq;
using FluentCassandra.Apache.Cassandra;
using FluentCassandra.Connections;
using FluentCassandra.Operations;

namespace FluentCassandra
{
public class CassandraSession : IDisposable
{
private IConnection _connection;

/// <summary>
///
/// </summary>
/// <param name="keyspace"></param>
/// <param name="server"></param>
/// <param name="timeout"></param>
public CassandraSession(string keyspace, Server server, string username = null, string password = null)
: this(keyspace, server.Host, server.Port, server.Timeout, username, password) { }

/// <summary>
///
/// </summary>
/// <param name="keyspace"></param>
/// <param name="host"></param>
/// <param name="port"></param>
/// <param name="timeout"></param>
/// <param name="provider"></param>
public CassandraSession(string keyspace, string host, int port = Server.DefaultPort, int timeout = Server.DefaultTimeout, string username = null, string password = null)
: this(new ConnectionBuilder(keyspace, host, port, timeout, username: username, password: password)) { }

/// <summary>
///
/// </summary>
/// <param name="connectionString"></param>
public CassandraSession(string connectionString)
: this(new ConnectionBuilder(connectionString)) { }

/// <summary>
///
/// </summary>
/// <param name="connectionBuilder"></param>
public CassandraSession(IConnectionBuilder connectionBuilder)
: this(ConnectionProviderFactory.Get(connectionBuilder), connectionBuilder.ReadConsistency, connectionBuilder.WriteConsistency) { }

/// <summary>
///
/// </summary>
/// <param name="connectionProvider"></param>
/// <param name="read"></param>
/// <param name="write"></param>
public CassandraSession(IConnectionProvider connectionProvider, ConsistencyLevel read, ConsistencyLevel write)
{
if (connectionProvider == null)
throw new ArgumentNullException("connectionProvider");

ConnectionBuilder = connectionProvider.ConnectionBuilder;
ConnectionProvider = connectionProvider;
ReadConsistency = read;
WriteConsistency = write;

IsAuthenticated = false;
}

/// <summary>
/// The connection builder that is currently in use for this session.
/// </summary>
public IConnectionBuilder ConnectionBuilder { get; private set; }

/// <summary>
///
/// </summary>
public IConnectionProvider ConnectionProvider { get; private set; }

/// <summary>
///
/// </summary>
public ConsistencyLevel ReadConsistency { get; private set; }

/// <summary>
///
/// </summary>
public ConsistencyLevel WriteConsistency { get; private set; }

/// <summary>
///
/// </summary>
public bool IsAuthenticated { get; private set; }

/// <summary>
///
/// </summary>
/// <param name="setKeyspace"></param>
/// <returns></returns>
internal CassandraClientWrapper GetClient(bool setKeyspace = true, bool? setCqlVersion = null)
{
var builder = ConnectionProvider.ConnectionBuilder;
setCqlVersion = setCqlVersion ?? (builder.CqlVersion != null);

if (_connection == null)
_connection = ConnectionProvider.Open();

if (!_connection.IsOpen)
_connection.Open();

if (setKeyspace)
_connection.SetKeyspace(builder.Keyspace);

if (setCqlVersion.Value)
_connection.SetCqlVersion(builder.CqlVersion);

if (!String.IsNullOrWhiteSpace(builder.Username) && !String.IsNullOrWhiteSpace(builder.Password))
Login(builder.Username, builder.Password);

return new CassandraClientWrapper(_connection.Client);
}

internal void MarkCurrentConnectionAsUnhealthy(Exception exc)
{
ConnectionProvider.ErrorOccurred(_connection, exc);
_connection = null;
}

/// <summary>
///
/// </summary>
public void Login()
{
var builder = ConnectionProvider.ConnectionBuilder;

if (String.IsNullOrWhiteSpace(builder.Username) || String.IsNullOrWhiteSpace(builder.Password))
throw new CassandraException("No username and/or password was set in the connection string, please use Login(username, password) method.");

Login(builder.Username, builder.Password);
}

/// <summary>
///
/// </summary>
/// <param name="username"></param>
/// <param name="password"></param>
public void Login(string username, string password)
{
var auth = new AuthenticationRequest {
Credentials = new Dictionary<string, string> { { "username", username }, { "password", password } }
};

try
{
GetClient().login(auth);
IsAuthenticated = true;
}
catch (Exception exc)
{
IsAuthenticated = false;
throw new CassandraException("Login failed.", exc);
}
}

/// <summary>
/// The last error that occurred during the execution of an operation.
/// </summary>
public CassandraException LastError { get; private set; }

/// <summary>
/// Indicates if errors should be thrown when occurring on operation.
/// </summary>
public bool ThrowErrors { get; set; }

/// <summary>
/// Execute the column family operation against the connection to the server.
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="action"></param>
/// <param name="throwOnError"></param>
/// <returns></returns>
public TResult ExecuteOperation<TResult>(Operation<TResult> action, bool? throwOnError = null)
{
if (WasDisposed)
throw new ObjectDisposedException(GetType().FullName);

if (!throwOnError.HasValue)
throwOnError = ThrowErrors;

LastError = null;
action.Session = this;

TResult result;
bool success = action.TryExecute(out result);

if (!success)
LastError = action.Error;

if (!success && (throwOnError ?? ThrowErrors))
throw action.Error;

return result;
}

#region IDisposable Members

/// <summary>
///
/// </summary>
public bool WasDisposed
{
get;
private set;
}

/// <summary>
///
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

/// <summary>
/// The dispose.
/// </summary>
/// <param name="disposing">
/// The disposing.
/// </param>
protected virtual void Dispose(bool disposing)
{
if (!WasDisposed && disposing && _connection != null)
ConnectionProvider.Close(_connection);

WasDisposed = true;
}

/// <summary>
/// Finalizes an instance of the <see cref="CassandraSession"/> class.
/// </summary>
~CassandraSession()
{
Dispose(false);
}

#endregion
}
}
Something went wrong with that request. Please try again.