Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

153 lines (132 sloc) 3.878 kb
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using FluentCassandra.Operations;
using FluentCassandra.Types;
namespace FluentCassandra
{
/// <seealso href="http://wiki.apache.org/cassandra/API"/>
public abstract class BaseCassandraColumnFamily
{
private CassandraContext _context;
/// <summary>
///
/// </summary>
/// <param name="keyspace"></param>
/// <param name="connection"></param>
public BaseCassandraColumnFamily(CassandraContext context, string columnFamily)
{
_context = context;
FamilyName = columnFamily;
ThrowErrors = context.ThrowErrors;
}
/// <summary>
/// The context the column family currently belongs to.
/// </summary>
public CassandraContext Context { get { return _context; } }
/// <summary>
/// The family name for this column family.
/// </summary>
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>
/// <returns></returns>
public bool IsPartOfFamily(IFluentBaseColumnFamily family)
{
return String.Equals(family.FamilyName, FamilyName);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public abstract CassandraColumnFamilySchema GetSchema();
/// <summary>
///
/// </summary>
/// <param name="schema"></param>
public abstract void SetSchema(CassandraColumnFamilySchema schema);
/// <summary>
///
/// </summary>
public abstract void ClearCachedColumnFamilySchema();
/// <summary>
///
/// </summary>
public void TryCreateSelf()
{
Context.Keyspace.TryCreateColumnFamily(GetSchema());
}
/// <summary>
///
/// </summary>
/// <param name="key"></param>
public void RemoveKey(CassandraObject key)
{
var op = new Remove(key);
ExecuteOperation(op);
}
/// <summary>
/// Removes all the rows from the given column family.
/// </summary>
public void RemoveAllRows()
{
_context.ExecuteOperation(new SimpleOperation<int>(ctx => {
ctx.Session.GetClient().truncate(FamilyName);
return 0;
}));
}
/// <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>(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();
}
}
public CassandraSlicePredicateQuery<TResult> CreateCassandraSlicePredicateQuery<TResult>(Expression expression)
{
return new CassandraSlicePredicateQuery<TResult>(this, expression);
}
public IEnumerable<TResult> ExecuteCassandraSlicePredicateQuery<TResult>(CassandraSlicePredicateQuery<TResult> query)
{
var op = query.BuildQueryableOperation();
return ExecuteOperation((QueryableColumnFamilyOperation<TResult>)op);
}
public override string ToString()
{
return FamilyName;
}
}
}
Jump to Line
Something went wrong with that request. Please try again.