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

Commit d8f3478

Browse files
author
Ovan Crone
committed
Bug fix for cachehash
1 parent da6cc3c commit d8f3478

File tree

11 files changed

+106
-68
lines changed

11 files changed

+106
-68
lines changed

src/Susanoo.Core.Tests/Dynamic/DynamicTypeTest.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Data;
55
using System.Linq;
66
using NUnit.Framework;
7+
using Susanoo.Pipeline.Command.ResultSets.Processing.Deserialization;
78

89
#endregion
910

@@ -18,20 +19,23 @@ public class DynamicTypeTest
1819
[Test]
1920
public void DynamicRowPerformance()
2021
{
21-
var results = CommandManager.DefineCommand("SELECT * FROM #DataTypeTable;", CommandType.Text)
22-
.DefineResults<dynamic>()
23-
.Realize("DynamicDataTypeTest")
24-
.Execute(_databaseManager);
25-
26-
Assert.IsNotNull(results);
22+
for (int i = 0; i < 500; i ++)
23+
{
24+
var results = CommandManager.DefineCommand("SELECT * FROM #DataTypeTable;", CommandType.Text)
25+
.DefineResults<dynamic>()
26+
.Realize()
27+
.Execute(_databaseManager);
28+
29+
Assert.IsNotNull(results);
30+
}
2731
}
2832

2933
[Test(Description = "Tests that dynamic results correctly map data to CLR types.")]
3034
public void DynamicResultDataTypes()
3135
{
3236
var results = CommandManager.DefineCommand("SELECT * FROM #DataTypeTable;", CommandType.Text)
3337
.DefineResults<dynamic>()
34-
.Realize("DynamicDataTypeTest")
38+
.Realize()
3539
.Execute(_databaseManager);
3640

3741
Assert.IsNotNull(results);

src/Susanoo.Core.Tests/Static/MultipleResults/IdenticalResults.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public void IdenticalResults7Test()
231231
}
232232

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

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

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

