Permalink
Browse files

refactored mutation handling to better handle super columns which fix…

…ed issue #52
  • Loading branch information...
1 parent efd5d8c commit 2c77dfca83891a9559e14a2b58797095c0486050 @nberardi nberardi committed Aug 26, 2012
View
@@ -20,6 +20,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{1A88B962
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentCassandra.Tests", "test\FluentCassandra.Tests\FluentCassandra.Tests.csproj", "{9DAF7022-5820-4214-B13E-AC0A1B37691F}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApplication1", "test\ConsoleApplication1\ConsoleApplication1.csproj", "{1A67421F-1671-4A73-A303-56881BA4C70F}"
+EndProject
Global
GlobalSection(TestCaseManagementSettings) = postSolution
CategoryFile = FluentCassandra.vsmdi
@@ -33,26 +35,6 @@ Global
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
- {9DAF7022-5820-4214-B13E-AC0A1B37691F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {9DAF7022-5820-4214-B13E-AC0A1B37691F}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {9DAF7022-5820-4214-B13E-AC0A1B37691F}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
- {9DAF7022-5820-4214-B13E-AC0A1B37691F}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
- {9DAF7022-5820-4214-B13E-AC0A1B37691F}.Debug|x86.ActiveCfg = Debug|Any CPU
- {9DAF7022-5820-4214-B13E-AC0A1B37691F}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {9DAF7022-5820-4214-B13E-AC0A1B37691F}.Release|Any CPU.Build.0 = Release|Any CPU
- {9DAF7022-5820-4214-B13E-AC0A1B37691F}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
- {9DAF7022-5820-4214-B13E-AC0A1B37691F}.Release|Mixed Platforms.Build.0 = Release|Any CPU
- {9DAF7022-5820-4214-B13E-AC0A1B37691F}.Release|x86.ActiveCfg = Release|Any CPU
- {DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}.Debug|Any CPU.ActiveCfg = Debug|x86
- {DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
- {DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}.Debug|Mixed Platforms.Build.0 = Debug|x86
- {DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}.Debug|x86.ActiveCfg = Debug|x86
- {DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}.Debug|x86.Build.0 = Debug|x86
- {DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}.Release|Any CPU.ActiveCfg = Release|x86
- {DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}.Release|Mixed Platforms.ActiveCfg = Release|x86
- {DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}.Release|Mixed Platforms.Build.0 = Release|x86
- {DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}.Release|x86.ActiveCfg = Release|x86
- {DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}.Release|x86.Build.0 = Release|x86
{EAA32600-3C2A-4B34-B9B2-5764F280FCE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EAA32600-3C2A-4B34-B9B2-5764F280FCE3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EAA32600-3C2A-4B34-B9B2-5764F280FCE3}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
@@ -63,13 +45,44 @@ Global
{EAA32600-3C2A-4B34-B9B2-5764F280FCE3}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{EAA32600-3C2A-4B34-B9B2-5764F280FCE3}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{EAA32600-3C2A-4B34-B9B2-5764F280FCE3}.Release|x86.ActiveCfg = Release|Any CPU
+ {DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}.Debug|Any CPU.ActiveCfg = Debug|x86
+ {DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
+ {DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}.Debug|Mixed Platforms.Build.0 = Debug|x86
+ {DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}.Debug|x86.ActiveCfg = Debug|x86
+ {DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}.Debug|x86.Build.0 = Debug|x86
+ {DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}.Release|Any CPU.ActiveCfg = Release|x86
+ {DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}.Release|Mixed Platforms.ActiveCfg = Release|x86
+ {DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}.Release|Mixed Platforms.Build.0 = Release|x86
+ {DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}.Release|x86.ActiveCfg = Release|x86
+ {DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}.Release|x86.Build.0 = Release|x86
+ {9DAF7022-5820-4214-B13E-AC0A1B37691F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {9DAF7022-5820-4214-B13E-AC0A1B37691F}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {9DAF7022-5820-4214-B13E-AC0A1B37691F}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
+ {9DAF7022-5820-4214-B13E-AC0A1B37691F}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
+ {9DAF7022-5820-4214-B13E-AC0A1B37691F}.Debug|x86.ActiveCfg = Debug|Any CPU
+ {9DAF7022-5820-4214-B13E-AC0A1B37691F}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {9DAF7022-5820-4214-B13E-AC0A1B37691F}.Release|Any CPU.Build.0 = Release|Any CPU
+ {9DAF7022-5820-4214-B13E-AC0A1B37691F}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
+ {9DAF7022-5820-4214-B13E-AC0A1B37691F}.Release|Mixed Platforms.Build.0 = Release|Any CPU
+ {9DAF7022-5820-4214-B13E-AC0A1B37691F}.Release|x86.ActiveCfg = Release|Any CPU
+ {1A67421F-1671-4A73-A303-56881BA4C70F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {1A67421F-1671-4A73-A303-56881BA4C70F}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {1A67421F-1671-4A73-A303-56881BA4C70F}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
+ {1A67421F-1671-4A73-A303-56881BA4C70F}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
+ {1A67421F-1671-4A73-A303-56881BA4C70F}.Debug|x86.ActiveCfg = Debug|Any CPU
+ {1A67421F-1671-4A73-A303-56881BA4C70F}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {1A67421F-1671-4A73-A303-56881BA4C70F}.Release|Any CPU.Build.0 = Release|Any CPU
+ {1A67421F-1671-4A73-A303-56881BA4C70F}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
+ {1A67421F-1671-4A73-A303-56881BA4C70F}.Release|Mixed Platforms.Build.0 = Release|Any CPU
+ {1A67421F-1671-4A73-A303-56881BA4C70F}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
- {9DAF7022-5820-4214-B13E-AC0A1B37691F} = {1A88B962-9A09-4692-80D3-E88712A9E516}
{DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55} = {1A88B962-9A09-4692-80D3-E88712A9E516}
+ {9DAF7022-5820-4214-B13E-AC0A1B37691F} = {1A88B962-9A09-4692-80D3-E88712A9E516}
+ {1A67421F-1671-4A73-A303-56881BA4C70F} = {1A88B962-9A09-4692-80D3-E88712A9E516}
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
$0.CSharpFormattingPolicy = $3
@@ -126,6 +126,7 @@
</Compile>
<Compile Include="CassandraCqlRowSchema.cs" />
<Compile Include="FluentCqlRow.cs" />
+ <Compile Include="ILoadable.cs" />
<Compile Include="ObjectSerializer\ObjectSerializerConventions.cs" />
<Compile Include="System\DateTimePrecise.cs" />
<Compile Include="FluentCassandraException.cs" />
View
@@ -194,5 +194,10 @@ private void UpdateParent(FluentColumnParent parent)
{
_parent = parent;
}
+
+ public override string ToString()
+ {
+ return String.Format("{0} = {1}", ColumnName, ColumnValue);
+ }
}
}
@@ -248,5 +248,10 @@ public override bool TrySetColumn(object name, object value)
return true;
}
+
+ public override string ToString()
+ {
+ return String.Format("{0} - {1}", FamilyName, Key);
+ }
}
}
@@ -29,11 +29,19 @@ public FluentColumnList(FluentColumnParent parent, IEnumerable<T> columns)
Parent = parent;
Columns = new List<T>();
+ SupressChangeNotification = true;
+
// make sure all columns have the same parent
foreach (var col in columns)
{
+ if (col is ILoadable)
+ ((ILoadable)col).BeginLoad();
+
col.SetParent(parent);
Columns.Add(col);
+
+ if (col is ILoadable)
+ ((ILoadable)col).EndLoad();
}
SupressChangeNotification = false;
@@ -26,5 +26,10 @@ internal FluentMutation()
///
/// </summary>
public DateTimeOffset ColumnTimestamp { get; private set; }
+
+ public override string ToString()
+ {
+ return String.Format("{0} - {1}", Type, Column);
+ }
}
}
@@ -1,4 +1,5 @@
using System;
+using System.Linq;
using System.Collections.Generic;
namespace FluentCassandra
@@ -15,15 +16,30 @@ protected internal FluentMutationTracker(IFluentRecord parentRecord)
public IFluentRecord ParentRecord { get; private set; }
- public void ColumnMutated(MutationType type, IFluentBaseColumn column)
+ public virtual void ColumnMutated(MutationType type, IFluentBaseColumn column)
{
+ if (ParentRecord is FluentSuperColumn)
+ {
+ var superColumn = (FluentSuperColumn)ParentRecord;
+ var superColumnFamilyMutationTracker = superColumn.Family.MutationTracker;
+
+ // check to see if there is a mutation for this column already, so we don't create duplicate mutations
+ if (!superColumnFamilyMutationTracker.GetMutations().Any(x => x.Column.ColumnName == superColumn.ColumnName))
+ superColumnFamilyMutationTracker.ColumnMutated(MutationType.Changed, superColumn);
+ }
+
_mutation.Add(new FluentMutation {
Type = type,
Column = column
});
}
- public void Clear()
+ protected internal void Remove(FluentMutation mutation)
+ {
+ _mutation.Remove(mutation);
+ }
+
+ public virtual void Clear()
{
_mutation.Clear();
}
View
@@ -10,7 +10,7 @@ namespace FluentCassandra
///
/// </summary>
/// <typeparam name="T">A type that impliments <see cref="IFluentColumn"/>.</typeparam>
- public abstract class FluentRecord<T> : DynamicObject, IFluentRecord, INotifyPropertyChanged, IEnumerable<IFluentBaseColumn>
+ public abstract class FluentRecord<T> : DynamicObject, IFluentRecord, INotifyPropertyChanged, IEnumerable<IFluentBaseColumn>, ILoadable
where T : IFluentBaseColumn
{
/// <summary>
@@ -19,6 +19,7 @@ public abstract class FluentRecord<T> : DynamicObject, IFluentRecord, INotifyPro
public FluentRecord()
{
MutationTracker = new FluentMutationTracker(this);
+ SuppressMutationTracking = false;
}
/// <summary>
@@ -106,6 +107,24 @@ public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object
[EditorBrowsable(EditorBrowsableState.Never)]
public abstract bool TrySetColumn(object name, object value);
+ private bool SuppressMutationTracking { get; set; }
+
+ void ILoadable.BeginLoad()
+ {
+ SuppressMutationTracking = true;
+
+ if (Columns is FluentColumnList<T>)
+ ((FluentColumnList<T>)Columns).SupressChangeNotification = true;
+ }
+
+ void ILoadable.EndLoad()
+ {
+ SuppressMutationTracking = false;
+
+ if (Columns is FluentColumnList<T>)
+ ((FluentColumnList<T>)Columns).SupressChangeNotification = false;
+ }
+
#region IEnumerable<IFluentBaseColumn> Members
public IEnumerator<IFluentBaseColumn> GetEnumerator()
@@ -166,6 +185,9 @@ protected void ResetMutation()
protected void ResetMutationAndAddAllColumns()
{
+ if (SuppressMutationTracking)
+ return;
+
ResetMutation();
foreach (var col in Columns)
MutationTracker.ColumnMutated(MutationType.Added, col);
@@ -275,5 +275,10 @@ private void UpdateParent(FluentColumnParent parent)
IFluentBaseColumnFamily IFluentBaseColumn.Family { get { return Family; } }
#endregion
+
+ public override string ToString()
+ {
+ return String.Format("{0} = {1} columns", ColumnName, Columns.Count);
+ }
}
}
@@ -38,7 +38,6 @@ public FluentSuperColumnFamily(CassandraObject key, string columnFamily, Cassand
FamilyName = columnFamily;
_columns = new FluentColumnList<FluentSuperColumn>(GetSelf());
-
}
/// <summary>
@@ -251,5 +250,10 @@ public override bool TrySetColumn(object name, object value)
return true;
}
+
+ public override string ToString()
+ {
+ return String.Format("{0} - {1}", FamilyName, Key);
+ }
}
}
View
@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace FluentCassandra
+{
+ public interface ILoadable
+ {
+ void BeginLoad();
+ void EndLoad();
+ }
+}
@@ -231,8 +231,10 @@ public static FluentSuperColumn ConvertSuperColumnToFluentCounterSuperColumn(Cou
ColumnName = CassandraObject.GetCassandraObjectFromDatabaseByteArray(col.Name, superColSchema.NameType)
};
+ ((ILoadable)superCol).BeginLoad();
foreach (var xcol in col.Columns)
superCol.Columns.Add(ConvertColumnToFluentCounterColumn(xcol, schema));
+ ((ILoadable)superCol).EndLoad();
return superCol;
}
@@ -254,8 +256,10 @@ public static FluentSuperColumn ConvertSuperColumnToFluentSuperColumn(SuperColum
ColumnName = CassandraObject.GetCassandraObjectFromDatabaseByteArray(col.Name, superColSchema.NameType)
};
+ ((ILoadable)superCol).BeginLoad();
foreach (var xcol in col.Columns)
superCol.Columns.Add(ConvertColumnToFluentColumn(xcol, schema));
+ ((ILoadable)superCol).EndLoad();
return superCol;
}
@@ -299,13 +303,37 @@ public static Mutation CreateInsertedOrChangedMutation(FluentMutation mutation)
{
case MutationType.Added:
case MutationType.Changed:
- return new Mutation {
- Column_or_supercolumn = CreateColumnOrSuperColumn(mutation.Column)
- };
-
- default:
- return null;
+ var column = mutation.Column;
+
+ if (column is FluentColumn)
+ {
+ return new Mutation {
+ Column_or_supercolumn = new ColumnOrSuperColumn {
+ Column = CreateColumn((FluentColumn)column)
+ }
+ };
+ }
+ else if (column is FluentSuperColumn)
+ {
+ var colY = (FluentSuperColumn)column;
+ var superColumn = new SuperColumn {
+ Name = colY.ColumnName.TryToBigEndian(),
+ Columns = new List<Column>()
+ };
+
+ foreach (var col in colY.MutationTracker.GetMutations().Select(x => x.Column).OfType<FluentColumn>())
+ superColumn.Columns.Add(CreateColumn(col));
+
+ return new Mutation {
+ Column_or_supercolumn = new ColumnOrSuperColumn {
+ Super_column = superColumn
+ }
+ };
+ }
+ break;
}
+
+ return null;
}
public static Column CreateColumn(FluentColumn column)
@@ -47,25 +47,17 @@ public override IEnumerable<FluentSuperColumnFamily> Execute()
var superCol = new FluentSuperColumn(superColSchema, result.Value.Select(col => Helper.ConvertColumnToFluentColumn(col.Column, schema)));
ColumnFamily.Context.Attach(superCol);
- superCol.MutationTracker.Clear();
superColumns = new[] { superCol };
}
else
{
- superColumns = result.Value.Select(col => {
- var superCol = Helper.ConvertSuperColumnToFluentSuperColumn(col.Super_column, schema);
- ColumnFamily.Context.Attach(superCol);
- superCol.MutationTracker.Clear();
-
- return superCol;
- });
+ superColumns = result.Value.Select(col => Helper.ConvertSuperColumnToFluentSuperColumn(col.Super_column, schema));
}
var familyName = ColumnFamily.FamilyName;
var r = new FluentSuperColumnFamily(key, familyName, schema, superColumns);
ColumnFamily.Context.Attach(r);
- r.MutationTracker.Clear();
yield return r;
}
@@ -108,7 +108,7 @@ public DateTimeOffset GetNowOffset()
return new DateTimeOffset(GetNow());
}
- private sealed struct DateTimePreciseSafeImmutable
+ private struct DateTimePreciseSafeImmutable
{
public DateTimePreciseSafeImmutable(DateTime observedTime, DateTime baseTime, long observedTicks, long stopWatchFrequency)
{
@@ -407,6 +407,7 @@ private static void CreateColumnFamilyWithTimestampOperator()
}
}
#endregion
+
#region TombstoneTest
private static void TombstoneTest()
{

0 comments on commit 2c77dfc

Please sign in to comment.