Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

cleaned up the use of sessions in the code

  • Loading branch information...
commit 2645bc73dcc22002412acd136fe83fce2673aec7 1 parent 90285fe
@nberardi nberardi authored
Showing with 363 additions and 472 deletions.
  1. +7 −4 src/BaseCassandraColumnFamily.cs
  2. +1 −1  src/CassandraColumnFamilyOperations.cs
  3. +29 −17 src/CassandraContext.cs
  4. +51 −8 src/CassandraSession.cs
  5. +1 −1  src/Connections/ConnectionBuilder.cs
  6. +2 −2 src/Operations/AddColumn.cs
  7. +2 −2 src/Operations/BatchMutate.cs
  8. +7 −19 src/Operations/CassandraClientWrapper.cs
  9. +2 −2 src/Operations/ColumnCount.cs
  10. +3 −3 src/Operations/ExecuteCqlNonQuery.cs
  11. +3 −3 src/Operations/ExecuteCqlQuery.cs
  12. +2 −2 src/Operations/GetColumn.cs
  13. +18 −30 src/Operations/GetColumnFamilyIndexedSlices.cs
  14. +18 −30 src/Operations/GetColumnFamilyRangeSlices.cs
  15. +14 −26 src/Operations/GetColumnFamilySlice.cs
  16. +2 −2 src/Operations/GetSuperColumn.cs
  17. +21 −33 src/Operations/GetSuperColumnFamilyIndexedSlices.cs
  18. +21 −33 src/Operations/GetSuperColumnFamilyRangeSlices.cs
  19. +16 −28 src/Operations/GetSuperColumnFamilySlice.cs
  20. +19 −32 src/Operations/GetSuperColumnIndexedSlices.cs
  21. +19 −31 src/Operations/GetSuperColumnRangeSlices.cs
  22. +14 −26 src/Operations/GetSuperColumnSlice.cs
  23. +2 −2 src/Operations/InsertColumn.cs
  24. +15 −27 src/Operations/MultiGetColumnCount.cs
  25. +21 −33 src/Operations/MultiGetColumnFamilySlice.cs
  26. +24 −36 src/Operations/MultiGetSuperColumnFamilySlice.cs
  27. +19 −31 src/Operations/MultiGetSuperColumnSlice.cs
  28. +3 −1 src/Operations/Operation.cs
  29. +2 −2 src/Operations/RemoveColumn.cs
  30. +2 −2 src/Operations/RemoveKey.cs
  31. +2 −2 src/Operations/RemoveSuperColumn.cs
  32. +1 −1  src/Operations/Truncate.cs
