Skip to content
This repository has been archived by the owner on May 25, 2021. It is now read-only.

Commit

Permalink
added support for counter column
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Berardi committed Feb 15, 2012
1 parent 700a50a commit ac522e7
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/CassandraColumnFamilyOperations.cs
Original file line number Original file line Diff line number Diff line change
@@ -1,9 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using FluentCassandra.Types;
using FluentCassandra.Operations;
using System.Linq.Expressions; using System.Linq.Expressions;
using FluentCassandra.Linq; using FluentCassandra.Linq;
using FluentCassandra.Operations;
using FluentCassandra.Types;


namespace FluentCassandra namespace FluentCassandra
{ {
Expand Down Expand Up @@ -58,7 +58,7 @@ public static void InsertColumn(this CassandraColumnFamily family, CassandraObje


#region AddColumn #region AddColumn


public static void InsertColumn(this CassandraColumnFamily family, CassandraObject key, CassandraObject columnName, long columnValue) public static void InsertCounterColumn(this CassandraColumnFamily family, CassandraObject key, CassandraObject columnName, long columnValue)
{ {
var op = new AddColumn(key, columnName, columnValue); var op = new AddColumn(key, columnName, columnValue);
family.ExecuteOperation(op); family.ExecuteOperation(op);
Expand Down
3 changes: 3 additions & 0 deletions src/CassandraContext.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ public CassandraSession OpenSession()
/// <returns></returns> /// <returns></returns>
public TResult ExecuteOperation<TResult>(Operation<TResult> action, bool? throwOnError = null) public TResult ExecuteOperation<TResult>(Operation<TResult> action, bool? throwOnError = null)
{ {
if (WasDisposed)
throw new ObjectDisposedException(GetType().FullName);

if (!throwOnError.HasValue) if (!throwOnError.HasValue)
throwOnError = ThrowErrors; throwOnError = ThrowErrors;


Expand Down
6 changes: 4 additions & 2 deletions src/CassandraSession.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -137,12 +137,14 @@ public void Login(string username, string password)
/// <returns></returns> /// <returns></returns>
public TResult ExecuteOperation<TResult>(Operation<TResult> action, bool? throwOnError = null) public TResult ExecuteOperation<TResult>(Operation<TResult> action, bool? throwOnError = null)
{ {
if (WasDisposed)
throw new ObjectDisposedException(GetType().FullName);

if (!throwOnError.HasValue) if (!throwOnError.HasValue)
throwOnError = ThrowErrors; throwOnError = ThrowErrors;


action.Session = this;

LastError = null; LastError = null;
action.Session = this;


TResult result; TResult result;
bool success = action.TryExecute(out result); bool success = action.TryExecute(out result);
Expand Down
5 changes: 2 additions & 3 deletions src/CassandraSuperColumnFamilyOperations.cs
Original file line number Original file line Diff line number Diff line change
@@ -1,8 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using FluentCassandra.Types;
using FluentCassandra.Operations; using FluentCassandra.Operations;
using System.Linq.Expressions; using FluentCassandra.Types;


namespace FluentCassandra namespace FluentCassandra
{ {
Expand Down Expand Up @@ -70,7 +69,7 @@ public static void InsertColumn(this CassandraSuperColumnFamily family, Cassandr


#region AddColumn #region AddColumn


public static void InsertColumn(this CassandraSuperColumnFamily family, CassandraObject key, CassandraObject superColumnName, CassandraObject columnName, long columnValue) public static void InsertCounterColumn(this CassandraSuperColumnFamily family, CassandraObject key, CassandraObject superColumnName, CassandraObject columnName, long columnValue)
{ {
var op = new AddColumn(key, superColumnName, columnName, columnValue); var op = new AddColumn(key, superColumnName, columnName, columnValue);
family.ExecuteOperation(op); family.ExecuteOperation(op);
Expand Down
4 changes: 3 additions & 1 deletion src/FluentCassandra.csproj
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -131,6 +131,7 @@
<Compile Include="FluentColumnPath.cs"> <Compile Include="FluentColumnPath.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="FluentCounterColumn.cs" />
<Compile Include="FluentMutation.cs"> <Compile Include="FluentMutation.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
Expand Down Expand Up @@ -255,6 +256,7 @@
<Compile Include="Types\CompositeType`1.cs" /> <Compile Include="Types\CompositeType`1.cs" />
<Compile Include="Types\BooleanType.cs" /> <Compile Include="Types\BooleanType.cs" />
<Compile Include="Types\BooleanTypeConverter.cs" /> <Compile Include="Types\BooleanTypeConverter.cs" />
<Compile Include="Types\CounterColumnType.cs" />
<Compile Include="Types\DateType.cs" /> <Compile Include="Types\DateType.cs" />
<Compile Include="Types\DateTypeConverter.cs" /> <Compile Include="Types\DateTypeConverter.cs" />
<Compile Include="Types\Int32Type.cs" /> <Compile Include="Types\Int32Type.cs" />
Expand Down
2 changes: 1 addition & 1 deletion src/FluentColumnFamily.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public override IList<FluentColumn> Columns
/// <returns></returns> /// <returns></returns>
public FluentColumnPath GetPath() public FluentColumnPath GetPath()
{ {
return new FluentColumnPath(this, null, null); return new FluentColumnPath(this, null, (FluentColumn)null);
} }


/// <summary> /// <summary>
Expand Down
11 changes: 11 additions & 0 deletions src/FluentColumnPath.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,12 +7,23 @@ public class FluentColumnPath : FluentColumnParent
public FluentColumnPath(FluentColumnParent parent, FluentColumn column) public FluentColumnPath(FluentColumnParent parent, FluentColumn column)
: this (parent.ColumnFamily, parent.SuperColumn, column) { } : this (parent.ColumnFamily, parent.SuperColumn, column) { }


public FluentColumnPath(FluentColumnParent parent, FluentCounterColumn column)
: this(parent.ColumnFamily, parent.SuperColumn, column) { }

public FluentColumnPath(IFluentBaseColumnFamily columnFamily, FluentSuperColumn superColumn, FluentColumn column) public FluentColumnPath(IFluentBaseColumnFamily columnFamily, FluentSuperColumn superColumn, FluentColumn column)
: base(columnFamily, superColumn) : base(columnFamily, superColumn)
{ {
Column = column; Column = column;
} }


public FluentColumnPath(IFluentBaseColumnFamily columnFamily, FluentSuperColumn superColumn, FluentCounterColumn column)
: base(columnFamily, superColumn)
{
CounterColumn = column;
}

public FluentColumn Column { get; set; } public FluentColumn Column { get; set; }

public FluentCounterColumn CounterColumn { get; set; }
} }
} }
18 changes: 18 additions & 0 deletions src/FluentCounterColumn.cs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using FluentCassandra.Types;

namespace FluentCassandra
{
public class FluentCounterColumn : FluentColumn
{
public FluentCounterColumn(CassandraColumnSchema schema = null)
: base(schema)
{
schema = GetSchema();
schema.ValueType = CassandraType.ColumnCounterType;

// ensure value type is set to column counter type
SetSchema(schema);
}
}
}
2 changes: 1 addition & 1 deletion src/FluentSuperColumn.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void SetSchema(CassandraColumnSchema schema)
/// <returns></returns> /// <returns></returns>
public FluentColumnPath GetPath() public FluentColumnPath GetPath()
{ {
return new FluentColumnPath(Family, this, null); return new FluentColumnPath(Family, this, (FluentColumn)null);
} }


/// <summary> /// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/FluentSuperColumnFamily.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void SetSchema(CassandraColumnFamilySchema schema)
/// <returns></returns> /// <returns></returns>
public FluentColumnPath GetPath() public FluentColumnPath GetPath()
{ {
return new FluentColumnPath(this, null, null); return new FluentColumnPath(this, null, (FluentColumn)null);
} }


/// <summary> /// <summary>
Expand Down
51 changes: 49 additions & 2 deletions src/Operations/Helper.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -146,13 +146,60 @@ public static IFluentBaseColumn ConvertToFluentBaseColumn(ColumnOrSuperColumn co
else if (col.Column != null) else if (col.Column != null)
return ConvertColumnToFluentColumn(col.Column, schema); return ConvertColumnToFluentColumn(col.Column, schema);
else if (col.Counter_super_column != null) else if (col.Counter_super_column != null)
throw new NotSupportedException("Reading CounterSuperColumns isn't supported yet."); return ConvertColumnToFluentCounterColumn(col.Counter_column, schema);
else if (col.Counter_column != null) else if (col.Counter_column != null)
throw new NotSupportedException("Reading CounterSuperColumns isn't supported yet."); return ConvertSuperColumnToFluentCounterSuperColumn(col.Counter_super_column, schema);
else else
return null; return null;
} }


public static FluentSuperColumn ConvertSuperColumnToFluentCounterSuperColumn(CounterSuperColumn col, CassandraColumnFamilySchema schema = null)
{
var superColSchema = new CassandraColumnSchema {
Name = col.Name
};

if (schema != null)
superColSchema = new CassandraColumnSchema {
NameType = schema.SuperColumnNameType,
Name = col.Name,
ValueType = schema.ColumnNameType
};

var superCol = new FluentSuperColumn(superColSchema) {
ColumnName = CassandraObject.GetTypeFromDatabaseValue(col.Name, superColSchema.NameType)
};

foreach (var xcol in col.Columns)
superCol.Columns.Add(ConvertColumnToFluentCounterColumn(xcol, schema));

return superCol;
}

public static FluentCounterColumn ConvertColumnToFluentCounterColumn(CounterColumn col, CassandraColumnFamilySchema schema = null)
{
var colSchema = new CassandraColumnSchema();

if (schema != null)
{
colSchema = schema.Columns.Where(x => x.Name == col.Name).FirstOrDefault();

if (colSchema == null)
{
colSchema = new CassandraColumnSchema();
colSchema.NameType = schema.ColumnNameType;
colSchema.ValueType = schema.DefaultColumnValueType;
}
}

var fcol = new FluentCounterColumn(colSchema) {
ColumnName = CassandraObject.GetTypeFromDatabaseValue(col.Name, colSchema.NameType),
ColumnValue = col.Value
};

return fcol;
}

public static FluentColumn ConvertColumnToFluentColumn(Column col, CassandraColumnFamilySchema schema = null) public static FluentColumn ConvertColumnToFluentColumn(Column col, CassandraColumnFamilySchema schema = null)
{ {
var colSchema = new CassandraColumnSchema(); var colSchema = new CassandraColumnSchema();
Expand Down
3 changes: 1 addition & 2 deletions src/Types/CassandraType.cs
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Numerics; using System.Numerics;


namespace FluentCassandra.Types namespace FluentCassandra.Types
Expand All @@ -22,6 +20,7 @@ public sealed class CassandraType
public static readonly CassandraType TimeUUIDType = new CassandraType("org.apache.cassandra.db.marshal.TimeUUIDType"); public static readonly CassandraType TimeUUIDType = new CassandraType("org.apache.cassandra.db.marshal.TimeUUIDType");
public static readonly CassandraType UTF8Type = new CassandraType("org.apache.cassandra.db.marshal.UTF8Type"); public static readonly CassandraType UTF8Type = new CassandraType("org.apache.cassandra.db.marshal.UTF8Type");
public static readonly CassandraType UUIDType = new CassandraType("org.apache.cassandra.db.marshal.UUIDType"); public static readonly CassandraType UUIDType = new CassandraType("org.apache.cassandra.db.marshal.UUIDType");
public static readonly CassandraType ColumnCounterType = new CassandraType("org.apache.cassandra.db.marshal.ColumnCounterType");


private readonly string _dbType; private readonly string _dbType;
private Type _type; private Type _type;
Expand Down
8 changes: 8 additions & 0 deletions src/Types/CounterColumnType.cs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace FluentCassandra.Types
{
public class CounterColumnType : LongType
{
}
}

0 comments on commit ac522e7

Please sign in to comment.