265265
var results = CommandManager.DefineCommand("SELECT * FROM #DataTypeTable;" +

src/Susanoo.Core.Tests/Static/SingleResult/StaticTypeTest.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@ public class StaticTypeTest
2323
[Test]
2424
public void StaticRowPerformance()
2525
{
26-
var results = CommandManager.DefineCommand("SELECT * FROM #DataTypeTable;", CommandType.Text)
27-
.DefineResults<TypeTestModel>()
28-
.Realize("StaticDataTypeTest")
29-
.Execute(_databaseManager);
26+
for (int i = 0; i < 500; i ++)
27+
{
28+
var results = CommandManager.DefineCommand("SELECT * FROM #DataTypeTable;", CommandType.Text)
29+
.DefineResults<TypeTestModel>()
30+
.Realize()
31+
.Execute(_databaseManager);
3032

31-
Assert.IsNotNull(results);
33+
Assert.IsNotNull(results);
34+
}
3235

3336
}
3437

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

4346
Assert.IsNotNull(results);

src/Susanoo.Core/Pipeline/Command/CommandExpression.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -452,19 +452,20 @@ public ICommandResultExpression<TFilter, TResult1, TResult2, TResult3, TResult4,
452452
/// </summary>
453453
private void ComputeHash()
454454
{
455-
var hashText = new StringBuilder(CommandText);
456-
457-
hashText.Append(DbCommandType);
458-
hashText.Append(_explicitInclusionMode);
459-
hashText.Append(_nullValueMode);
460-
hashText.Append(_constantParameters.Aggregate(string.Empty, (p, c) => p + c.Key));
461-
hashText.Append(_parameterInclusions.Aggregate(string.Empty, (p, c) => p + c.Key));
462-
hashText.Append(_parameterExclusions.Aggregate(string.Empty, (p, c) => p + c));
463-
464-
//string resultBeforeHash = hashText.ToString();
465-
//BigInteger hashCode = HashBuilder.Compute(resultBeforeHash);
455+
//This is faster than string builder and less resource intensive
456+
var strings =
457+
string.Concat(_constantParameters.Select(c => c.Key)
458+
.Concat(_parameterInclusions.Select(c => c.Key))
459+
.Concat(_parameterExclusions)
460+
.Concat(new []
461+
{
462+
CommandText,
463+
DbCommandType.ToString(),
464+
_explicitInclusionMode.ToString(),
465+
_nullValueMode.ToString()
466+
}));
466467

467-
_cacheHash = HashBuilder.Compute(hashText.ToString());
468+
_cacheHash = HashBuilder.Compute(strings);
468469
}
469470

470471
/// <summary>

src/Susanoo.Core/Pipeline/Command/ResultSets/CommandResultExpression.cs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ internal CommandResultExpression(ICommandExpression<TFilter> command,
121121
/// <value>The cache hash.</value>
122122
public override BigInteger CacheHash
123123
{
124-
get { return (base.CacheHash * 31) ^ typeof(TResult).AssemblyQualifiedName.GetHashCode(); }
124+
get { return (base.CacheHash * 31) ^ typeof(TResult).AssemblyQualifiedName.GetHashCode()
125+
^ this.GetType().AssemblyQualifiedName.GetHashCode(); }
125126
}
126127

127128
/// <summary>
@@ -215,7 +216,8 @@ public override BigInteger CacheHash
215216
{
216217
return (base.CacheHash * 31)
217218
^ typeof(TResult1).AssemblyQualifiedName.GetHashCode()
218-
^ typeof(TResult2).AssemblyQualifiedName.GetHashCode();
219+
^ typeof(TResult2).AssemblyQualifiedName.GetHashCode()
220+
^ this.GetType().AssemblyQualifiedName.GetHashCode();
219221
}
220222
}
221223

@@ -285,9 +287,10 @@ public override BigInteger CacheHash
285287
get
286288
{
287289
return (base.CacheHash * 31)
288-
^ HashBuilder.Compute(typeof(TResult1).AssemblyQualifiedName)
289-
^ HashBuilder.Compute(typeof(TResult2).AssemblyQualifiedName)
290-
^ HashBuilder.Compute(typeof(TResult3).AssemblyQualifiedName);
290+
^ typeof(TResult1).AssemblyQualifiedName.GetHashCode()
291+
^ typeof(TResult2).AssemblyQualifiedName.GetHashCode()
292+
^ typeof(TResult3).AssemblyQualifiedName.GetHashCode()
293+
^ this.GetType().AssemblyQualifiedName.GetHashCode();
291294
}
292295
}
293296

@@ -359,10 +362,11 @@ public override BigInteger CacheHash
359362
get
360363
{
361364
return (base.CacheHash * 31)
362-
^ HashBuilder.Compute(typeof(TResult1).AssemblyQualifiedName)
363-
^ HashBuilder.Compute(typeof(TResult2).AssemblyQualifiedName)
364-
^ HashBuilder.Compute(typeof(TResult3).AssemblyQualifiedName)
365-
^ HashBuilder.Compute(typeof(TResult4).AssemblyQualifiedName);
365+
^ typeof(TResult1).AssemblyQualifiedName.GetHashCode()
366+
^ typeof(TResult2).AssemblyQualifiedName.GetHashCode()
367+
^ typeof(TResult3).AssemblyQualifiedName.GetHashCode()
368+
^ typeof(TResult4).AssemblyQualifiedName.GetHashCode()
369+
^ this.GetType().AssemblyQualifiedName.GetHashCode();
366370
}
367371
}
368372

@@ -438,11 +442,12 @@ public override BigInteger CacheHash
438442
get
439443
{
440444
return (base.CacheHash * 31)
441-
^ HashBuilder.Compute(typeof(TResult1).AssemblyQualifiedName)
442-
^ HashBuilder.Compute(typeof(TResult2).AssemblyQualifiedName)
443-
^ HashBuilder.Compute(typeof(TResult3).AssemblyQualifiedName)
444-
^ HashBuilder.Compute(typeof(TResult4).AssemblyQualifiedName)
445-
^ HashBuilder.Compute(typeof(TResult5).AssemblyQualifiedName);
445+
^ typeof(TResult1).AssemblyQualifiedName.GetHashCode()
446+
^ typeof(TResult2).AssemblyQualifiedName.GetHashCode()
447+
^ typeof(TResult3).AssemblyQualifiedName.GetHashCode()
448+
^ typeof(TResult4).AssemblyQualifiedName.GetHashCode()
449+
^ typeof(TResult5).AssemblyQualifiedName.GetHashCode()
450+
^ this.GetType().AssemblyQualifiedName.GetHashCode();
446451
}
447452
}
448453

