Skip to content
This repository has been archived by the owner on Dec 23, 2017. It is now read-only.

Commit

Permalink
Bug fix for cachehash
Browse files Browse the repository at this point in the history
  • Loading branch information
Ovan Crone committed Feb 12, 2015
1 parent da6cc3c commit d8f3478
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 68 deletions.
18 changes: 11 additions & 7 deletions src/Susanoo.Core.Tests/Dynamic/DynamicTypeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Data;
using System.Linq;
using NUnit.Framework;
using Susanoo.Pipeline.Command.ResultSets.Processing.Deserialization;

#endregion

Expand All @@ -18,20 +19,23 @@ public class DynamicTypeTest
[Test]
public void DynamicRowPerformance()
{
var results = CommandManager.DefineCommand("SELECT * FROM #DataTypeTable;", CommandType.Text)
.DefineResults<dynamic>()
.Realize("DynamicDataTypeTest")
.Execute(_databaseManager);

Assert.IsNotNull(results);
for (int i = 0; i < 500; i ++)
{
var results = CommandManager.DefineCommand("SELECT * FROM #DataTypeTable;", CommandType.Text)
.DefineResults<dynamic>()
.Realize()
.Execute(_databaseManager);

Assert.IsNotNull(results);
}
}

[Test(Description = "Tests that dynamic results correctly map data to CLR types.")]
public void DynamicResultDataTypes()
{
var results = CommandManager.DefineCommand("SELECT * FROM #DataTypeTable;", CommandType.Text)
.DefineResults<dynamic>()
.Realize("DynamicDataTypeTest")
.Realize()
.Execute(_databaseManager);

Assert.IsNotNull(results);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public void IdenticalResults7Test()
}

