Skip to content

Fix Issue 224 (Caching queries with object captures) #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions Orm/Xtensive.Orm/Orm/Internals/CompiledQueryRunner.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2012-2021 Xtensive LLC.
// Copyright (C) 2012-2022 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.
// Created by: Denis Krjuchkov
Expand All @@ -7,6 +7,7 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Xtensive.Caching;
Expand All @@ -19,6 +20,8 @@ namespace Xtensive.Orm.Internals
{
internal class CompiledQueryRunner
{
private static readonly Func<FieldInfo, bool> FieldIsSimple = fieldInfo => TypeIsSimple(fieldInfo.FieldType);

private readonly Domain domain;
private readonly Session session;
private readonly QueryEndpoint endpoint;
Expand Down Expand Up @@ -108,7 +111,7 @@ private DelayedQuery<TElement> CreateDelayedSequenceQuery<TElement>(
private ParameterizedQuery GetScalarQuery<TResult>(
Func<QueryEndpoint, TResult> query, bool executeAsSideEffect, out TResult result)
{
AllocateParameterAndReplacer();
var cacheable = AllocateParameterAndReplacer();

var parameterContext = new ParameterContext(outerContext);
parameterContext.SetValue(queryParameter, queryTarget);
Expand All @@ -123,7 +126,9 @@ private ParameterizedQuery GetScalarQuery<TResult>(
throw new NotSupportedException(Strings.ExNonLinqCallsAreNotSupportedWithinQueryExecuteDelayed);
}

PutCachedQuery(parameterizedQuery);
if (cacheable) {
PutCachedQuery(parameterizedQuery);
}
return parameterizedQuery;
}

Expand All @@ -135,24 +140,26 @@ private ParameterizedQuery GetSequenceQuery<TElement>(
return parameterizedQuery;
}

AllocateParameterAndReplacer();
var cacheable = AllocateParameterAndReplacer();
var scope = new CompiledQueryProcessingScope(queryParameter, queryParameterReplacer);
using (scope.Enter()) {
var result = query.Invoke(endpoint);
var translatedQuery = endpoint.Provider.Translate(result.Expression);
parameterizedQuery = (ParameterizedQuery) translatedQuery;
}

PutCachedQuery(parameterizedQuery);
if (cacheable) {
PutCachedQuery(parameterizedQuery);
}
return parameterizedQuery;
}

private void AllocateParameterAndReplacer()
private bool AllocateParameterAndReplacer()
{
if (queryTarget == null) {
queryParameter = null;
queryParameterReplacer = new ExtendedExpressionReplacer(e => e);
return;
return true;
}

var closureType = queryTarget.GetType();
Expand Down Expand Up @@ -192,6 +199,25 @@ private void AllocateParameterAndReplacer()
}
return null;
});

return !closureType.Name.Contains("<>c__DisplayClass") // 'DisplayClass' is generated class for captured objects
|| closureType.GetFields().All(FieldIsSimple);
}

private static bool TypeIsSimple(Type type)
{
var typeInfo = type.GetTypeInfo();
if (typeInfo.IsGenericType) {
var genericDef = typeInfo.GetGenericTypeDefinition();
return (genericDef == WellKnownTypes.NullableOfT || genericDef.IsAssignableTo(WellKnownTypes.IReadOnlyList))
&& TypeIsSimple(typeInfo.GetGenericArguments()[0]);
}
else if (typeInfo.IsArray) {
return TypeIsSimple(typeInfo.GetElementType());
}
else {
return typeInfo.IsPrimitive || typeInfo.IsEnum || type == WellKnownTypes.String || type == WellKnownTypes.Decimal;
}
}

private ParameterizedQuery GetCachedQuery() =>
Expand Down
4 changes: 3 additions & 1 deletion Orm/Xtensive.Orm/Reflection/WellKnownTypes.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright (C) 2020 Xtensive LLC.
// Copyright (C) 2020-2022 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
Expand Down Expand Up @@ -73,6 +74,7 @@ internal static class WellKnownTypes

public static readonly Type ByteArray = typeof(byte[]);
public static readonly Type ObjectArray = typeof(object[]);
public static readonly Type IReadOnlyList = typeof(IReadOnlyList<>);

public static readonly Type DefaultMemberAttribute = typeof(DefaultMemberAttribute);
}
Expand Down