@@ -524,12 +529,13 @@ public override BigInteger CacheHash
524529
get
525530
{
526531
return (base.CacheHash * 31)
527-
^ HashBuilder.Compute(typeof(TResult1).AssemblyQualifiedName)
528-
^ HashBuilder.Compute(typeof(TResult2).AssemblyQualifiedName)
529-
^ HashBuilder.Compute(typeof(TResult3).AssemblyQualifiedName)
530-
^ HashBuilder.Compute(typeof(TResult4).AssemblyQualifiedName)
531-
^ HashBuilder.Compute(typeof(TResult5).AssemblyQualifiedName)
532-
^ HashBuilder.Compute(typeof(TResult6).AssemblyQualifiedName);
532+
^ typeof(TResult1).AssemblyQualifiedName.GetHashCode()
533+
^ typeof(TResult2).AssemblyQualifiedName.GetHashCode()
534+
^ typeof(TResult3).AssemblyQualifiedName.GetHashCode()
535+
^ typeof(TResult4).AssemblyQualifiedName.GetHashCode()
536+
^ typeof(TResult5).AssemblyQualifiedName.GetHashCode()
537+
^ typeof(TResult6).AssemblyQualifiedName.GetHashCode()
538+
^ this.GetType().AssemblyQualifiedName.GetHashCode();
533539
}
534540
}
535541

@@ -617,13 +623,14 @@ public override BigInteger CacheHash
617623
get
618624
{
619625
return (base.CacheHash * 31)
620-
^ HashBuilder.Compute(typeof(TResult1).AssemblyQualifiedName)
621-
^ HashBuilder.Compute(typeof(TResult2).AssemblyQualifiedName)
622-
^ HashBuilder.Compute(typeof(TResult3).AssemblyQualifiedName)
623-
^ HashBuilder.Compute(typeof(TResult4).AssemblyQualifiedName)
624-
^ HashBuilder.Compute(typeof(TResult5).AssemblyQualifiedName)
625-
^ HashBuilder.Compute(typeof(TResult6).AssemblyQualifiedName)
626-
^ HashBuilder.Compute(typeof(TResult7).AssemblyQualifiedName);
626+
^ typeof(TResult1).AssemblyQualifiedName.GetHashCode()
627+
^ typeof(TResult2).AssemblyQualifiedName.GetHashCode()
628+
^ typeof(TResult3).AssemblyQualifiedName.GetHashCode()
629+
^ typeof(TResult4).AssemblyQualifiedName.GetHashCode()
630+
^ typeof(TResult5).AssemblyQualifiedName.GetHashCode()
631+
^ typeof(TResult6).AssemblyQualifiedName.GetHashCode()
632+
^ typeof(TResult7).AssemblyQualifiedName.GetHashCode()
633+
^ this.GetType().AssemblyQualifiedName.GetHashCode();
627634
}
628635
}
629636

