Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewKostousov committed Aug 14, 2018
1 parent cac1863 commit 1dca8f4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 74 deletions.
2 changes: 0 additions & 2 deletions Mutators.Tests/TreeMutatorsTest.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;

using GrobExp.Compiler;
using GrobExp.Mutators;
using GrobExp.Mutators.Exceptions;

Expand Down
77 changes: 26 additions & 51 deletions Mutators/ConverterCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Action<TSource, TDest> GetMerger(MutatorsContext context)

public MutatorsTreeBase<TSource> Migrate(MutatorsTreeBase<TDest> mutatorsTree, MutatorsContext context)
{
return mutatorsTree == null ? null : mutatorsTree.Migrate<TSource>(GetOrCreateHashtableSlot(context).ConverterTree);
return mutatorsTree?.Migrate<TSource>(GetOrCreateHashtableSlot(context).ConverterTree);
}

public MutatorsTreeBase<TSource> GetValidationsTree(MutatorsContext context, int priority)
Expand All @@ -71,7 +71,7 @@ public MutatorsTreeBase<TSource> GetValidationsTree(MutatorsContext context, int

public MutatorsTreeBase<TDest> MigratePaths(MutatorsTreeBase<TDest> mutatorsTree, MutatorsContext context)
{
return mutatorsTree == null ? null : mutatorsTree.MigratePaths<TSource>(GetOrCreateHashtableSlot(context).ConverterTree);
return mutatorsTree?.MigratePaths<TSource>(GetOrCreateHashtableSlot(context).ConverterTree);
}

protected abstract void Configure(MutatorsContext context, ConverterConfigurator<TSource, TDest> configurator);
Expand Down Expand Up @@ -105,33 +105,37 @@ private HashtableSlot GetOrCreateHashtableSlot(MutatorsContext context)
tree.ExtractValidationsFromConverters(validationsTree);

var treeConverter = (Expression<Action<TDest, TSource>>)tree.BuildTreeMutator(typeof(TSource));
var sw = Stopwatch.StartNew();
var compiledTreeConverter = LambdaCompiler.Compile(treeConverter, CompilerOptions.All);
var compilationTime = sw.ElapsedMilliseconds;

var typeName = GetType().Name;
var properties = GetLogProperties(context, typeName, compilationTime);

logger.Log(new LogEvent(LogLevel.Info, DateTimeOffset.Now, $"{typeName} was compiled in {compilationTime} ms", properties));
Action<TDest, TSource> compiledTreeConverter;
var sw = Stopwatch.StartNew();
try
{
compiledTreeConverter = LambdaCompiler.Compile(treeConverter, CompilerOptions.All);
}
finally
{
sw.Stop();
LogConverterCompilation(context, sw);
}

slot = new HashtableSlot
{
ConverterTree = tree,
ValidationsTree = validationsTree,
Converter = (source =>
Converter = source =>
{
var dest = new TDest();
BeforeConvert(source);
compiledTreeConverter(dest, source);
AfterConvert(dest, source);
return dest;
}),
Merger = ((source, dest) =>
},
Merger = (source, dest) =>
{
BeforeConvert(source);
compiledTreeConverter(dest, source);
AfterConvert(dest, source);
}),
},
ValidationMutatorsTrees = new Hashtable()
};
//if(!MutatorsAssignRecorder.IsRecording())
Expand All @@ -143,45 +147,17 @@ private HashtableSlot GetOrCreateHashtableSlot(MutatorsContext context)
return slot;
}

private static Dictionary<string, object> GetLogProperties(MutatorsContext context, string typeName, long compilationTime)
private void LogConverterCompilation(MutatorsContext context, Stopwatch sw)
{
var properties = new Dictionary<string, object>
var logProperties = new Dictionary<string, object>
{
{"ConverterCollectionName", typeName},
{"CompilationTime", compilationTime}
};
foreach (var propertyPair in context.GetProperties())
properties.Add($"Context.{propertyPair.Key}", propertyPair.Value);
return properties;
}

