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

Commit

Permalink
Changed the pipeline to have caching done at the deserializer rqather…
Browse files Browse the repository at this point in the history
… than processor
  • Loading branch information
psibernetic committed Oct 8, 2015
1 parent aea1f4c commit 528109f
Show file tree
Hide file tree
Showing 58 changed files with 4,102 additions and 4,394 deletions.
14 changes: 7 additions & 7 deletions src/Susanoo.Core.Tests/Static/SingleResult/StaticTypeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,16 @@ public class StaticTypeTest
{
private readonly DatabaseManager _databaseManager = Setup.DatabaseManager;

private ISingleResultSetCommandProcessor<dynamic, TypeTestModel> results =
CommandManager.Instance.DefineCommand("SELECT TOP 1 * FROM #DataTypeTable;", CommandType.Text)
.DefineResults<TypeTestModel>()
.Realize();


[Test]
public void StaticRowPerformance()
{
for (int i = 0; i < 500; i++)
{
results.Execute(_databaseManager);
CommandManager.Instance
.DefineCommand("SELECT TOP 1 * FROM #DataTypeTable;", CommandType.Text)
.DefineResults<TypeTestModel>()
.Realize()
.Execute(_databaseManager);
}
}

Expand Down Expand Up @@ -102,6 +100,8 @@ public void StaticResultDataTypesInsert()
.ExecuteNonQuery(_databaseManager, result);
}



[Test(Description = "Tests that results correctly map data to CLR types.")]
public void StaticResultDataTypesInsertAnonymous()
{
Expand Down
29 changes: 0 additions & 29 deletions src/Susanoo.Core/Command/CommandExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;
using System.Numerics;
using System.Reflection;
using Susanoo.Processing;
using Susanoo.ResultSets;
Expand Down Expand Up @@ -201,13 +200,6 @@ public virtual DbParameter[] BuildParameters(IDatabaseManager databaseManager, T
/// <value>The CommandBuilder text.</value>
public string CommandText { get; set; }

/// <summary>
/// Gets the hash code used for caching result mapping compilations.
/// </summary>
/// <value>The cache hash.</value>
public virtual BigInteger CacheHash =>
ComputeHash();

/// <summary>
/// Realizes the pipeline with no result mappings.
/// </summary>
Expand Down Expand Up @@ -369,27 +361,6 @@ public ICommandMultipleResultExpression<TFilter> DefineResults(params Type[] res
.BuildCommandMultipleResultExpression(this, resultTypes);
}

/// <summary>
/// Computes the hash.
/// </summary>
private BigInteger ComputeHash()
{
//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()
}));

return HashBuilder.Compute(strings);
}