src/Susanoo.Core/Pipeline/Command/ResultSets/CommandResultImplementor.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ namespace Susanoo.Pipeline.Command.ResultSets
1818
public class CommandResultImplementor<TFilter> : ICommandResultImplementor<TFilter>
1919
{
2020
private readonly IDictionary<Type, IResultMappingExport> _mappingContainer;
21-
21+
private readonly IDictionary<Type, IResultMappingExport> _mappingContainerRuntime;
2222
/// <summary>
2323
/// Initializes a new instance of the <see cref="CommandResultImplementor{TFilter}" /> class.
2424
/// </summary>
2525
public CommandResultImplementor()
2626
{
2727
_mappingContainer = new Dictionary<Type, IResultMappingExport>();
28+
_mappingContainerRuntime = new Dictionary<Type, IResultMappingExport>();
2829
}
2930

3031
/// <summary>
@@ -33,7 +34,11 @@ public CommandResultImplementor()
3334
/// <value>The cache hash.</value>
3435
public BigInteger CacheHash
3536
{
36-
get { return _mappingContainer.Aggregate(default(BigInteger), (p, c) => (p * 31) ^ c.Value.CacheHash); }
37+
get
38+
{
39+
return _mappingContainer.Aggregate(default(BigInteger),
40+
(p, c) => ((p * 31) ^ c.Value.CacheHash));
41+
}
3742
}
3843

3944
/// <summary>
@@ -48,11 +53,14 @@ public IResultMappingExport RetrieveExporter(Type resultType)
4853
IResultMappingExport value;
4954
if (!_mappingContainer.TryGetValue(resultType, out value))
5055
{
51-
result = new DefaultResultMapping(resultType);
52-
_mappingContainer.Add(resultType, result);
56+
if (!_mappingContainerRuntime.TryGetValue(resultType, out value))
57+
{
58+
result = new DefaultResultMapping(resultType);
59+
_mappingContainerRuntime.Add(resultType, result);
60+
}
5361
}
5462

55-
return result;
63+
return result ?? value;
5664
}
5765

5866
/// <summary>

src/Susanoo.Core/Pipeline/Command/ResultSets/Mapping/DefaultResultMapping.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void MapDeclarativeProperties()
4949
/// <value>The cache hash.</value>
5050
public BigInteger CacheHash
5151
{
52-
get { return _mappingActions.Aggregate(HashBuilder.Seed, (i, pair) => (i * 31) ^ pair.Value.CacheHash); }
52+
get { return -1; }
5353
}
5454

5555
/// <summary>

src/Susanoo.Core/Pipeline/Command/ResultSets/Processing/Deserialization/BuiltInTypeDeserializer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ public static class BuiltInTypeDeserializer
1717
/// <returns>dynamic.</returns>
1818
public static IEnumerable<TResult> Deserialize<TResult>(IDataReader reader, ColumnChecker checker)
1919
{
20-
var resultSet = new List<object>();
20+
var resultSet = new ListResult<TResult>();
2121

2222
var fieldCount = reader.FieldCount;
2323
if (fieldCount > 0)
2424
{
2525
while (reader.Read())
2626
{
2727

28-
resultSet.Add(reader.GetValue(0));
28+
resultSet.Add((TResult)reader.GetValue(0));
2929
}
3030
}
3131

32-
return resultSet.Cast<TResult>();
32+
return resultSet;
3333
}
3434
}
3535
}

src/Susanoo.Core/Pipeline/Command/ResultSets/Processing/Deserialization/ComplexTypeDeserializer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@ public static Func<IDataReader, ColumnChecker, IEnumerable<TResult>> Compile<TRe
167167
{
168168
var result = Compile(mapping, resultType);
169169

170-
return (reader, columnMeta) => (IEnumerable<TResult>)(result(reader, columnMeta));
170+
Func<IDataReader, ColumnChecker, IEnumerable<TResult>> typedResult =
171+
(reader, columnMeta) => (IEnumerable<TResult>)(result.Invoke(reader, columnMeta));
172+
173+
return typedResult;
171174
}
172175
}
173176
}

src/Susanoo.Core/Pipeline/Command/ResultSets/Processing/Deserialization/DynamicRow.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ public DynamicRow(ColumnChecker columns)
2525
_values = new object[_columns.Count];
2626
}
2727

28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="DynamicRow"/> class.
30+
/// </summary>
31+
public DynamicRow()
32+
{
33+
_columns = new ColumnChecker();
34+
_values = new object[_columns.Count];
35+
}
36+
2837
/// <summary>
2938
/// Initializes a new instance of the <see cref="DynamicRow" /> class.
3039
/// </summary>

0 commit comments

Comments
 (0)