private HashtableSlot CreateHashtableSlot(MutatorsContext context)
{
var converterTree = ModelConfigurationNode.CreateRoot(typeof(TDest));
ConfigureInternal(context, new ConverterConfigurator<TSource, TDest>(converterTree));
var validationsTree = ModelConfigurationNode.CreateRoot(typeof(TSource));
converterTree.ExtractValidationsFromConverters(validationsTree);
var treeMutator = (Expression<Action<TDest, TSource>>)converterTree.BuildTreeMutator(typeof(TSource));
var compiledTreeMutator = LambdaCompiler.Compile(treeMutator, CompilerOptions.All);
return new HashtableSlot
{
ConverterTree = converterTree,
ValidationsTree = validationsTree,
Converter = source =>
{
var dest = new TDest();
BeforeConvert(source);
compiledTreeMutator(dest, source);
AfterConvert(dest, source);
return dest;
},
Merger = (source, dest) =>
{
BeforeConvert(source);
compiledTreeMutator(dest, source);
AfterConvert(dest, source);
}
{"ConverterCollectionName", GetType().Name},
{"CompilationTimeMilliseconds", sw.ElapsedMilliseconds}
};
var mutatorsContextTypeName = context.GetType().Name;
foreach (var propertyInfo in context.GetType().GetProperties())
logProperties.Add($"{mutatorsContextTypeName}.{propertyInfo.Name}", propertyInfo.GetValue(context));
logger.Log(new LogEvent(LogLevel.Info, DateTimeOffset.UtcNow, "{ConverterCollectionName} was compiled in {CompilationTimeMilliseconds} ms", logProperties));
}

private static TypeCode GetTypeCode(Type type)
Expand Down Expand Up @@ -484,8 +460,7 @@ private void ConfigureCustomFieldsForArrays(ConverterConfigurator<TSource, TDest
if (node == null)
continue;
var arrays = node.GetArrays();
Expression pathToSourceArray;
if (!arrays.TryGetValue(typeof(TSource), out pathToSourceArray))
if (!arrays.TryGetValue(typeof(TSource), out var pathToSourceArray))
continue;
var pathToDestArrayItem = Expression.Call(MutatorsHelperFunctions.EachMethod.MakeGenericMethod(pathToDestArray.Type.GetItemType()), pathToDestArray);
var pathToSourceArrayItem = Expression.Call(MutatorsHelperFunctions.EachMethod.MakeGenericMethod(pathToSourceArray.Type.GetItemType()), pathToSourceArray);
Expand Down
12 changes: 1 addition & 11 deletions Mutators/MutatorsContext.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
using System.Collections.Generic;
using System.Linq;

namespace GrobExp.Mutators
{
public abstract class MutatorsContext
{
public abstract string GetKey();

public Dictionary<string, object> GetProperties()
{
var properties = GetType().GetProperties();
var propertiesDict = properties.ToDictionary(prop => prop.Name, prop => prop.GetValue(this));
return propertiesDict;
}

public static readonly MutatorsContext Empty = new EmptyMutatorsContext();
}

public sealed class EmptyMutatorsContext : MutatorsContext
{
public override string GetKey()
{
return "";
return string.Empty;
}
}
}
10 changes: 0 additions & 10 deletions Mutators/MutatorsTreeBase.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

using GrobExp.Mutators.Aggregators;
using GrobExp.Mutators.ModelConfiguration;
Expand Down Expand Up @@ -127,13 +125,6 @@ internal MutatorsTreeBase<TData> MigratePaths<T>(string key, ModelConfigurationN

public abstract MutatorsTreeBase<TData> Merge(MutatorsTreeBase<TData> other);

public void SetValidatorsAssembly()
{
if (File.Exists("Validators.dll"))
validatorsAssembly = Assembly.LoadFrom("Validators.dll");
}

//TODO: прикрутить чтение из dll, как и в компиляторах
internal Action<TChild, ValidationResultTreeNode> GetValidatorInternal<TChild>(Expression<Func<TData, TChild>> path)
{
var nodeInfo = GetOrCreateNodeInfo(path);
Expand Down Expand Up @@ -279,7 +270,6 @@ private NodeInfo<T> GetOrCreateNodeInfo<T>(Expression<Func<TData, T>> path)
return nodeInfo;
}

private Assembly validatorsAssembly;
private readonly Hashtable hashtable = new Hashtable();
private readonly object lockObject = new object();

Expand Down

0 comments on commit 1dca8f4

Please sign in to comment.