Skip to content
Merged
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
6 changes: 0 additions & 6 deletions Orm/Xtensive.Orm/Orm/Domain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
// Created by: Dmitri Maximov
// Created: 2007.08.03

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Xtensive.Caching;
using Xtensive.Collections;
Expand Down Expand Up @@ -132,8 +128,6 @@ public static Domain Demand()

internal FastConcurrentLruCache<QueryKey, (QueryKey Key, ParameterizedQuery Query)> QueryCache { get; }

internal ConcurrentDictionary<Type, System.Linq.Expressions.MethodCallExpression> RootCallExpressionsCache { get; } = new();

/// <summary>
/// Caches uncompiled queries used by <see cref="PrefetchManager"/> to fetch certain entities.
/// </summary>
Expand Down
19 changes: 8 additions & 11 deletions Orm/Xtensive.Orm/Orm/Linq/QueryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
// Created by: Denis Krjuchkov
// Created: 2009.04.02

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Collections.Concurrent;
using System.Linq.Expressions;
using System.Reflection;
using Xtensive.Core;
Expand Down Expand Up @@ -66,10 +63,10 @@ public static Expression<Func<Tuple, bool>> BuildFilterLambda(int startIndex, IR
return FastExpression.Lambda<Func<Tuple, bool>>(filterExpression, TupleParameters);
}

private static Expression CreateEntityQuery(Type elementType, Domain domain)
{
return domain.RootCallExpressionsCache.GetOrAdd(elementType, (t) => Expression.Call(null, WellKnownMembers.Query.All.MakeGenericMethod(elementType)));
}
private static readonly ConcurrentDictionary<Type, MethodCallExpression> RootCallExpressionsCache = new();

internal static Expression CreateEntityQuery(Type elementType) =>
RootCallExpressionsCache.GetOrAdd(elementType, static t => Expression.Call(null, WellKnownMembers.Query.All.MakeGenericMethod(t)));

public static bool IsDirectEntitySetQuery(Expression entitySet)
{
Expand Down Expand Up @@ -134,7 +131,7 @@ public static Expression CreateEntitySetQuery(Expression ownerEntity, FieldInfo
);
return Expression.Call(
WellKnownMembers.Queryable.Where.CachedMakeGenericMethod(elementType),
CreateEntityQuery(elementType, domain),
CreateEntityQuery(elementType),
FastExpression.Lambda(whereExpression, whereParameter)
);
}
Expand All @@ -159,7 +156,7 @@ public static Expression CreateEntitySetQuery(Expression ownerEntity, FieldInfo

var outerQuery = Expression.Call(
WellKnownMembers.Queryable.Where.CachedMakeGenericMethod(connectorType),
CreateEntityQuery(connectorType, domain),
CreateEntityQuery(connectorType),
FastExpression.Lambda(filterExpression, filterParameter)
);

Expand All @@ -171,7 +168,7 @@ public static Expression CreateEntitySetQuery(Expression ownerEntity, FieldInfo
var innerSelector = FastExpression.Lambda(innerSelectorParameter, innerSelectorParameter);
var resultSelector = FastExpression.Lambda(innerSelectorParameter, outerSelectorParameter, innerSelectorParameter);

var innerQuery = CreateEntityQuery(elementType, domain);
var innerQuery = CreateEntityQuery(elementType);
var joinMethodInfo = WellKnownMembers.Queryable.Join
.MakeGenericMethod(new[] {
connectorType,
Expand Down
40 changes: 16 additions & 24 deletions Orm/Xtensive.Orm/Orm/QueryEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@
// 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;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Xtensive.Core;
using Xtensive.Orm.FullTextSearchCondition.Interfaces;
using Xtensive.Orm.FullTextSearchCondition.Nodes;
using Xtensive.Orm.Internals;
using Xtensive.Orm.Internals.Prefetch;
using Xtensive.Orm.Linq;
using Xtensive.Reflection;
using Tuple = Xtensive.Tuples.Tuple;
Expand Down Expand Up @@ -51,7 +45,13 @@ public bool Equals(QueryEndpoint other) =>

public override int GetHashCode() => HashCode.Combine(Provider, RootBuilder);

/// <summary>
private static class Traits<T>
{
public static readonly MethodCallExpression RootCallExpression =
Expression.Call(null, WellKnownMembers.Query.All.MakeGenericMethod(typeof(T)));
}

/// <summary>
/// The "starting point" for any LINQ query -
/// a <see cref="IQueryable{T}"/> enumerating all the instances
/// of type <typeparamref name="T"/>.
Expand All @@ -61,11 +61,10 @@ public bool Equals(QueryEndpoint other) =>
/// An <see cref="IQueryable{T}"/> enumerating all the instances
/// of type <typeparamref name="T"/>.
/// </returns>
public IQueryable<T> All<T>()
where T : class, IEntity
{
return Provider.CreateQuery<T>(BuildRootExpression(typeof(T)));
}
public IQueryable<T> All<T>() where T : class, IEntity =>
Provider.CreateQuery<T>(RootBuilder != null
? RootBuilder.BuildRootExpression(typeof(T))
: Traits<T>.RootCallExpression);

/// <summary>
/// The "starting point" for dynamic LINQ query -
Expand All @@ -77,10 +76,11 @@ public IQueryable<T> All<T>()
/// An <see cref="IQueryable"/> enumerating all the instances
/// of type <paramref name="elementType"/>.
/// </returns>
public IQueryable All(Type elementType)
{
return ((IQueryProvider) Provider).CreateQuery(BuildRootExpression(elementType));
}
public IQueryable All(Type elementType) =>
((IQueryProvider) Provider).CreateQuery(RootBuilder != null
? RootBuilder.BuildRootExpression(elementType)
: QueryHelper.CreateEntityQuery(elementType)
);

#region Full-text related

Expand Down Expand Up @@ -970,13 +970,6 @@ private Key GetKeyByValues<T>(ReadOnlySpan<object> keyValues)
return Key.Create(session.Domain, session.StorageNodeId, session.Domain.Model.Types[typeof(T)], TypeReferenceAccuracy.BaseType, keyValues);
}

private Expression BuildRootExpression(Type elementType)
{
return RootBuilder!=null
? RootBuilder.BuildRootExpression(elementType)
: Session.Domain.RootCallExpressionsCache.GetOrAdd(elementType, (t) => Expression.Call(null, WellKnownMembers.Query.All.MakeGenericMethod(t)));
}

private static void ThrowKeyNotFoundException(Key key) =>
throw new KeyNotFoundException(String.Format(Strings.EntityWithKeyXDoesNotExist, key));

Expand All @@ -987,7 +980,6 @@ private static void ThrowKeyNotFoundException(Key key) =>

internal QueryEndpoint(QueryProvider provider)
{
ArgumentNullException.ThrowIfNull(provider);
Provider = provider;
}

Expand Down