Permalink
Browse files

Modified QueryableColumnFamilyOperation to be a ColumnFamilyOperation…

…<IList<TResult>> instead of IEnumerable.

now returning lists instead of doing the yield pattern. Also noticed that in some situations if you do something like

var cf = db.GetColumnFamily("test").Get();

if (cf.Any())
{
   var key = cf.Last();
}

This would result in two seperate calls to cassandra. I've modified CassandraSlicePredicateQuery to cache the enumerator returned
from

Family.ExecuteCassandraSlicePredicateQuery(this)

On the first operation and then all other calls to GetEnumerator return the cached enumerator.

```c#
public IEnumerator<TResult> GetEnumerator()
{
   if (_internalList == null)
      _internalList = Family.ExecuteCassandraSlicePredicateQuery(this).GetEnumerator();

      return _internalList;
}
```
  • Loading branch information...
1 parent 79db9aa commit 9413757c3b8376b6004e7d024a97d0bc1ca0bd93 @eplowe eplowe committed Nov 16, 2012
@@ -371,22 +371,26 @@ public TResult ExecuteOperation<TResult>(Operation<TResult> action, bool? throwO
if (WasDisposed)
throw new ObjectDisposedException(GetType().FullName);
- if (_session == null)
- _session = new CassandraSession(ConnectionBuilder);
+ var localSession = _session == null;
+ var session = _session;
+ if (session == null)
+ session = new CassandraSession(ConnectionBuilder);
action.Context = this;
try
{
- var result = _session.ExecuteOperation(action, throwOnError ?? ThrowErrors);
- LastError = _session.LastError;
+ var result = session.ExecuteOperation(action, throwOnError ?? ThrowErrors);
+ LastError = session.LastError;
return result;
}
finally
{
- if (_session != null)
- _session.CloseClient();
+ if (localSession && session != null)
+ {
+ session.Dispose();
+ }
}
}
@@ -123,6 +123,7 @@ internal void CloseClient()
if (_connection != null)
{
ConnectionProvider.Close(_connection);
+
}
}
@@ -70,7 +70,7 @@ public int get_count(CassandraObject key, CassandraColumnParent column_parent, C
public List<Apache.Cassandra.KeySlice> get_range_slices(CassandraColumnParent column_parent, CassandraSlicePredicate predicate, CassandraKeyRange range, Apache.Cassandra.ConsistencyLevel consistency_level)
{
- return _client.get_range_slices(
+ return _client.get_range_slices(
Helper.CreateColumnParent(column_parent),
Helper.CreateSlicePredicate(predicate),
Helper.CreateKeyRange(range),
@@ -2,11 +2,14 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq.Expressions;
+using System.Linq;
namespace FluentCassandra.Operations
{
public class CassandraSlicePredicateQuery<TResult> : IEnumerable<TResult>
{
+ private IEnumerator<TResult> _internalList;
+
internal CassandraSlicePredicateQuery(BaseCassandraColumnFamily family, Expression expression)
{
Family = family;
@@ -16,12 +19,16 @@ internal CassandraSlicePredicateQuery(BaseCassandraColumnFamily family, Expressi
Expression = Expression.Constant(this);
}
+
#region IEnumerable<TResult> Members
public IEnumerator<TResult> GetEnumerator()
{
- return Family.ExecuteCassandraSlicePredicateQuery(this).GetEnumerator();
- }
+ if (_internalList == null)
+ _internalList = Family.ExecuteCassandraSlicePredicateQuery(this).GetEnumerator();
+
+ return _internalList;
+ }
#endregion
@@ -13,14 +13,15 @@ public class GetColumnFamilyIndexedSlices : QueryableColumnFamilyOperation<Fluen
public CassandraIndexClause IndexClause { get; private set; }
- public override IEnumerable<FluentColumnFamily> Execute()
+ public override IList<FluentColumnFamily> Execute()
{
- return GetFamilies(ColumnFamily);
+ return GetFamilies(ColumnFamily);
}
- private IEnumerable<FluentColumnFamily> GetFamilies(BaseCassandraColumnFamily columnFamily)
+ private IList<FluentColumnFamily> GetFamilies(BaseCassandraColumnFamily columnFamily)
{
var schema = ColumnFamily.GetSchema();
+ var list = new List<FluentColumnFamily>();
var parent = new CassandraColumnParent {
ColumnFamily = columnFamily.FamilyName
@@ -45,8 +46,10 @@ private IEnumerable<FluentColumnFamily> GetFamilies(BaseCassandraColumnFamily co
columnFamily.Context.Attach(r);
r.MutationTracker.Clear();
- yield return r;
+ list.Add(r);
}
+
+ return list;
}
public GetColumnFamilyIndexedSlices(CassandraIndexClause indexClause, CassandraSlicePredicate columnSlicePredicate)
@@ -8,15 +8,16 @@ namespace FluentCassandra.Operations
public class GetColumnFamilyRangeSlices : QueryableColumnFamilyOperation<FluentColumnFamily>
{
public CassandraKeyRange KeyRange { get; private set; }
-
- public override IEnumerable<FluentColumnFamily> Execute()
+
+ public override IList<FluentColumnFamily> Execute()
{
- return GetFamilies(ColumnFamily);
+ return GetFamilies(ColumnFamily);
}
- private IEnumerable<FluentColumnFamily> GetFamilies(BaseCassandraColumnFamily columnFamily)
+ private IList<FluentColumnFamily> GetFamilies(BaseCassandraColumnFamily columnFamily)
{
var schema = ColumnFamily.GetSchema();
+ var list = new List<FluentColumnFamily>();
var parent = new CassandraColumnParent {
ColumnFamily = columnFamily.FamilyName
@@ -40,9 +41,10 @@ private IEnumerable<FluentColumnFamily> GetFamilies(BaseCassandraColumnFamily co
}));
columnFamily.Context.Attach(r);
r.MutationTracker.Clear();
-
- yield return r;
+ list.Add(r);
}
+
+ return list;
}
public GetColumnFamilyRangeSlices(CassandraKeyRange keyRange, CassandraSlicePredicate columnSlicePredicate)
@@ -13,9 +13,10 @@ public class GetSuperColumnFamilyIndexedSlices : QueryableColumnFamilyOperation<
public CassandraIndexClause IndexClause { get; private set; }
- public override IEnumerable<FluentSuperColumnFamily> Execute()
+ public override IList<FluentSuperColumnFamily> Execute()
{
var schema = ColumnFamily.GetSchema();
+ var list = new List<FluentSuperColumnFamily>();
var parent = new CassandraColumnParent {
ColumnFamily = ColumnFamily.FamilyName
@@ -44,8 +45,10 @@ public override IEnumerable<FluentSuperColumnFamily> Execute()
ColumnFamily.Context.Attach(r);
r.MutationTracker.Clear();
- yield return r;
+ list.Add(r);
}
+
+ return list;
}
public GetSuperColumnFamilyIndexedSlices(CassandraIndexClause indexClause, CassandraSlicePredicate columnSlicePredicate)
@@ -11,9 +11,10 @@ public class GetSuperColumnFamilyRangeSlices : QueryableColumnFamilyOperation<Fl
public CassandraObject SuperColumnName { get; private set; }
- public override IEnumerable<FluentSuperColumnFamily> Execute()
+ public override IList<FluentSuperColumnFamily> Execute()
{
var schema = ColumnFamily.GetSchema();
+ var list = new List<FluentSuperColumnFamily>();
var parent = new CassandraColumnParent {
ColumnFamily = ColumnFamily.FamilyName
@@ -41,9 +42,10 @@ public override IEnumerable<FluentSuperColumnFamily> Execute()
}));
ColumnFamily.Context.Attach(r);
r.MutationTracker.Clear();
-
- yield return r;
+ list.Add(r);
}
+
+ return list;
}
public GetSuperColumnFamilyRangeSlices(CassandraKeyRange keyRange, CassandraObject superColumnName, CassandraSlicePredicate columnSlicePredicate)
@@ -13,9 +13,10 @@ public class MultiGetColumnFamilySlice : QueryableColumnFamilyOperation<FluentCo
public List<CassandraObject> Keys { get; private set; }
- public override IEnumerable<FluentColumnFamily> Execute()
+ public override IList<FluentColumnFamily> Execute()
{
var schema = ColumnFamily.GetSchema();
+ var list = new List<FluentColumnFamily>();
var parent = new CassandraColumnParent {
ColumnFamily = ColumnFamily.FamilyName
@@ -40,8 +41,10 @@ public override IEnumerable<FluentColumnFamily> Execute()
ColumnFamily.Context.Attach(r);
r.MutationTracker.Clear();
- yield return r;
+ list.Add(r);
}
+
+ return list;
}
public MultiGetColumnFamilySlice(IEnumerable<CassandraObject> keys, CassandraSlicePredicate columnSlicePredicate)
@@ -11,9 +11,10 @@ public class MultiGetSuperColumnFamilySlice : QueryableColumnFamilyOperation<Flu
public CassandraObject SuperColumnName { get; private set; }
- public override IEnumerable<FluentSuperColumnFamily> Execute()
+ public override IList<FluentSuperColumnFamily> Execute()
{
var schema = ColumnFamily.GetSchema();
+ var list = new List<FluentSuperColumnFamily>();
var parent = new CassandraColumnParent {
ColumnFamily = ColumnFamily.FamilyName
@@ -59,8 +60,10 @@ public override IEnumerable<FluentSuperColumnFamily> Execute()
var r = new FluentSuperColumnFamily(key, familyName, schema, superColumns);
ColumnFamily.Context.Attach(r);
- yield return r;
+ list.Add(r);
}
+
+ return list;
}
public MultiGetSuperColumnFamilySlice(IEnumerable<CassandraObject> keys, CassandraObject superColumnName, CassandraSlicePredicate columnSlicePredicate)
@@ -3,7 +3,7 @@
namespace FluentCassandra.Operations
{
- public abstract class QueryableColumnFamilyOperation<TResult> : ColumnFamilyOperation<IEnumerable<TResult>>
+ public abstract class QueryableColumnFamilyOperation<TResult> : ColumnFamilyOperation<IList<TResult>>
{
public CassandraSlicePredicate SlicePredicate { get; internal protected set; }
}

0 comments on commit 9413757

Please sign in to comment.