View
11 src/BaseCassandraColumnFamily.cs
@@ -70,9 +70,12 @@ public void RemoveAllRows()
/// <returns></returns>
public TResult ExecuteOperation<TResult>(ColumnFamilyOperation<TResult> action, bool? throwOnError = null)
{
- CassandraSession _localSession = null;
+ CassandraSession localSession = null;
if (CassandraSession.Current == null)
- _localSession = new CassandraSession();
+ localSession = _context.OpenSession();
+
+ action.Context = _context;
+ action.Session = localSession;
try
{
@@ -92,8 +95,8 @@ public TResult ExecuteOperation<TResult>(ColumnFamilyOperation<TResult> action,
}
finally
{
- if (_localSession != null)
- _localSession.Dispose();
+ if (localSession != null)
+ localSession.Dispose();
}
}
View
2  src/CassandraColumnFamilyOperations.cs
@@ -225,7 +225,7 @@ public static IEnumerable<IFluentColumnFamily<CompareWith>> Get<CompareWith>(thi
public static IEnumerable<ICqlRow<CompareWith>> Cql<CompareWith>(this CassandraColumnFamily<CompareWith> family, UTF8Type cqlQuery)
where CompareWith : CassandraType
{
- var op = new ExecuteCqlQuery<CompareWith>(cqlQuery);
+ var op = new ExecuteCqlQuery<CompareWith>(cqlQuery, family.Context.ConnectionBuilder.CompressCqlQueries);
return family.ExecuteOperation(op);
}
}
View
46 src/CassandraContext.cs
@@ -9,15 +9,7 @@ namespace FluentCassandra
{
public class CassandraContext : IDisposable
{
- [ThreadStatic]
- private static ConnectionBuilder _currentConnectionBuilder;
-
- public static ConnectionBuilder CurrentConnectionBuilder
- {
- get { return _currentConnectionBuilder; }
- internal set { _currentConnectionBuilder = value; }
- }
-
+ private readonly ConnectionBuilder _connectionBuilder;
private IList<IFluentMutationTracker> _trackers;
/// <summary>
@@ -53,9 +45,9 @@ public CassandraContext(string connectionString)
/// <param name="connectionBuilder"></param>
public CassandraContext(ConnectionBuilder connectionBuilder)
{
- CurrentConnectionBuilder = connectionBuilder;
ThrowErrors = true;
-
+
+ _connectionBuilder = connectionBuilder;
_trackers = new List<IFluentMutationTracker>();
}
@@ -137,7 +129,7 @@ public void SaveChanges(IFluentRecord record)
/// <param name="cqlQuery"></param>
public IEnumerable<ICqlRow<BytesType>> ExecuteQuery(UTF8Type cqlQuery)
{
- var op = new ExecuteCqlQuery<BytesType>(cqlQuery);
+ var op = new ExecuteCqlQuery<BytesType>(cqlQuery, ConnectionBuilder.CompressCqlQueries);
return ExecuteOperation(op);
}
@@ -147,11 +139,25 @@ public IEnumerable<ICqlRow<BytesType>> ExecuteQuery(UTF8Type cqlQuery)
/// <param name="cqlQuery"></param>
public void ExecuteNonQuery(UTF8Type cqlQuery)
{
- var op = new ExecuteCqlNonQuery(cqlQuery);
+ var op = new ExecuteCqlNonQuery(cqlQuery, ConnectionBuilder.CompressCqlQueries);
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; }
@@ -173,9 +179,12 @@ public TResult ExecuteOperation<TResult>(Operation<TResult> action, bool? throwO
if (!throwOnError.HasValue)
throwOnError = ThrowErrors;
- CassandraSession _localSession = null;
+ CassandraSession localSession = null;
if (CassandraSession.Current == null)
- _localSession = new CassandraSession();
+ localSession = OpenSession();
+
+ action.Context = this;
+ action.Session = localSession;
try
{
@@ -194,13 +203,16 @@ public TResult ExecuteOperation<TResult>(Operation<TResult> action, bool? throwO
}
finally
{
- if (_localSession != null)
- _localSession.Dispose();
+ if (localSession != null)
+ localSession.Dispose();
}
}
#region IDisposable Members
+ /// <summary>
+ ///
+ /// </summary>
public bool WasDisposed
{
get;
View
59 src/CassandraSession.cs
@@ -109,17 +109,13 @@ public static string DescribeSnitch(Server server)
#endregion
- private bool _disposed;
private IConnection _connection;
- public CassandraSession()
- : this(CassandraContext.CurrentConnectionBuilder) { }
-
public CassandraSession(ConnectionBuilder connectionBuilder)
: this(ConnectionProviderFactory.Get(connectionBuilder), connectionBuilder.ReadConsistency, connectionBuilder.WriteConsistency) { }
- public CassandraSession(ConsistencyLevel read, ConsistencyLevel write)
- : this(ConnectionProviderFactory.Get(CassandraContext.CurrentConnectionBuilder), read, write) { }
+ public CassandraSession(ConnectionBuilder connectionBuilder, ConsistencyLevel read, ConsistencyLevel write)
+ : this(ConnectionProviderFactory.Get(connectionBuilder), read, write) { }
public CassandraSession(IConnectionProvider connectionProvider, ConsistencyLevel read, ConsistencyLevel write)
{
@@ -135,16 +131,36 @@ public CassandraSession(IConnectionProvider connectionProvider, ConsistencyLevel
Current = this;
}
+ /// <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 CassandraKeyspace Keyspace { 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)
{
if (_connection == null)
@@ -159,6 +175,24 @@ internal CassandraClientWrapper GetClient(bool setKeyspace = true)
return new CassandraClientWrapper(_connection.Client);
}
+ /// <summary>
+ ///
+ /// </summary>
+ public void Login()
+ {
+ var builder = ConnectionProvider.Builder;
+
+ 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 {
@@ -182,6 +216,15 @@ public void Login(string username, string password)
/// <summary>
///
/// </summary>
+ public bool WasDisposed
+ {
+ get;
+ private set;
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
public void Dispose()
{
Dispose(true);
@@ -196,7 +239,7 @@ public void Dispose()
/// </param>
protected virtual void Dispose(bool disposing)
{
- if (!_disposed && disposing)
+ if (!WasDisposed && disposing)
{
if (_connection != null)
ConnectionProvider.Close(_connection);
@@ -205,7 +248,7 @@ protected virtual void Dispose(bool disposing)
Current = null;
}
- _disposed = true;
+ WasDisposed = true;
}
/// <summary>
View
2  src/Connections/ConnectionBuilder.cs
@@ -330,7 +330,7 @@ private string GetConnectionString()
b.AppendFormat(format, "Read", ReadConsistency);
b.AppendFormat(format, "Write", WriteConsistency);
b.AppendFormat(format, "Username", Username);
- b.AppendFormat(format, "Password", Password == null ? "" : new String('X', Password.Length));
+ b.AppendFormat(format, "Password", Password);
return b.ToString();
}
View
4 src/Operations/AddColumn.cs
@@ -29,11 +29,11 @@ public override Void Execute()
Value = ColumnValue
};
- CassandraSession.Current.GetClient().add(
+ Session.GetClient().add(
Key,
parent,
column,
- CassandraSession.Current.WriteConsistency
+ Session.WriteConsistency
);
return new Void();
View
4 src/Operations/BatchMutate.cs
@@ -50,9 +50,9 @@ public override Void Execute()
}
// Dictionary<string : key, Dicationary<string : columnFamily, List<Mutation>>>
- CassandraSession.Current.GetClient().batch_mutate(
+ Session.GetClient().batch_mutate(
mutationMap,
- CassandraSession.Current.WriteConsistency
+ Session.WriteConsistency
);
return new Void();
View
26 src/Operations/CassandraClientWrapper.cs
@@ -23,25 +23,13 @@ public CassandraClientWrapper(Apache.Cassandra.Cassandra.Iface client)
/// <returns></returns>
private TResult ExecuteOperation<TResult>(Operation<TResult> action)
{
- CassandraSession _localSession = null;
- if (CassandraSession.Current == null)
- _localSession = new CassandraSession();
-
- try
- {
- TResult result;
- bool success = action.TryExecute(out result);
-
- if (!success)
- throw action.Error;
-
- return result;
- }
- finally
- {
- if (_localSession != null)
- _localSession.Dispose();
- }
+ TResult result;
+ bool success = action.TryExecute(out result);
+
+ if (!success)
+ throw action.Error;
+
+ return result;
}
#region Iface Members
View
4 src/Operations/ColumnCount.cs
@@ -27,11 +27,11 @@ public override int Execute()
if (SuperColumnName != null)
parent.SuperColumn = SuperColumnName;
- var result = CassandraSession.Current.GetClient().get_count(
+ var result = Session.GetClient().get_count(
Key,
parent,
SlicePredicate,
- CassandraSession.Current.ReadConsistency
+ Session.ReadConsistency
);
return result;
View
6 src/Operations/ExecuteCqlNonQuery.cs
@@ -21,7 +21,7 @@ public override Void Execute()
if (CompressCqlQuery)
query = GzipCompress(query);
- var result = CassandraSession.Current.GetClient().execute_cql_query(
+ var result = Session.GetClient().execute_cql_query(
query,
CompressCqlQuery ? Apache.Cassandra.Compression.GZIP : Apache.Cassandra.Compression.NONE
);
@@ -39,10 +39,10 @@ private byte[] GzipCompress(byte[] cqlQuery)
}
}
- public ExecuteCqlNonQuery(UTF8Type cqlQuery, bool? compressCqlQuery = null)
+ public ExecuteCqlNonQuery(UTF8Type cqlQuery, bool compressCqlQuery)
{
CqlQuery = cqlQuery;
- CompressCqlQuery = compressCqlQuery ?? CassandraContext.CurrentConnectionBuilder.CompressCqlQueries;
+ CompressCqlQuery = compressCqlQuery;
}
}
}
View
6 src/Operations/ExecuteCqlQuery.cs
@@ -40,7 +40,7 @@ public override IEnumerable<ICqlRow<CompareWith>> Execute()
if (CompressCqlQuery)
query = GzipCompress(query);
- var result = CassandraSession.Current.GetClient().execute_cql_query(
+ var result = Session.GetClient().execute_cql_query(
query,
CompressCqlQuery ? Apache.Cassandra.Compression.GZIP : Apache.Cassandra.Compression.NONE
);
@@ -80,10 +80,10 @@ private byte[] GzipCompress(byte[] cqlQuery)
}
}
- public ExecuteCqlQuery(UTF8Type cqlQuery, bool? compressCqlQuery = null)
+ public ExecuteCqlQuery(UTF8Type cqlQuery, bool compressCqlQuery)
{
CqlQuery = cqlQuery;
- CompressCqlQuery = compressCqlQuery ?? CassandraContext.CurrentConnectionBuilder.CompressCqlQueries;
+ CompressCqlQuery = compressCqlQuery;
}
}
}
View
4 src/Operations/GetColumn.cs
@@ -28,10 +28,10 @@ public override IFluentColumn<CompareWith> Execute()
if (ColumnName != null)
path.Column = ColumnName;
- var output = CassandraSession.Current.GetClient().get(
+ var output = Session.GetClient().get(
Key,
path,
- CassandraSession.Current.ReadConsistency
+ Session.ReadConsistency
);
return (IFluentColumn<CompareWith>)Helper.ConvertToFluentBaseColumn<CompareWith, VoidType>(output);
View
48 src/Operations/GetColumnFamilyIndexedSlices.cs
@@ -22,38 +22,26 @@ public override IEnumerable<IFluentColumnFamily<CompareWith>> Execute()
private IEnumerable<IFluentColumnFamily<CompareWith>> GetFamilies(BaseCassandraColumnFamily columnFamily)
{
- CassandraSession _localSession = null;
- if (CassandraSession.Current == null)
- _localSession = new CassandraSession();
-
- try
+ var parent = new CassandraColumnParent {
+ ColumnFamily = columnFamily.FamilyName
+ };
+
+ var output = Session.GetClient().get_indexed_slices(
+ parent,
+ IndexClause,
+ SlicePredicate,
+ Session.ReadConsistency
+ );
+
+ foreach (var result in output)
{
- var parent = new CassandraColumnParent {
- ColumnFamily = columnFamily.FamilyName
- };
-
- var output = CassandraSession.Current.GetClient().get_indexed_slices(
- parent,
- IndexClause,
- SlicePredicate,
- CassandraSession.Current.ReadConsistency
- );
-
- foreach (var result in output)
- {
- var r = new FluentColumnFamily<CompareWith>(result.Key, columnFamily.FamilyName, result.Columns.Select(col => {
- return Helper.ConvertColumnToFluentColumn<CompareWith>(col.Column);
- }));
- columnFamily.Context.Attach(r);
- r.MutationTracker.Clear();
+ var r = new FluentColumnFamily<CompareWith>(result.Key, columnFamily.FamilyName, result.Columns.Select(col => {
+ return Helper.ConvertColumnToFluentColumn<CompareWith>(col.Column);
+ }));
+ columnFamily.Context.Attach(r);
+ r.MutationTracker.Clear();
- yield return r;
- }
- }
- finally
- {
- if (_localSession != null)
- _localSession.Dispose();
+ yield return r;
}
}
View
48 src/Operations/GetColumnFamilyRangeSlices.cs
@@ -22,38 +22,26 @@ public override IEnumerable<IFluentColumnFamily<CompareWith>> Execute()
private IEnumerable<IFluentColumnFamily<CompareWith>> GetFamilies(BaseCassandraColumnFamily columnFamily)
{
- CassandraSession _localSession = null;
- if (CassandraSession.Current == null)
- _localSession = new CassandraSession();
-
- try
+ var parent = new CassandraColumnParent {
+ ColumnFamily = columnFamily.FamilyName
+ };
+
+ var output = Session.GetClient().get_range_slices(
+ parent,
+ SlicePredicate,
+ KeyRange,
+ Session.ReadConsistency
+ );
+
+ foreach (var result in output)
{
- var parent = new CassandraColumnParent {
- ColumnFamily = columnFamily.FamilyName
- };
-
- var output = CassandraSession.Current.GetClient().get_range_slices(
- parent,
- SlicePredicate,
- KeyRange,
- CassandraSession.Current.ReadConsistency
- );
-
- foreach (var result in output)
- {
- var r = new FluentColumnFamily<CompareWith>(result.Key, columnFamily.FamilyName, result.Columns.Select(col => {
- return Helper.ConvertColumnToFluentColumn<CompareWith>(col.Column);
- }));
- columnFamily.Context.Attach(r);
- r.MutationTracker.Clear();
+ var r = new FluentColumnFamily<CompareWith>(result.Key, columnFamily.FamilyName, result.Columns.Select(col => {
+ return Helper.ConvertColumnToFluentColumn<CompareWith>(col.Column);
+ }));
+ columnFamily.Context.Attach(r);
+ r.MutationTracker.Clear();
- yield return r;
- }
- }
- finally
- {
- if (_localSession != null)
- _localSession.Dispose();
+ yield return r;
}
}
View
40 src/Operations/GetColumnFamilySlice.cs
@@ -27,33 +27,21 @@ public override IFluentColumnFamily<CompareWith> Execute()
private IEnumerable<IFluentColumn<CompareWith>> GetColumns(BaseCassandraColumnFamily columnFamily)
{
- CassandraSession _localSession = null;
- if (CassandraSession.Current == null)
- _localSession = new CassandraSession();
-
- try
- {
- var parent = new CassandraColumnParent {
- ColumnFamily = columnFamily.FamilyName
- };
-
- var output = CassandraSession.Current.GetClient().get_slice(
- Key,
- parent,
- SlicePredicate,
- CassandraSession.Current.ReadConsistency
- );
-
- foreach (var result in output)
- {
- var r = Helper.ConvertColumnToFluentColumn<CompareWith>(result.Column);
- yield return r;
- }
- }
- finally
+ var parent = new CassandraColumnParent {
+ ColumnFamily = columnFamily.FamilyName
+ };
+
+ var output = Session.GetClient().get_slice(
+ Key,
+ parent,
+ SlicePredicate,
+ Session.ReadConsistency
+ );
+
+ foreach (var result in output)
{
- if (_localSession != null)
- _localSession.Dispose();
+ var r = Helper.ConvertColumnToFluentColumn<CompareWith>(result.Column);
+ yield return r;
}
}
View
4 src/Operations/GetSuperColumn.cs
@@ -25,10 +25,10 @@ public class GetSuperColumn<CompareWith, CompareSubcolumnWith> : ColumnFamilyOpe
if (SuperColumnName != null)
path.SuperColumn = SuperColumnName;
- var output = CassandraSession.Current.GetClient().get(
+ var output = Session.GetClient().get(
Key,
path,
- CassandraSession.Current.ReadConsistency
+ Session.ReadConsistency
);
return (IFluentSuperColumn<CompareWith, CompareSubcolumnWith>)Helper.ConvertToFluentBaseColumn<CompareWith, CompareSubcolumnWith>(output);
View
54 src/Operations/GetSuperColumnFamilyIndexedSlices.cs
@@ -18,42 +18,30 @@ public class GetSuperColumnFamilyIndexedSlices<CompareWith, CompareSubcolumnWith
public override IEnumerable<IFluentSuperColumnFamily<CompareWith, CompareSubcolumnWith>> Execute()
{
- CassandraSession _localSession = null;
- if (CassandraSession.Current == null)
- _localSession = new CassandraSession();
-
- try
+ var parent = new CassandraColumnParent {
+ ColumnFamily = ColumnFamily.FamilyName
+ };
+
+ var output = Session.GetClient().get_indexed_slices(
+ parent,
+ IndexClause,
+ SlicePredicate,
+ Session.ReadConsistency
+ );
+
+ foreach (var result in output)
{
- var parent = new CassandraColumnParent {
- ColumnFamily = ColumnFamily.FamilyName
- };
-
- var output = CassandraSession.Current.GetClient().get_indexed_slices(
- parent,
- IndexClause,
- SlicePredicate,
- CassandraSession.Current.ReadConsistency
- );
-
- foreach (var result in output)
- {
- var r = new FluentSuperColumnFamily<CompareWith, CompareSubcolumnWith>(result.Key, ColumnFamily.FamilyName, result.Columns.Select(col => {
- var superCol = Helper.ConvertSuperColumnToFluentSuperColumn<CompareWith, CompareSubcolumnWith>(col.Super_column);
- ColumnFamily.Context.Attach(superCol);
- superCol.MutationTracker.Clear();
+ var r = new FluentSuperColumnFamily<CompareWith, CompareSubcolumnWith>(result.Key, ColumnFamily.FamilyName, result.Columns.Select(col => {
+ var superCol = Helper.ConvertSuperColumnToFluentSuperColumn<CompareWith, CompareSubcolumnWith>(col.Super_column);
+ ColumnFamily.Context.Attach(superCol);
+ superCol.MutationTracker.Clear();
- return superCol;
- }));
- ColumnFamily.Context.Attach(r);
- r.MutationTracker.Clear();
+ return superCol;
+ }));
+ ColumnFamily.Context.Attach(r);
+ r.MutationTracker.Clear();
- yield return r;
- }
- }
- finally
- {
- if (_localSession != null)
- _localSession.Dispose();
+ yield return r;
}
}
View
54 src/Operations/GetSuperColumnFamilyRangeSlices.cs
@@ -18,42 +18,30 @@ public class GetSuperColumnFamilyRangeSlices<CompareWith, CompareSubcolumnWith>
public override IEnumerable<IFluentSuperColumnFamily<CompareWith, CompareSubcolumnWith>> Execute()
{
- CassandraSession _localSession = null;
- if (CassandraSession.Current == null)
- _localSession = new CassandraSession();
-
- try
+ var parent = new CassandraColumnParent {
+ ColumnFamily = ColumnFamily.FamilyName
+ };
+
+ var output = Session.GetClient().get_range_slices(
+ parent,
+ SlicePredicate,
+ KeyRange,
+ Session.ReadConsistency
+ );
+
+ foreach (var result in output)
{
- var parent = new CassandraColumnParent {
- ColumnFamily = ColumnFamily.FamilyName
- };
-
- var output = CassandraSession.Current.GetClient().get_range_slices(
- parent,
- SlicePredicate,
- KeyRange,
- CassandraSession.Current.ReadConsistency
- );
-
- foreach (var result in output)
- {
- var r = new FluentSuperColumnFamily<CompareWith, CompareSubcolumnWith>(result.Key, ColumnFamily.FamilyName, result.Columns.Select(col => {
- var superCol = Helper.ConvertSuperColumnToFluentSuperColumn<CompareWith, CompareSubcolumnWith>(col.Super_column);
- ColumnFamily.Context.Attach(superCol);
- superCol.MutationTracker.Clear();
+ var r = new FluentSuperColumnFamily<CompareWith, CompareSubcolumnWith>(result.Key, ColumnFamily.FamilyName, result.Columns.Select(col => {
+ var superCol = Helper.ConvertSuperColumnToFluentSuperColumn<CompareWith, CompareSubcolumnWith>(col.Super_column);
+ ColumnFamily.Context.Attach(superCol);
+ superCol.MutationTracker.Clear();
- return superCol;
- }));
- ColumnFamily.Context.Attach(r);
- r.MutationTracker.Clear();
+ return superCol;
+ }));
+ ColumnFamily.Context.Attach(r);
+ r.MutationTracker.Clear();
- yield return r;
- }
- }
- finally
- {
- if (_localSession != null)
- _localSession.Dispose();
+ yield return r;
}
}
View
44 src/Operations/GetSuperColumnFamilySlice.cs
@@ -28,36 +28,24 @@ public class GetSuperColumnFamilySlice<CompareWith, CompareSubcolumnWith> : Colu
private IEnumerable<IFluentSuperColumn<CompareWith, CompareSubcolumnWith>> GetColumns(BaseCassandraColumnFamily columnFamily)
{
- CassandraSession _localSession = null;
- if (CassandraSession.Current == null)
- _localSession = new CassandraSession();
-
- try
+ var parent = new CassandraColumnParent {
+ ColumnFamily = columnFamily.FamilyName
+ };
+
+ var output = Session.GetClient().get_slice(
+ Key,
+ parent,
+ SlicePredicate,
+ Session.ReadConsistency
+ );
+
+ foreach (var result in output)
{
- var parent = new CassandraColumnParent {
- ColumnFamily = columnFamily.FamilyName
- };
-
- var output = CassandraSession.Current.GetClient().get_slice(
- Key,
- parent,
- SlicePredicate,
- CassandraSession.Current.ReadConsistency
- );
-
- foreach (var result in output)
- {
- var r = Helper.ConvertSuperColumnToFluentSuperColumn<CompareWith, CompareSubcolumnWith>(result.Super_column);
- columnFamily.Context.Attach(r);
- r.MutationTracker.Clear();
+ var r = Helper.ConvertSuperColumnToFluentSuperColumn<CompareWith, CompareSubcolumnWith>(result.Super_column);
+ columnFamily.Context.Attach(r);
+ r.MutationTracker.Clear();
- yield return r;
- }
- }
- finally
- {
- if (_localSession != null)
- _localSession.Dispose();
+ yield return r;
}
}
View
51 src/Operations/GetSuperColumnIndexedSlices.cs
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
-using Apache.Cassandra;
using FluentCassandra.Types;
namespace FluentCassandra.Operations
@@ -20,41 +19,29 @@ public class GetSuperColumnIndexedSlices<CompareWith, CompareSubcolumnWith> : Qu
public override IEnumerable<IFluentSuperColumn<CompareWith, CompareSubcolumnWith>> Execute()
{
- CassandraSession _localSession = null;
- if (CassandraSession.Current == null)
- _localSession = new CassandraSession();
+ var parent = new CassandraColumnParent {
+ ColumnFamily = ColumnFamily.FamilyName
+ };
- try
- {
- var parent = new CassandraColumnParent {
- ColumnFamily = ColumnFamily.FamilyName
- };
-
- if (SuperColumnName != null)
- parent.SuperColumn = SuperColumnName;
-
- var output = CassandraSession.Current.GetClient().get_indexed_slices(
- parent,
- IndexClause,
- SlicePredicate,
- CassandraSession.Current.ReadConsistency
- );
+ if (SuperColumnName != null)
+ parent.SuperColumn = SuperColumnName;
- foreach (var result in output)
- {
- var r = new FluentSuperColumn<CompareWith, CompareSubcolumnWith>(result.Columns.Select(col => {
- return Helper.ConvertColumnToFluentColumn<CompareSubcolumnWith>(col.Column);
- }));
- ColumnFamily.Context.Attach(r);
- r.MutationTracker.Clear();
+ var output = Session.GetClient().get_indexed_slices(
+ parent,
+ IndexClause,
+ SlicePredicate,
+ Session.ReadConsistency
+ );
- yield return r;
- }
- }
- finally
+ foreach (var result in output)
{
- if (_localSession != null)
- _localSession.Dispose();
+ var r = new FluentSuperColumn<CompareWith, CompareSubcolumnWith>(result.Columns.Select(col => {
+ return Helper.ConvertColumnToFluentColumn<CompareSubcolumnWith>(col.Column);
+ }));
+ ColumnFamily.Context.Attach(r);
+ r.MutationTracker.Clear();
+
+ yield return r;
}
}
View
50 src/Operations/GetSuperColumnRangeSlices.cs
@@ -20,41 +20,29 @@ public class GetSuperColumnRangeSlices<CompareWith, CompareSubcolumnWith> : Quer
public override IEnumerable<IFluentSuperColumn<CompareWith, CompareSubcolumnWith>> Execute()
{
- CassandraSession _localSession = null;
- if (CassandraSession.Current == null)
- _localSession = new CassandraSession();
+ var parent = new CassandraColumnParent {
+ ColumnFamily = ColumnFamily.FamilyName
+ };
- try
- {
- var parent = new CassandraColumnParent {
- ColumnFamily = ColumnFamily.FamilyName
- };
-
- if (SuperColumnName != null)
- parent.SuperColumn = SuperColumnName;
-
- var output = CassandraSession.Current.GetClient().get_range_slices(
- parent,
- SlicePredicate,
- KeyRange,
- CassandraSession.Current.ReadConsistency
- );
+ if (SuperColumnName != null)
+ parent.SuperColumn = SuperColumnName;
- foreach (var result in output)
- {
- var r = new FluentSuperColumn<CompareWith, CompareSubcolumnWith>(result.Columns.Select(col => {
- return Helper.ConvertColumnToFluentColumn<CompareSubcolumnWith>(col.Column);
- }));
- ColumnFamily.Context.Attach(r);
- r.MutationTracker.Clear();
+ var output = Session.GetClient().get_range_slices(
+ parent,
+ SlicePredicate,
+ KeyRange,
+ Session.ReadConsistency
+ );
- yield return r;
- }
- }
- finally
+ foreach (var result in output)
{
- if (_localSession != null)
- _localSession.Dispose();
+ var r = new FluentSuperColumn<CompareWith, CompareSubcolumnWith>(result.Columns.Select(col => {
+ return Helper.ConvertColumnToFluentColumn<CompareSubcolumnWith>(col.Column);
+ }));
+ ColumnFamily.Context.Attach(r);
+ r.MutationTracker.Clear();
+
+ yield return r;
}
}
View
40 src/Operations/GetSuperColumnSlice.cs
@@ -30,36 +30,24 @@ public class GetSuperColumnSlice<CompareWith, CompareSubcolumnWith> : ColumnFami
private IEnumerable<IFluentColumn<CompareSubcolumnWith>> GetColumns(BaseCassandraColumnFamily columnFamily)
{
- CassandraSession _localSession = null;
- if (CassandraSession.Current == null)
- _localSession = new CassandraSession();
+ var parent = new CassandraColumnParent {
+ ColumnFamily = columnFamily.FamilyName
+ };
- try
- {
- var parent = new CassandraColumnParent {
- ColumnFamily = columnFamily.FamilyName
- };
-
- if (SuperColumnName != null)
- parent.SuperColumn = SuperColumnName;
+ if (SuperColumnName != null)
+ parent.SuperColumn = SuperColumnName;
- var output = CassandraSession.Current.GetClient().get_slice(
- Key,
- parent,
- SlicePredicate,
- CassandraSession.Current.ReadConsistency
- );
+ var output = Session.GetClient().get_slice(
+ Key,
+ parent,
+ SlicePredicate,
+ Session.ReadConsistency
+ );
- foreach (var result in output)
- {
- var r = Helper.ConvertColumnToFluentColumn<CompareSubcolumnWith>(result.Column);
- yield return r;
- }
- }
- finally
+ foreach (var result in output)
{
- if (_localSession != null)
- _localSession.Dispose();
+ var r = Helper.ConvertColumnToFluentColumn<CompareSubcolumnWith>(result.Column);
+ yield return r;
}
}
View
4 src/Operations/InsertColumn.cs
@@ -39,11 +39,11 @@ public override Void Execute()
Ttl = TimeToLive
};
- CassandraSession.Current.GetClient().insert(
+ Session.GetClient().insert(
Key,
parent,
column,
- CassandraSession.Current.WriteConsistency
+ Session.WriteConsistency
);
return new Void();
View
42 src/Operations/MultiGetColumnCount.cs
@@ -16,38 +16,26 @@ public class MultiGetColumnCount : ColumnFamilyOperation<IDictionary<BytesType,
public override IDictionary<BytesType, int> Execute()
{
- CassandraSession _localSession = null;
- if (CassandraSession.Current == null)
- _localSession = new CassandraSession();
+ var parent = new CassandraColumnParent {
+ ColumnFamily = ColumnFamily.FamilyName
+ };
- try
- {
- var parent = new CassandraColumnParent {
- ColumnFamily = ColumnFamily.FamilyName
- };
+ if (SuperColumnName != null)
+ parent.SuperColumn = SuperColumnName;
- if (SuperColumnName != null)
- parent.SuperColumn = SuperColumnName;
+ var output = Session.GetClient().multiget_count(
+ Keys,
+ parent,
+ SlicePredicate,
+ Session.ReadConsistency
+ );
- var output = CassandraSession.Current.GetClient().multiget_count(
- Keys,
- parent,
- SlicePredicate,
- CassandraSession.Current.ReadConsistency
- );
+ var results = new Dictionary<BytesType, int>();
- var results = new Dictionary<BytesType, int>();
+ foreach (var result in output)
+ results.Add(result.Key, result.Value);
- foreach (var result in output)
- results.Add(result.Key, result.Value);
-
- return results;
- }
- finally
- {
- if (_localSession != null)
- _localSession.Dispose();
- }
+ return results;
}
public MultiGetColumnCount(IEnumerable<BytesType> keys, CassandraSlicePredicate columnSlicePredicate)
View
54 src/Operations/MultiGetColumnFamilySlice.cs
@@ -16,40 +16,28 @@ public class MultiGetColumnFamilySlice<CompareWith> : QueryableColumnFamilyOpera
public override IEnumerable<IFluentColumnFamily<CompareWith>> Execute()
{
- CassandraSession _localSession = null;
- if (CassandraSession.Current == null)
- _localSession = new CassandraSession();
-
- try
- {
- var parent = new CassandraColumnParent {
- ColumnFamily = ColumnFamily.FamilyName
- };
-
- var output = CassandraSession.Current.GetClient().multiget_slice(
- Keys,
- parent,
- SlicePredicate,
- CassandraSession.Current.ReadConsistency
- );
-
- foreach (var result in output)
- {
- var key = CassandraType.GetTypeFromDatabaseValue<BytesType>(result.Key);
-
- var r = new FluentColumnFamily<CompareWith>(key, ColumnFamily.FamilyName, result.Value.Select(col => {
- return Helper.ConvertColumnToFluentColumn<CompareWith>(col.Column);
- }));
- ColumnFamily.Context.Attach(r);
- r.MutationTracker.Clear();
-
- yield return r;
- }
- }
- finally
+ var parent = new CassandraColumnParent {
+ ColumnFamily = ColumnFamily.FamilyName
+ };
+
+ var output = Session.GetClient().multiget_slice(
+ Keys,
+ parent,
+ SlicePredicate,
+ Session.ReadConsistency
+ );
+
+ foreach (var result in output)
{
- if (_localSession != null)
- _localSession.Dispose();
+ var key = CassandraType.GetTypeFromDatabaseValue<BytesType>(result.Key);
+
+ var r = new FluentColumnFamily<CompareWith>(key, ColumnFamily.FamilyName, result.Value.Select(col => {
+ return Helper.ConvertColumnToFluentColumn<CompareWith>(col.Column);
+ }));
+ ColumnFamily.Context.Attach(r);
+ r.MutationTracker.Clear();
+
+ yield return r;
}
}
View
60 src/Operations/MultiGetSuperColumnFamilySlice.cs
@@ -17,47 +17,35 @@ public class MultiGetSuperColumnFamilySlice<CompareWith, CompareSubcolumnWith> :
public override IEnumerable<IFluentSuperColumnFamily<CompareWith, CompareSubcolumnWith>> Execute()
{
- CassandraSession _localSession = null;
- if (CassandraSession.Current == null)
- _localSession = new CassandraSession();
-
- try
+ var parent = new CassandraColumnParent {
+ ColumnFamily = ColumnFamily.FamilyName
+ };
+
+ var output = Session.GetClient().multiget_slice(
+ Keys,
+ parent,
+ SlicePredicate,
+ Session.ReadConsistency
+ );
+
+ foreach (var result in output)
{
- var parent = new CassandraColumnParent {
- ColumnFamily = ColumnFamily.FamilyName
- };
-
- var output = CassandraSession.Current.GetClient().multiget_slice(
- Keys,
- parent,
- SlicePredicate,
- CassandraSession.Current.ReadConsistency
- );
-
- foreach (var result in output)
- {
- var key = CassandraType.GetTypeFromDatabaseValue<BytesType>(result.Key);
+ var key = CassandraType.GetTypeFromDatabaseValue<BytesType>(result.Key);
- var superColumns = result.Value.Select(col => {
- var superCol = Helper.ConvertSuperColumnToFluentSuperColumn<CompareWith, CompareSubcolumnWith>(col.Super_column);
- ColumnFamily.Context.Attach(superCol);
- superCol.MutationTracker.Clear();
+ var superColumns = result.Value.Select(col => {
+ var superCol = Helper.ConvertSuperColumnToFluentSuperColumn<CompareWith, CompareSubcolumnWith>(col.Super_column);
+ ColumnFamily.Context.Attach(superCol);
+ superCol.MutationTracker.Clear();
- return superCol;
- });
+ return superCol;
+ });
- var familyName = ColumnFamily.FamilyName;
- var r = new FluentSuperColumnFamily<CompareWith, CompareSubcolumnWith>(key, familyName, superColumns);
- ColumnFamily.Context.Attach(r);
- r.MutationTracker.Clear();
+ var familyName = ColumnFamily.FamilyName;
+ var r = new FluentSuperColumnFamily<CompareWith, CompareSubcolumnWith>(key, familyName, superColumns);
+ ColumnFamily.Context.Attach(r);
+ r.MutationTracker.Clear();
- yield return r;
- }
- }
- finally
- {
- if (_localSession != null)
- _localSession.Dispose();
+ yield return r;
}
}
View
50 src/Operations/MultiGetSuperColumnSlice.cs
@@ -19,41 +19,29 @@ public class MultiGetSuperColumnSlice<CompareWith, CompareSubcolumnWith> : Query
public override IEnumerable<IFluentSuperColumn<CompareWith, CompareSubcolumnWith>> Execute()
{
- CassandraSession _localSession = null;
- if (CassandraSession.Current == null)
- _localSession = new CassandraSession();
+ var parent = new CassandraColumnParent {
+ ColumnFamily = ColumnFamily.FamilyName
+ };
- try
- {
- var parent = new CassandraColumnParent {
- ColumnFamily = ColumnFamily.FamilyName
- };
-
- if (SuperColumnName != null)
- parent.SuperColumn = SuperColumnName;
-
- var output = CassandraSession.Current.GetClient().multiget_slice(
- Keys,
- parent,
- SlicePredicate,
- CassandraSession.Current.ReadConsistency
- );
+ if (SuperColumnName != null)
+ parent.SuperColumn = SuperColumnName;
- foreach (var result in output)
- {
- var r = new FluentSuperColumn<CompareWith, CompareSubcolumnWith>(result.Value.Select(col => {
- return Helper.ConvertColumnToFluentColumn<CompareSubcolumnWith>(col.Column);
- }));
- ColumnFamily.Context.Attach(r);
- r.MutationTracker.Clear();
+ var output = Session.GetClient().multiget_slice(
+ Keys,
+ parent,
+ SlicePredicate,
+ Session.ReadConsistency
+ );
- yield return r;
- }
- }
- finally
+ foreach (var result in output)
{
- if (_localSession != null)
- _localSession.Dispose();
+ var r = new FluentSuperColumn<CompareWith, CompareSubcolumnWith>(result.Value.Select(col => {
+ return Helper.ConvertColumnToFluentColumn<CompareSubcolumnWith>(col.Column);
+ }));
+ ColumnFamily.Context.Attach(r);
+ r.MutationTracker.Clear();
+
+ yield return r;
}
}
View
4 src/Operations/Operation.cs
@@ -11,8 +11,10 @@ public Operation()
HasError = false;
}
- public bool HasError { get; protected set; }
+ public CassandraContext Context { get; set; }
+ public CassandraSession Session { get; set; }
+ public bool HasError { get; protected set; }
public CassandraException Error { get; protected set; }
public virtual bool TryExecute(out TResult result)
View
4 src/Operations/RemoveColumn.cs
@@ -28,11 +28,11 @@ public override Void Execute()
if (ColumnName != null)
path.Column = ColumnName;
- CassandraSession.Current.GetClient().remove(
+ Session.GetClient().remove(
Key,
path,
DateTimeOffset.Now.ToTimestamp(),
- CassandraSession.Current.WriteConsistency
+ Session.WriteConsistency
);
return new Void();
View
4 src/Operations/RemoveKey.cs
@@ -18,11 +18,11 @@ public override Void Execute()
ColumnFamily = ColumnFamily.FamilyName
};
- CassandraSession.Current.GetClient().remove(
+ Session.GetClient().remove(
Key,
path,
DateTimeOffset.UtcNow.ToTimestamp(),
- CassandraSession.Current.WriteConsistency
+ Session.WriteConsistency
);
return new Void();
View
4 src/Operations/RemoveSuperColumn.cs
@@ -23,11 +23,11 @@ public override Void Execute()
if (SuperColumnName != null)
path.SuperColumn = SuperColumnName;
- CassandraSession.Current.GetClient().remove(
+ Session.GetClient().remove(
Key,
path,
DateTimeOffset.Now.ToTimestamp(),
- CassandraSession.Current.WriteConsistency
+ Session.WriteConsistency
);
return new Void();
View
2  src/Operations/Truncate.cs
@@ -6,7 +6,7 @@ public class Truncate : ColumnFamilyOperation<Void>
{
public override Void Execute()
{
- CassandraSession.Current.GetClient().truncate(ColumnFamily.FamilyName);
+ Session.GetClient().truncate(ColumnFamily.FamilyName);
return new Void();
}
Please sign in to comment.
Something went wrong with that request. Please try again.