/// <summary>
/// Builds the property inclusion parameters.
Expand Down
4 changes: 4 additions & 0 deletions src/Susanoo.Core/Command/CommandExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public CommandExpressionFactory(
/// <param name="commandText">The command text.</param>
/// <param name="commandType">Type of the command.</param>
/// <returns>ICommandExpression&lt;TFilter&gt;.</returns>
/// <exception cref="ArgumentNullException">commandText</exception>
/// <exception cref="ArgumentException">No CommandBuilder text provided.;commandText
/// or
/// TableDirect is not supported.;commandType</exception>
public virtual ICommandExpression<TFilter> BuildCommandExpression<TFilter>(string commandText, CommandType commandType)
{
return new CommandExpression<TFilter>(
Expand Down
3 changes: 1 addition & 2 deletions src/Susanoo.Core/Command/ICommandBuilderInfo.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System.Collections.Generic;
using System.Data.Common;
using Susanoo.Pipeline;

namespace Susanoo.Command
{
/// <summary>
/// Basic details about a CommandBuilder and parameter building.
/// </summary>
public interface ICommandBuilderInfo<in TFilter>
: ICommandInfo, IFluentPipelineFragment
: ICommandInfo
{
/// <summary>
/// Builds the parameters.
Expand Down
2 changes: 0 additions & 2 deletions src/Susanoo.Core/Command/ICommandExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Data.Common;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using Susanoo.Pipeline;
using Susanoo.Processing;
using Susanoo.ResultSets;

Expand All @@ -18,7 +17,6 @@ namespace Susanoo.Command
/// </summary>
/// <typeparam name="TFilter">The type of the filter.</typeparam>
public interface ICommandExpression<TFilter>
: IFluentPipelineFragment
{
/// <summary>
/// Gets or sets the CommandBuilder text.
Expand Down
2 changes: 1 addition & 1 deletion src/Susanoo.Core/Command/IExecutableCommandInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface IExecutableCommandInfo
DbParameter[] Parameters { get; }

/// <summary>
/// Gets a deterministic key based.
/// Gets a deterministic key.
/// </summary>
/// <returns>System.string</returns>
string GetDeterministicKey();
Expand Down
102 changes: 21 additions & 81 deletions src/Susanoo.Core/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Reflection.Emit;
using Susanoo.Command;
using Susanoo.Processing;

#endregion

Expand All @@ -22,7 +19,7 @@ public class CommandManager
{
private CommandManager()
{

}

private static readonly IDictionary<Type, DbType> BuiltinTypeConversions =
Expand Down Expand Up @@ -64,12 +61,6 @@ private CommandManager()
{typeof (DateTimeOffset?), DbType.DateTimeOffset}
});

private readonly ConcurrentDictionary<BigInteger, ICommandProcessorWithResults> _registeredCommandProcessors =
new ConcurrentDictionary<BigInteger, ICommandProcessorWithResults>();

private readonly ConcurrentDictionary<string, ICommandProcessorWithResults> _namedCommandProcessors =
new ConcurrentDictionary<string, ICommandProcessorWithResults>();

/// <summary>
/// Gets the expression assembly that contains runtime compiled methods used for mappings.
/// </summary>
Expand All @@ -81,7 +72,7 @@ private CommandManager()
/// Gets the dynamic namespace.
/// </summary>
/// <value>The dynamic namespace.</value>
internal static ModuleBuilder DynamicNamespace { get; } =
internal static ModuleBuilder DynamicNamespace { get; } =
ExpressionAssembly
.DefineDynamicModule("Susanoo.DynamicExpression", "Susanoo.DynamicExpression.dll");

Expand Down Expand Up @@ -129,73 +120,6 @@ public ICommandExpression<dynamic> DefineCommand(string commandText, CommandType
return typeToUse;
}

/// <summary>
/// Attempts to get a CommandBuilder processor by hash code.
/// </summary>
/// <param name="hash">The hash.</param>
/// <param name="commandProcessor">The CommandBuilder processor.</param>
/// <returns><c>true</c> if a CommandBuilder processor with the same configuration has been registered and not garbage collected,
/// <c>false</c> otherwise.</returns>
public bool TryGetCommandProcessor(BigInteger hash, out ICommandProcessorWithResults commandProcessor)
{
var result = _registeredCommandProcessors.TryGetValue(hash, out commandProcessor);

return result;
}

/// <summary>
/// Attempts to get a CommandBuilder processor by name.
/// </summary>
/// <param name="name">The name of the processor.</param>
/// <param name="commandProcessor">The CommandBuilder processor.</param>
/// <returns><c>true</c> if a CommandBuilder processor with the same configuration has been registered and not garbage collected,
/// <c>false</c> otherwise.</returns>
public bool TryGetCommandProcessor(string name, out ICommandProcessorWithResults commandProcessor)
{
var result = _namedCommandProcessors.TryGetValue(name, out commandProcessor);

return result;
}

/// <summary>
/// Registers the CommandBuilder processor.
/// </summary>
/// <param name="processor">The processor.</param>
/// <param name="name">The name.</param>
/// <param name="hashCodeOverride">The hash code override.</param>
public void RegisterCommandProcessor(ICommandProcessorWithResults processor, string name = null, BigInteger hashCodeOverride = default(BigInteger))
{
var hash = hashCodeOverride != default(BigInteger) ? hashCodeOverride : processor.CacheHash;

_registeredCommandProcessors.TryAdd(hash, processor);

if (!string.IsNullOrWhiteSpace(name))
_namedCommandProcessors.TryAdd(name, processor);
}

/// <summary>
/// Clears any column index information that may have been cached.
/// </summary>
/// <param name="processor">The processor.</param>
/// <exception cref="System.ArgumentNullException">processor</exception>
public void ClearColumnIndexInfo(ICommandProcessorWithResults processor)
{
if (processor == null)
throw new ArgumentNullException(nameof(processor));

processor.ClearColumnIndexInfo();
}

/// <summary>
/// Clears any column index information that may have been cached.
/// </summary>
public void ClearColumnIndexInfo()
{
foreach (var processor in _registeredCommandProcessors
.Select(kvp => kvp.Value))
processor.ClearColumnIndexInfo();
}

/// <summary>
/// Saves the dynamic assembly to disk.
/// </summary>
Expand All @@ -208,6 +132,8 @@ public static void SaveDynamicAssemblyToDisk()

private static readonly object SyncRoot = new object();

private ISusanooBootstrapper _bootstrapper;

/// <summary>
/// Gets the instance.
/// </summary>
Expand Down Expand Up @@ -239,7 +165,9 @@ public void Bootstrap(ISusanooBootstrapper bootstrapper)
if (bootstrapper == null)
throw new ArgumentNullException(nameof(bootstrapper));

Bootstrapper = bootstrapper;
_bootstrapper = bootstrapper;

_bootstrapper.Initialize();
}

/// <summary>
Expand All @@ -257,7 +185,19 @@ public static IDatabaseManagerFactory ResolveDatabaseManagerFactory(string name
/// Gets the bootstrapper.
/// </summary>
/// <value>The bootstrapper.</value>
public ISusanooBootstrapper Bootstrapper { get; private set; } =
new SusanooBootstrapper();
public ISusanooBootstrapper Bootstrapper
{
get
{
if (_bootstrapper == null)
{
_bootstrapper = new SusanooBootstrapper();

_bootstrapper.Initialize();
}

return _bootstrapper;
}
}
}
}
Loading

0 comments on commit 528109f

Please sign in to comment.