[Test(Description = "Tests that attempting to get less results than available works fine.")]
public void LessResultsAreAvailableTest()
public void LessResultsThanAvailableTest()
{

var results = CommandManager.DefineCommand("SELECT * FROM #DataTypeTable;" +
Expand Down Expand Up @@ -259,7 +259,7 @@ public void LessResultsAreAvailableTest()
}

[Test(Description = "Tests that attempting to get more results than available provides null for the additional results.")]
public void MoreResultsAreAvailableTest()
public void MoreResultsThanAvailableTest()
{

var results = CommandManager.DefineCommand("SELECT * FROM #DataTypeTable;" +
Expand Down
15 changes: 9 additions & 6 deletions src/Susanoo.Core.Tests/Static/SingleResult/StaticTypeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ public class StaticTypeTest
[Test]
public void StaticRowPerformance()
{
var results = CommandManager.DefineCommand("SELECT * FROM #DataTypeTable;", CommandType.Text)
.DefineResults<TypeTestModel>()
.Realize("StaticDataTypeTest")
.Execute(_databaseManager);
for (int i = 0; i < 500; i ++)
{
var results = CommandManager.DefineCommand("SELECT * FROM #DataTypeTable;", CommandType.Text)
.DefineResults<TypeTestModel>()
.Realize()
.Execute(_databaseManager);

Assert.IsNotNull(results);
Assert.IsNotNull(results);
}

}

Expand All @@ -37,7 +40,7 @@ public void StaticResultDataTypes()
{
var results = CommandManager.DefineCommand("SELECT * FROM #DataTypeTable;", CommandType.Text)
.DefineResults<TypeTestModel>()
.Realize("StaticDataTypeTest")
.Realize()
.Execute(_databaseManager);

Assert.IsNotNull(results);
Expand Down
25 changes: 13 additions & 12 deletions src/Susanoo.Core/Pipeline/Command/CommandExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,19 +452,20 @@ public ICommandResultExpression<TFilter, TResult1, TResult2, TResult3, TResult4,
/// </summary>
private void ComputeHash()
{
var hashText = new StringBuilder(CommandText);

hashText.Append(DbCommandType);
hashText.Append(_explicitInclusionMode);
hashText.Append(_nullValueMode);
hashText.Append(_constantParameters.Aggregate(string.Empty, (p, c) => p + c.Key));
hashText.Append(_parameterInclusions.Aggregate(string.Empty, (p, c) => p + c.Key));
hashText.Append(_parameterExclusions.Aggregate(string.Empty, (p, c) => p + c));

//string resultBeforeHash = hashText.ToString();
//BigInteger hashCode = HashBuilder.Compute(resultBeforeHash);
//This is faster than string builder and less resource intensive
var strings =
string.Concat(_constantParameters.Select(c => c.Key)
.Concat(_parameterInclusions.Select(c => c.Key))
.Concat(_parameterExclusions)
.Concat(new []
{
CommandText,
DbCommandType.ToString(),
_explicitInclusionMode.ToString(),
_nullValueMode.ToString()
}));

_cacheHash = HashBuilder.Compute(hashText.ToString());
_cacheHash = HashBuilder.Compute(strings);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ internal CommandResultExpression(ICommandExpression<TFilter> command,
/// <value>The cache hash.</value>
public override BigInteger CacheHash
{
get { return (base.CacheHash * 31) ^ typeof(TResult).AssemblyQualifiedName.GetHashCode(); }
get { return (base.CacheHash * 31) ^ typeof(TResult).AssemblyQualifiedName.GetHashCode()
^ this.GetType().AssemblyQualifiedName.GetHashCode(); }
}

/// <summary>
Expand Down Expand Up @@ -215,7 +216,8 @@ public override BigInteger CacheHash
{
return (base.CacheHash * 31)
^ typeof(TResult1).AssemblyQualifiedName.GetHashCode()
^ typeof(TResult2).AssemblyQualifiedName.GetHashCode();
^ typeof(TResult2).AssemblyQualifiedName.GetHashCode()
^ this.GetType().AssemblyQualifiedName.GetHashCode();
}
}

Expand Down Expand Up @@ -285,9 +287,10 @@ public override BigInteger CacheHash
get
{
return (base.CacheHash * 31)
^ HashBuilder.Compute(typeof(TResult1).AssemblyQualifiedName)
^ HashBuilder.Compute(typeof(TResult2).AssemblyQualifiedName)
^ HashBuilder.Compute(typeof(TResult3).AssemblyQualifiedName);
^ typeof(TResult1).AssemblyQualifiedName.GetHashCode()
^ typeof(TResult2).AssemblyQualifiedName.GetHashCode()
^ typeof(TResult3).AssemblyQualifiedName.GetHashCode()
^ this.GetType().AssemblyQualifiedName.GetHashCode();
}
}

Expand Down Expand Up @@ -359,10 +362,11 @@ public override BigInteger CacheHash
get
{
return (base.CacheHash * 31)
^ HashBuilder.Compute(typeof(TResult1).AssemblyQualifiedName)
^ HashBuilder.Compute(typeof(TResult2).AssemblyQualifiedName)
^ HashBuilder.Compute(typeof(TResult3).AssemblyQualifiedName)
^ HashBuilder.Compute(typeof(TResult4).AssemblyQualifiedName);
^ typeof(TResult1).AssemblyQualifiedName.GetHashCode()
^ typeof(TResult2).AssemblyQualifiedName.GetHashCode()
^ typeof(TResult3).AssemblyQualifiedName.GetHashCode()
^ typeof(TResult4).AssemblyQualifiedName.GetHashCode()
^ this.GetType().AssemblyQualifiedName.GetHashCode();
}
}

Expand Down Expand Up @@ -438,11 +442,12 @@ public override BigInteger CacheHash
get
{
return (base.CacheHash * 31)
^ HashBuilder.Compute(typeof(TResult1).AssemblyQualifiedName)
^ HashBuilder.Compute(typeof(TResult2).AssemblyQualifiedName)
^ HashBuilder.Compute(typeof(TResult3).AssemblyQualifiedName)
^ HashBuilder.Compute(typeof(TResult4).AssemblyQualifiedName)
^ HashBuilder.Compute(typeof(TResult5).AssemblyQualifiedName);
^ typeof(TResult1).AssemblyQualifiedName.GetHashCode()
^ typeof(TResult2).AssemblyQualifiedName.GetHashCode()
^ typeof(TResult3).AssemblyQualifiedName.GetHashCode()
^ typeof(TResult4).AssemblyQualifiedName.GetHashCode()
^ typeof(TResult5).AssemblyQualifiedName.GetHashCode()
^ this.GetType().AssemblyQualifiedName.GetHashCode();
}
}

Expand Down Expand Up @@ -524,12 +529,13 @@ public override BigInteger CacheHash
get
{
return (base.CacheHash * 31)
^ HashBuilder.Compute(typeof(TResult1).AssemblyQualifiedName)
^ HashBuilder.Compute(typeof(TResult2).AssemblyQualifiedName)
^ HashBuilder.Compute(typeof(TResult3).AssemblyQualifiedName)
^ HashBuilder.Compute(typeof(TResult4).AssemblyQualifiedName)
^ HashBuilder.Compute(typeof(TResult5).AssemblyQualifiedName)
^ HashBuilder.Compute(typeof(TResult6).AssemblyQualifiedName);
^ typeof(TResult1).AssemblyQualifiedName.GetHashCode()
^ typeof(TResult2).AssemblyQualifiedName.GetHashCode()
^ typeof(TResult3).AssemblyQualifiedName.GetHashCode()
^ typeof(TResult4).AssemblyQualifiedName.GetHashCode()
^ typeof(TResult5).AssemblyQualifiedName.GetHashCode()
^ typeof(TResult6).AssemblyQualifiedName.GetHashCode()
^ this.GetType().AssemblyQualifiedName.GetHashCode();
}
}

Expand Down Expand Up @@ -617,13 +623,14 @@ public override BigInteger CacheHash
get
{
return (base.CacheHash * 31)
^ HashBuilder.Compute(typeof(TResult1).AssemblyQualifiedName)
^ HashBuilder.Compute(typeof(TResult2).AssemblyQualifiedName)
^ HashBuilder.Compute(typeof(TResult3).AssemblyQualifiedName)
^ HashBuilder.Compute(typeof(TResult4).AssemblyQualifiedName)
^ HashBuilder.Compute(typeof(TResult5).AssemblyQualifiedName)
^ HashBuilder.Compute(typeof(TResult6).AssemblyQualifiedName)
^ HashBuilder.Compute(typeof(TResult7).AssemblyQualifiedName);
^ typeof(TResult1).AssemblyQualifiedName.GetHashCode()
^ typeof(TResult2).AssemblyQualifiedName.GetHashCode()
^ typeof(TResult3).AssemblyQualifiedName.GetHashCode()
^ typeof(TResult4).AssemblyQualifiedName.GetHashCode()
^ typeof(TResult5).AssemblyQualifiedName.GetHashCode()
^ typeof(TResult6).AssemblyQualifiedName.GetHashCode()
^ typeof(TResult7).AssemblyQualifiedName.GetHashCode()
^ this.GetType().AssemblyQualifiedName.GetHashCode();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ namespace Susanoo.Pipeline.Command.ResultSets
public class CommandResultImplementor<TFilter> : ICommandResultImplementor<TFilter>
{
private readonly IDictionary<Type, IResultMappingExport> _mappingContainer;

private readonly IDictionary<Type, IResultMappingExport> _mappingContainerRuntime;
/// <summary>
/// Initializes a new instance of the <see cref="CommandResultImplementor{TFilter}" /> class.
/// </summary>
public CommandResultImplementor()
{
_mappingContainer = new Dictionary<Type, IResultMappingExport>();
_mappingContainerRuntime = new Dictionary<Type, IResultMappingExport>();
}

/// <summary>
Expand All @@ -33,7 +34,11 @@ public CommandResultImplementor()
/// <value>The cache hash.</value>
public BigInteger CacheHash
{
get { return _mappingContainer.Aggregate(default(BigInteger), (p, c) => (p * 31) ^ c.Value.CacheHash); }
get
{
return _mappingContainer.Aggregate(default(BigInteger),
(p, c) => ((p * 31) ^ c.Value.CacheHash));
}
}

/// <summary>
Expand All @@ -48,11 +53,14 @@ public IResultMappingExport RetrieveExporter(Type resultType)
IResultMappingExport value;
if (!_mappingContainer.TryGetValue(resultType, out value))
{
result = new DefaultResultMapping(resultType);
_mappingContainer.Add(resultType, result);
if (!_mappingContainerRuntime.TryGetValue(resultType, out value))
{
result = new DefaultResultMapping(resultType);
_mappingContainerRuntime.Add(resultType, result);
}
}

return result;
return result ?? value;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void MapDeclarativeProperties()
/// <value>The cache hash.</value>
public BigInteger CacheHash
{
get { return _mappingActions.Aggregate(HashBuilder.Seed, (i, pair) => (i * 31) ^ pair.Value.CacheHash); }
get { return -1; }
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ public static class BuiltInTypeDeserializer
/// <returns>dynamic.</returns>
public static IEnumerable<TResult> Deserialize<TResult>(IDataReader reader, ColumnChecker checker)
{
var resultSet = new List<object>();
var resultSet = new ListResult<TResult>();

var fieldCount = reader.FieldCount;
if (fieldCount > 0)
{
while (reader.Read())
{

resultSet.Add(reader.GetValue(0));
resultSet.Add((TResult)reader.GetValue(0));
}
}

return resultSet.Cast<TResult>();
return resultSet;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ public static Func<IDataReader, ColumnChecker, IEnumerable<TResult>> Compile<TRe
{
var result = Compile(mapping, resultType);

return (reader, columnMeta) => (IEnumerable<TResult>)(result(reader, columnMeta));
Func<IDataReader, ColumnChecker, IEnumerable<TResult>> typedResult =
(reader, columnMeta) => (IEnumerable<TResult>)(result.Invoke(reader, columnMeta));

return typedResult;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ public DynamicRow(ColumnChecker columns)
_values = new object[_columns.Count];
}

/// <summary>
/// Initializes a new instance of the <see cref="DynamicRow"/> class.
/// </summary>
public DynamicRow()
{
_columns = new ColumnChecker();
_values = new object[_columns.Count];
}

/// <summary>
/// Initializes a new instance of the <see cref="DynamicRow" /> class.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections;
using System.Collections.Generic;
using System.Data;

namespace Susanoo.Pipeline.Command.ResultSets.Processing.Deserialization
Expand All @@ -16,7 +17,9 @@ public static class DynamicRowDeserializer
/// <returns>dynamic.</returns>
public static IEnumerable<TResult> Deserialize<TResult>(IDataReader reader, ColumnChecker checker)
{
var resultSet = new ListResult<object>();
IList resultSet = new ListResult<TResult>();


checker = checker ?? new ColumnChecker();

var fieldCount = reader.FieldCount;
Expand Down Expand Up @@ -46,9 +49,9 @@ public static IEnumerable<TResult> Deserialize<TResult>(IDataReader reader, Colu
resultSet.Add(new DynamicRow(checker, values));
}

resultSet.BuildReport(checker);
((ListResult<TResult>)resultSet).BuildReport(checker);

return resultSet as IEnumerable<TResult>;
return (IEnumerable<TResult>) resultSet;
}
}
}

0 comments on commit d8f3478

Please sign in to comment.