Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

got rid of the CassandraSession.Current since it was an anti-pattern …

…that didn't enforce best practices, and caused issues in threading environments
  • Loading branch information...
commit bfe118e1196d72dcc53ac1f279050cf2d80effb9 1 parent 8876f7e
@nberardi nberardi authored
View
35 src/BaseCassandraColumnFamily.cs
@@ -10,7 +10,7 @@ namespace FluentCassandra
/// <seealso href="http://wiki.apache.org/cassandra/API"/>
public abstract class BaseCassandraColumnFamily
{
- private CassandraContext _context;
+ private readonly CassandraContext _context;
/// <summary>
///
@@ -21,7 +21,6 @@ public BaseCassandraColumnFamily(CassandraContext context, string columnFamily)
{
_context = context;
FamilyName = columnFamily;
- ThrowErrors = context.ThrowErrors;
}
/// <summary>
@@ -35,16 +34,6 @@ public BaseCassandraColumnFamily(CassandraContext context, string columnFamily)
public string FamilyName { get; private set; }
/// <summary>
- /// The last error that occured during the execution of an operation.
- /// </summary>
- public CassandraException LastError { get; private set; }
-
- /// <summary>
- /// Indicates if errors should be thrown when occuring on opperation.
- /// </summary>
- public bool ThrowErrors { get; set; }
-
- /// <summary>
/// Verifies that the family passed in is part of this family.
/// </summary>
/// <param name="family"></param>
@@ -109,29 +98,9 @@ public void RemoveAllRows()
/// <returns></returns>
public TResult ExecuteOperation<TResult>(ColumnFamilyOperation<TResult> action, bool? throwOnError = null)
{
- if (!throwOnError.HasValue)
- throwOnError = ThrowErrors;
-
- var localSession = CassandraSession.Current == null;
- var session = CassandraSession.Current;
- if (session == null)
- session = _context.OpenSession();
-
- action.Context = _context;
action.ColumnFamily = this;
- try
- {
- var result = session.ExecuteOperation(action, throwOnError);
- LastError = session.LastError;
-
- return result;
- }
- finally
- {
- if (localSession && session != null)
- session.Dispose();
- }
+ return _context.ExecuteOperation(action, throwOnError);
}
public CassandraSlicePredicateQuery<TResult> CreateCassandraSlicePredicateQuery<TResult>(Expression expression)
View
7 src/CassandraColumnFamilyOperations.cs
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
-using FluentCassandra.Linq;
using FluentCassandra.Operations;
using FluentCassandra.Types;
@@ -128,11 +127,5 @@ public static CassandraSlicePredicateQuery<FluentColumnFamily> Get(this Cassandr
}
#endregion
-
- public static IEnumerable<ICqlRow> Cql(this CassandraColumnFamily family, UTF8Type cqlQuery)
- {
- var op = new ExecuteCqlQuery(cqlQuery, family.Context.ConnectionBuilder.CompressCqlQueries);
- return family.ExecuteOperation(op);
- }
}
}
View
61 src/CassandraContext.cs
@@ -11,7 +11,9 @@ namespace FluentCassandra
{
public class CassandraContext : IDisposable
{
- private IList<IFluentMutationTracker> _trackers;
+ private readonly IList<IFluentMutationTracker> _trackers;
+ private readonly ConnectionBuilder _connectionBuilder;
+ private CassandraSession _session;
/// <summary>
///
@@ -19,8 +21,8 @@ public class CassandraContext : IDisposable
/// <param name="keyspace"></param>
/// <param name="server"></param>
/// <param name="timeout"></param>
- public CassandraContext(string keyspace, Server server, string username = null, string password = null)
- : this(keyspace, server.Host, server.Port, server.Timeout, username, password) { }
+ public CassandraContext(string keyspace, Server server)
+ : this(keyspace, server.Host, server.Port, server.Timeout) { }
/// <summary>
///
@@ -30,8 +32,8 @@ public CassandraContext(string keyspace, Server server, string username = null,
/// <param name="port"></param>
/// <param name="timeout"></param>
/// <param name="provider"></param>
- public CassandraContext(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)) { }
+ public CassandraContext(string keyspace, string host, int port = Server.DefaultPort, int timeout = Server.DefaultTimeout)
+ : this(new ConnectionBuilder(keyspace, host, port, timeout)) { }
/// <summary>
///
@@ -43,15 +45,25 @@ public CassandraContext(string connectionString)
/// <summary>
///
/// </summary>
+ /// <param name="session"></param>
+ public CassandraContext(CassandraSession session)
+ : this(session.ConnectionBuilder)
+ {
+ _session = session;
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
/// <param name="connectionBuilder"></param>
public CassandraContext(ConnectionBuilder connectionBuilder)
{
ThrowErrors = true;
_trackers = new List<IFluentMutationTracker>();
+ _connectionBuilder = connectionBuilder;
- ConnectionBuilder = connectionBuilder;
- Keyspace = new CassandraKeyspace(connectionBuilder.Keyspace, this);
+ Keyspace = new CassandraKeyspace(_connectionBuilder.Keyspace, this);
}
/// <summary>
@@ -265,7 +277,7 @@ public void SaveChanges(IFluentRecord record)
/// <param name="cqlQuery"></param>
public IEnumerable<ICqlRow> ExecuteQuery(UTF8Type cqlQuery)
{
- var op = new ExecuteCqlQuery(cqlQuery, ConnectionBuilder.CompressCqlQueries);
+ var op = new ExecuteCqlQuery(cqlQuery);
return ExecuteOperation(op);
}
@@ -275,25 +287,11 @@ public IEnumerable<ICqlRow> ExecuteQuery(UTF8Type cqlQuery)
/// <param name="cqlQuery"></param>
public void ExecuteNonQuery(UTF8Type cqlQuery)
{
- var op = new ExecuteCqlNonQuery(cqlQuery, ConnectionBuilder.CompressCqlQueries);
+ var op = new ExecuteCqlNonQuery(cqlQuery);
ExecuteOperation(op);
}
/// <summary>
- /// Open a session against the database.
- /// </summary>
- /// <returns></returns>
- public CassandraSession OpenSession()
- {
- return new CassandraSession(ConnectionBuilder);
- }
-
- /// <summary>
- /// The connection builder that is currently in use for this context.
- /// </summary>
- public ConnectionBuilder ConnectionBuilder { get; private set; }
-
- /// <summary>
/// The last error that occured during the execution of an operation.
/// </summary>
public CassandraException LastError { get; private set; }
@@ -315,19 +313,16 @@ public TResult ExecuteOperation<TResult>(Operation<TResult> action, bool? throwO
if (WasDisposed)
throw new ObjectDisposedException(GetType().FullName);
- if (!throwOnError.HasValue)
- throwOnError = ThrowErrors;
-
- var localSession = CassandraSession.Current == null;
- var session = CassandraSession.Current;
+ var localSession = _session == null;
+ var session = _session;
if (session == null)
- session = OpenSession();
+ session = new CassandraSession(_connectionBuilder);
action.Context = this;
try
{
- var result = session.ExecuteOperation(action, throwOnError);
+ var result = session.ExecuteOperation(action, throwOnError ?? ThrowErrors);
LastError = session.LastError;
return result;
@@ -367,10 +362,10 @@ public void Dispose()
/// </param>
protected virtual void Dispose(bool disposing)
{
- if (!WasDisposed && disposing && CassandraSession.Current != null)
+ if (!WasDisposed && disposing && _session != null)
{
- CassandraSession.Current.Dispose();
- CassandraSession.Current = null;
+ _session.Dispose();
+ _session = null;
}
WasDisposed = true;
View
11 src/CassandraKeyspace.cs
@@ -11,8 +11,9 @@ namespace FluentCassandra
public class CassandraKeyspace
{
private readonly string _keyspaceName;
+ private readonly CassandraContext _context;
+
private CassandraKeyspaceSchema _cachedSchema;
- private CassandraContext _context;
public CassandraKeyspace(string keyspaceName, CassandraContext context)
{
@@ -64,8 +65,8 @@ public void TryCreateSelf()
}
catch(Exception exc)
{
- Debug.WriteLine(exc);
- Debug.WriteLine(schema.Name + " already exists", "keyspace setup");
+ if (_context.ThrowErrors)
+ throw exc;
}
}
@@ -80,8 +81,8 @@ public void TryCreateColumnFamily(CassandraColumnFamilySchema schema)
}
catch (Exception exc)
{
- Debug.WriteLine(exc);
- Debug.WriteLine(schema.FamilyName + " already exists", "column family setup");
+ if (_context.ThrowErrors)
+ throw exc;
}
}
View
72 src/CassandraSession.cs
@@ -9,37 +9,67 @@ namespace FluentCassandra
{
public class CassandraSession : IDisposable
{
- [ThreadStatic]
- private static CassandraSession _current;
+ private IConnection _connection;
- public static CassandraSession Current
- {
- get { return _current; }
- internal set { _current = value; }
- }
+ /// <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) { }
- private IConnection _connection;
+ /// <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(ConnectionBuilder connectionBuilder)
: this(ConnectionProviderFactory.Get(connectionBuilder), connectionBuilder.ReadConsistency, connectionBuilder.WriteConsistency) { }
- public CassandraSession(ConnectionBuilder connectionBuilder, ConsistencyLevel read, ConsistencyLevel write)
- : this(ConnectionProviderFactory.Get(connectionBuilder), read, write) { }
-
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="connectionProvider"></param>
+ /// <param name="read"></param>
+ /// <param name="write"></param>
public CassandraSession(IConnectionProvider connectionProvider, ConsistencyLevel read, ConsistencyLevel write)
{
- if (Current != null)
- throw new CassandraException("Cannot create a new session while there is one already active.");
+ if (connectionProvider == null)
+ throw new ArgumentNullException("connectionProvider");
+ ConnectionBuilder = connectionProvider.ConnectionBuilder;
ConnectionProvider = connectionProvider;
ReadConsistency = read;
WriteConsistency = write;
IsAuthenticated = false;
- Current = this;
}
/// <summary>
+ /// The connection builder that is currently in use for this session.
+ /// </summary>
+ public ConnectionBuilder ConnectionBuilder { get; private set; }
+
+ /// <summary>
///
/// </summary>
public IConnectionProvider ConnectionProvider { get; private set; }
@@ -66,7 +96,7 @@ public CassandraSession(IConnectionProvider connectionProvider, ConsistencyLevel
/// <returns></returns>
internal CassandraClientWrapper GetClient(bool setKeyspace = true, bool? setCqlVersion = null)
{
- var builder = ConnectionProvider.Builder;
+ var builder = ConnectionProvider.ConnectionBuilder;
setCqlVersion = setCqlVersion ?? (builder.CqlVersion != null);
if (_connection == null)
@@ -92,7 +122,7 @@ internal CassandraClientWrapper GetClient(bool setKeyspace = true, bool? setCqlV
/// </summary>
public void Login()
{
- var builder = ConnectionProvider.Builder;
+ 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.");
@@ -191,14 +221,8 @@ public void Dispose()
/// </param>
protected virtual void Dispose(bool disposing)
{
- if (!WasDisposed && disposing)
- {
- if (_connection != null)
- ConnectionProvider.Close(_connection);
-
- if (Current == this)
- Current = null;
- }
+ if (!WasDisposed && disposing && _connection != null)
+ ConnectionProvider.Close(_connection);
WasDisposed = true;
}
View
6 src/Connections/ConnectionBuilder.cs
@@ -14,7 +14,7 @@ public class ConnectionBuilder
/// <param name="host"></param>
/// <param name="port"></param>
/// <param name="timeout"></param>
- public ConnectionBuilder(string keyspace, string host, int port = Server.DefaultPort, int connectionTimeout = Server.DefaultTimeout, bool pooling = false, int minPoolSize = 0, int maxPoolSize = 100, int connectionLifetime = 0, ConnectionType connectionType = ConnectionType.Framed, int bufferSize = 1024, ConsistencyLevel read = ConsistencyLevel.QUORUM, ConsistencyLevel write = ConsistencyLevel.QUORUM, string cqlVersion = FluentCassandra.Connections.CqlVersion.ServerDefault, bool compressCqlQueries = false, string username = null, string password = null)
+ public ConnectionBuilder(string keyspace, string host, int port = Server.DefaultPort, int connectionTimeout = Server.DefaultTimeout, bool pooling = false, int minPoolSize = 0, int maxPoolSize = 100, int connectionLifetime = 0, ConnectionType connectionType = ConnectionType.Framed, int bufferSize = 1024, ConsistencyLevel read = ConsistencyLevel.QUORUM, ConsistencyLevel write = ConsistencyLevel.QUORUM, string cqlVersion = FluentCassandra.Connections.CqlVersion.ServerDefault, bool compressCqlQueries = true, string username = null, string password = null)
{
Keyspace = keyspace;
Servers = new List<Server>() { new Server(host, port) };
@@ -35,7 +35,7 @@ public ConnectionBuilder(string keyspace, string host, int port = Server.Default
ConnectionString = GetConnectionString();
}
- public ConnectionBuilder(string keyspace, Server server, bool pooling = false, int minPoolSize = 0, int maxPoolSize = 100, int connectionLifetime = 0, ConnectionType connectionType = ConnectionType.Framed, int bufferSize = 1024, ConsistencyLevel read = ConsistencyLevel.QUORUM, ConsistencyLevel write = ConsistencyLevel.QUORUM, string cqlVersion = FluentCassandra.Connections.CqlVersion.ServerDefault, bool compressCqlQueries = false, string username = null, string password = null)
+ public ConnectionBuilder(string keyspace, Server server, bool pooling = false, int minPoolSize = 0, int maxPoolSize = 100, int connectionLifetime = 0, ConnectionType connectionType = ConnectionType.Framed, int bufferSize = 1024, ConsistencyLevel read = ConsistencyLevel.QUORUM, ConsistencyLevel write = ConsistencyLevel.QUORUM, string cqlVersion = FluentCassandra.Connections.CqlVersion.ServerDefault, bool compressCqlQueries = true, string username = null, string password = null)
{
Keyspace = keyspace;
Servers = new List<Server>() { server };
@@ -316,7 +316,7 @@ private void InitializeConnectionString(string connectionString)
if (!pairs.ContainsKey("Compress CQL Queries"))
{
- CompressCqlQueries = false;
+ CompressCqlQueries = true;
}
else
{
View
4 src/Connections/ConnectionProvider.cs
@@ -10,14 +10,14 @@ public abstract class ConnectionProvider : IConnectionProvider
/// <param name="builder"></param>
protected ConnectionProvider(ConnectionBuilder builder)
{
- Builder = builder;
+ ConnectionBuilder = builder;
Servers = new RoundRobinServerManager(builder);
}
/// <summary>
///
/// </summary>
- public ConnectionBuilder Builder { get; private set; }
+ public ConnectionBuilder ConnectionBuilder { get; private set; }
/// <summary>
///
View
2  src/Connections/IConnectionProvider.cs
@@ -4,7 +4,7 @@ namespace FluentCassandra.Connections
{
public interface IConnectionProvider
{
- ConnectionBuilder Builder { get; }
+ ConnectionBuilder ConnectionBuilder { get; }
IServerManager Servers { get; }
View
2  src/Connections/NormalConnectionProvider.cs
@@ -63,7 +63,7 @@ public override IConnection CreateConnection()
return null;
var server = Servers.Next();
- var conn = new Connection(server, Builder);
+ var conn = new Connection(server, ConnectionBuilder);
return conn;
}
View
2  src/Linq/CqlObjectQuery.cs
@@ -57,7 +57,7 @@ IEnumerator IEnumerable.GetEnumerator()
public IEnumerator<T> GetEnumerator()
{
var result = CqlQueryEvaluator.GetCql(Expression, _family.ObjectConventions);
- var fluentObjects = _family.Cql(result);
+ var fluentObjects = _family.Context.ExecuteQuery(result);
var serializer = ObjectSerializerFactory.Get(typeof(T));
return serializer
View
2  src/Linq/CqlQuery.cs
@@ -46,7 +46,7 @@ IEnumerator IEnumerable.GetEnumerator()
public IEnumerator<ICqlRow> GetEnumerator()
{
var result = CqlQueryEvaluator.GetCql(Expression);
- return Provider.Cql(result).GetEnumerator();
+ return Provider.Context.ExecuteQuery(result).GetEnumerator();
}
#endregion
View
5 src/Operations/ExecuteCqlNonQuery.cs
@@ -15,7 +15,7 @@ public override Void Execute()
{
Debug.Write(CqlQuery.ToString(), "query");
byte[] query = CqlQuery;
- bool isCqlQueryCompressed = query.Length > 200 && CompressCqlQuery;
+ bool isCqlQueryCompressed = query.Length > 200 && Session.ConnectionBuilder.CompressCqlQueries;
// it doesn't make sense to compress queryies that are really small
if (isCqlQueryCompressed)
@@ -29,10 +29,9 @@ public override Void Execute()
return new Void();
}
- public ExecuteCqlNonQuery(UTF8Type cqlQuery, bool compressCqlQuery)
+ public ExecuteCqlNonQuery(UTF8Type cqlQuery)
{
CqlQuery = cqlQuery;
- CompressCqlQuery = compressCqlQuery;
}
}
}
View
8 src/Operations/ExecuteCqlQuery.cs
@@ -14,8 +14,6 @@ public class ExecuteCqlQuery : ColumnFamilyOperation<IEnumerable<ICqlRow>>
public UTF8Type CqlQuery { get; private set; }
- public bool CompressCqlQuery { get; private set; }
-
private CassandraColumnFamilySchema TryGetSchema(Apache.Cassandra.CqlResult result, string familyName)
{
//if (ColumnFamily != null && ColumnFamily.FamilyName != null)
@@ -65,8 +63,9 @@ private string TryGetFamilyName()
public override IEnumerable<ICqlRow> Execute()
{
Debug.Write(CqlQuery.ToString(), "query");
+
byte[] query = CqlQuery;
- bool isCqlQueryCompressed = query.Length > 200 && CompressCqlQuery;
+ bool isCqlQueryCompressed = query.Length > 200 && Session.ConnectionBuilder.CompressCqlQueries;
// it doesn't make sense to compress queryies that are really small
if (isCqlQueryCompressed)
@@ -106,10 +105,9 @@ private IEnumerable<FluentColumn> GetColumns(Apache.Cassandra.CqlRow row, Cassan
}
}
- public ExecuteCqlQuery(UTF8Type cqlQuery, bool compressCqlQuery)
+ public ExecuteCqlQuery(UTF8Type cqlQuery)
{
CqlQuery = cqlQuery;
- CompressCqlQuery = compressCqlQuery;
}
}
}
View
3  src/Types/BytesTypeConverter.cs
@@ -210,6 +210,9 @@ public override object ConvertToInternal(byte[] value, Type destinationType)
case TypeCode.Boolean:
return BitConverter.ToBoolean(bytes, 0);
case TypeCode.Char:
+ if (bytes.Length < 2)
+ Array.Resize<byte>(ref bytes, 2);
+
return BitConverter.ToChar(bytes, 0);
case TypeCode.Double:
return BitConverter.ToDouble(bytes, 0);
View
2  src/Types/CassandraType.cs
@@ -268,7 +268,7 @@ public static CassandraType GetCassandraType(Type sourceType)
case TypeCode.Byte:
case TypeCode.SByte:
- destinationType = BytesType;
+ destinationType = Int32Type;
break;
default:
View
139 test/FluentCassandra.Tests/CassandraDatabaseSetup.cs
@@ -8,12 +8,20 @@ namespace FluentCassandra
{
public class CassandraDatabaseSetup
{
+ public ConnectionBuilder ConnectionBuilder;
public CassandraContext DB;
+
public CassandraColumnFamily<AsciiType> Family;
public CassandraSuperColumnFamily<AsciiType, AsciiType> SuperFamily;
public CassandraColumnFamily UserFamily;
- public User[] Users;
+ public User[] Users = new[] {
+ new User { Id = 1, Name = "Darren Gemmell", Email = "darren@somewhere.com", Age = 32 },
+ new User { Id = 2, Name = "Fernando Laubscher", Email = "fernando@somewhere.com", Age = 23 },
+ new User { Id = 3, Name = "Cody Millhouse", Email = "cody@somewhere.com", Age = 56 },
+ new User { Id = 4, Name = "Emilia Thibert", Email = "emilia@somewhere.com", Age = 67 },
+ new User { Id = 5, Name = "Allyson Schurr", Email = "allyson@somewhere.com", Age = 21 }
+ };
public class User
{
@@ -29,22 +37,15 @@ public class User
public const string TestStandardName = "Test1";
public const string TestSuperName = "SubTest1";
+ public const string Keyspace = "Testing";
+ public static readonly Server Server = new Server("localhost");
+
public CassandraDatabaseSetup(bool reset = false)
{
- var keyspaceName = "Testing";
- var server = new Server("localhost");
-
- DB = new CassandraContext(keyspaceName, server);
- DB.ThrowErrors = true;
+ ConnectionBuilder = new ConnectionBuilder(keyspace: Keyspace, server: Server);
+ DB = new CassandraContext(ConnectionBuilder);
- var exists = DB.KeyspaceExists(keyspaceName);
- Users = new[] {
- new User { Id = 1, Name = "Darren Gemmell", Email = "darren@somewhere.com", Age = 32 },
- new User { Id = 2, Name = "Fernando Laubscher", Email = "fernando@somewhere.com", Age = 23 },
- new User { Id = 3, Name = "Cody Millhouse", Email = "cody@somewhere.com", Age = 56 },
- new User { Id = 4, Name = "Emilia Thibert", Email = "emilia@somewhere.com", Age = 67 },
- new User { Id = 5, Name = "Allyson Schurr", Email = "allyson@somewhere.com", Age = 21 }
- };
+ var exists = DB.KeyspaceExists(Keyspace);
Family = DB.GetColumnFamily<AsciiType>("Standard");
SuperFamily = DB.GetColumnFamily<AsciiType, AsciiType>("Super");
@@ -53,15 +54,23 @@ public CassandraDatabaseSetup(bool reset = false)
if (exists && !reset)
return;
- using (var session = DB.OpenSession())
+ ResetDatabase();
+ }
+
+ public void ResetDatabase()
+ {
+ using (var session = new CassandraSession(ConnectionBuilder))
+ using (var db = new CassandraContext(session))
{
- if (exists)
- DB.DropKeyspace(keyspaceName);
+ db.ThrowErrors = true;
+
+ try { db.DropKeyspace(Keyspace); }
+ catch (Exception exc) { Console.WriteLine(exc); }
- var keyspace = new CassandraKeyspace(new CassandraKeyspaceSchema {
- Name = keyspaceName
- }, DB);
- DB.Keyspace = keyspace;
+ var keyspace = new CassandraKeyspace(new CassandraKeyspaceSchema {
+ Name = Keyspace
+ }, db);
+ db.Keyspace = keyspace;
keyspace.TryCreateSelf();
keyspace.TryCreateColumnFamily<AsciiType>("Standard");
@@ -83,60 +92,74 @@ public CassandraDatabaseSetup(bool reset = false)
ColumnNameType = CassandraType.DynamicCompositeType(new Dictionary<char, CassandraType> { { 'a', CassandraType.AsciiType }, { 'd', CassandraType.DoubleType } })
});
- Family = DB.GetColumnFamily<AsciiType>("Standard");
- SuperFamily = DB.GetColumnFamily<AsciiType, AsciiType>("Super");
-
- ResetFamily();
- ResetSuperFamily();
-
- DB.ExecuteNonQuery(@"
+ db.ExecuteNonQuery(@"
CREATE COLUMNFAMILY Users (
KEY int PRIMARY KEY,
Name ascii,
Email ascii,
Age int
);");
- DB.ExecuteNonQuery(@"CREATE INDEX User_Age ON Users (Age);");
- DB.Keyspace.ClearCachedKeyspaceSchema();
- UserFamily = DB.GetColumnFamily("Users");
-
- foreach (var user in Users)
- {
- dynamic record = UserFamily.CreateRecord(user.Id);
- record.Name = user.Name;
- record.Email = user.Email;
- record.Age = user.Age;
-
- DB.Attach(record);
- }
- DB.SaveChanges();
+ db.ExecuteNonQuery(@"CREATE INDEX User_Age ON Users (Age);");
+ db.Keyspace.ClearCachedKeyspaceSchema();
+
+ var family = db.GetColumnFamily<AsciiType>("Standard");
+ var superFamily = db.GetColumnFamily<AsciiType, AsciiType>("Super");
+ var userFamily = db.GetColumnFamily("Users");
+
+ ResetFamily(family);
+ ResetSuperFamily(superFamily);
+ ResetUsersFamily(userFamily);
}
}
- public void ResetFamily()
+ public void ResetUsersFamily(CassandraColumnFamily userFamily = null)
{
- Family.RemoveAllRows();
+ userFamily = userFamily ?? UserFamily;
- Family.InsertColumn(TestKey1, "Test1", Math.PI);
- Family.InsertColumn(TestKey1, "Test2", Math.PI);
- Family.InsertColumn(TestKey1, "Test3", Math.PI);
+ var db = userFamily.Context;
+ userFamily.RemoveAllRows();
- Family.InsertColumn(TestKey2, "Test1", Math.PI);
- Family.InsertColumn(TestKey2, "Test2", Math.PI);
- Family.InsertColumn(TestKey2, "Test3", Math.PI);
+ foreach (var user in Users)
+ {
+ dynamic record = userFamily.CreateRecord(user.Id);
+ record.Name = user.Name;
+ record.Email = user.Email;
+ record.Age = user.Age;
+
+ db.Attach(record);
+ }
+
+ db.SaveChanges();
+ }
+
+ public void ResetFamily(CassandraColumnFamily family = null)
+ {
+ family = family ?? Family;
+
+ family.RemoveAllRows();
+
+ family.InsertColumn(TestKey1, "Test1", Math.PI);
+ family.InsertColumn(TestKey1, "Test2", Math.PI);
+ family.InsertColumn(TestKey1, "Test3", Math.PI);
+
+ family.InsertColumn(TestKey2, "Test1", Math.PI);
+ family.InsertColumn(TestKey2, "Test2", Math.PI);
+ family.InsertColumn(TestKey2, "Test3", Math.PI);
}
- public void ResetSuperFamily()
+ public void ResetSuperFamily(CassandraSuperColumnFamily superFamily = null)
{
- SuperFamily.RemoveAllRows();
+ superFamily = superFamily ?? SuperFamily;
+
+ superFamily.RemoveAllRows();
- SuperFamily.InsertColumn(TestKey1, TestSuperName, "Test1", Math.PI);
- SuperFamily.InsertColumn(TestKey1, TestSuperName, "Test2", Math.PI);
- SuperFamily.InsertColumn(TestKey1, TestSuperName, "Test3", Math.PI);
+ superFamily.InsertColumn(TestKey1, TestSuperName, "Test1", Math.PI);
+ superFamily.InsertColumn(TestKey1, TestSuperName, "Test2", Math.PI);
+ superFamily.InsertColumn(TestKey1, TestSuperName, "Test3", Math.PI);
- SuperFamily.InsertColumn(TestKey2, TestSuperName, "Test1", Math.PI);
- SuperFamily.InsertColumn(TestKey2, TestSuperName, "Test2", Math.PI);
- SuperFamily.InsertColumn(TestKey2, TestSuperName, "Test3", Math.PI);
+ superFamily.InsertColumn(TestKey2, TestSuperName, "Test1", Math.PI);
+ superFamily.InsertColumn(TestKey2, TestSuperName, "Test2", Math.PI);
+ superFamily.InsertColumn(TestKey2, TestSuperName, "Test3", Math.PI);
}
}
}
View
22 test/FluentCassandra.Tests/Operations/CqlTest.cs
@@ -5,7 +5,6 @@
namespace FluentCassandra.Operations
{
-
public class CqlTest : IUseFixture<CassandraDatabaseSetupFixture>, IDisposable
{
private CassandraContext _db;
@@ -24,26 +23,15 @@ public void Dispose()
[Fact]
public void With_Compression()
{
- var cb = new ConnectionBuilder(
- keyspace: _db.ConnectionBuilder.Keyspace,
- host: _db.ConnectionBuilder.Servers[0].Host,
- port: _db.ConnectionBuilder.Servers[0].Port,
- connectionTimeout: _db.ConnectionBuilder.Servers[0].Timeout,
- compressCqlQueries: true
- );
-
// arrange
var insertQuery = @"INSERT INTO Users (KEY, Name, Email, Age) VALUES (23, '" + new String('X', 200) + "', 'test@test.com', 43)";
- using (var db = new CassandraContext(cb))
- {
- // act
- db.ExecuteNonQuery(insertQuery);
- var actual = db.ExecuteQuery("SELECT * FROM Users");
+ // act
+ _db.ExecuteNonQuery(insertQuery);
+ var actual = _db.ExecuteQuery("SELECT * FROM Users");
- // assert
- Assert.Equal(6, actual.Count());
- }
+ // assert
+ Assert.Equal(6, actual.Count());
}
}
}
View
7 utility/FluentCassandra.LinqPad/CassandraConnectionInfo.cs
@@ -147,13 +147,14 @@ public bool CanSave()
public FluentCassandra.CassandraContext CreateContext()
{
- return new FluentCassandra.CassandraContext(
+ return new FluentCassandra.CassandraContext(new ConnectionBuilder(
keyspace: Keyspace,
host: Host,
port: Port,
- timeout: Timeout,
+ connectionTimeout: Timeout,
username: Username,
- password: Password);
+ password: Password
+ ));
}
public static CassandraConnectionInfo Load(IConnectionInfo connectionInfo)
Please sign in to comment.
Something went wrong with that request. Please try again.