Skip to content

Commit cc27172

Browse files
authored
Merge pull request #123 from DataObjects-NET/prefetch-for-delayedquery
Dedicated Prefetch extensions for DelayedQuery{T} and QueryResult{T}
2 parents 34bc893 + 832fab5 commit cc27172

File tree

6 files changed

+340
-102
lines changed

6 files changed

+340
-102
lines changed

Orm/Xtensive.Orm.Manual/Prefetch/PrefetchTest.cs

Lines changed: 283 additions & 91 deletions
Large diffs are not rendered by default.

Orm/Xtensive.Orm/Core/Extensions/EnumerableExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2008-2020 Xtensive LLC.
1+
// Copyright (C) 2008-2021 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44
// Created by: Alex Yakunin
@@ -14,7 +14,7 @@
1414
using System.Threading.Tasks;
1515
using Xtensive.Collections;
1616
using Xtensive.Collections.Graphs;
17-
using Xtensive.Orm.Internals;
17+
using Xtensive.Orm;
1818

1919

2020
namespace Xtensive.Core

Orm/Xtensive.Orm/Orm/DelayedQuery{T}.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2009-2020 Xtensive LLC.
1+
// Copyright (C) 2009-2021 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44
// Created by: Alexander Nikolaev
@@ -10,9 +10,10 @@
1010
using System.Threading;
1111
using System.Threading.Tasks;
1212
using Xtensive.Core;
13+
using Xtensive.Orm.Internals;
1314
using Xtensive.Orm.Linq;
1415

15-
namespace Xtensive.Orm.Internals
16+
namespace Xtensive.Orm
1617
{
1718
/// <summary>
1819
/// Represents a delayed sequence query (query where result can be enumerated after an execution).

Orm/Xtensive.Orm/Orm/Linq/Materialization/MaterializingReader.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2020 Xtensive LLC.
1+
// Copyright (C) 2020-2021 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44

@@ -11,20 +11,23 @@
1111

1212
namespace Xtensive.Orm.Linq.Materialization
1313
{
14-
public interface IMaterializingReader<out TItem>
14+
internal interface IMaterializingReader<out TItem>
1515
{
16+
Session Session { get; }
1617
IEnumerator<TItem> AsEnumerator();
1718
IAsyncEnumerator<TItem> AsAsyncEnumerator();
1819
}
1920

20-
public class MaterializingReader<TItem>: IMaterializingReader<TItem>, IEnumerator<TItem>, IAsyncEnumerator<TItem>
21+
internal class MaterializingReader<TItem>: IMaterializingReader<TItem>, IEnumerator<TItem>, IAsyncEnumerator<TItem>
2122
{
2223
private readonly RecordSetReader recordSetReader;
2324
private readonly MaterializationContext context;
2425
private readonly ParameterContext parameterContext;
2526
private readonly IItemMaterializer<TItem> itemMaterializer;
2627
private readonly Queue<Action> materializationQueue;
2728

29+
public Session Session => context.Session;
30+
2831
public IEnumerator<TItem> AsEnumerator()
2932
{
3033
recordSetReader.Reset();

Orm/Xtensive.Orm/Orm/PrefetchExtensions.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2009-2020 Xtensive LLC.
1+
// Copyright (C) 2009-2021 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44
// Created by: Alexander Nikolaev
@@ -37,6 +37,42 @@ public static PrefetchQuery<TElement> Prefetch<TElement, TFieldValue>(
3737
return Prefetch(source, Session.Demand(), expression);
3838
}
3939

40+
/// <summary>
41+
/// Registers fields specified by <paramref name="expression"/> for prefetch.
42+
/// </summary>
43+
/// <typeparam name="TElement">The type of the element of the source sequence.</typeparam>
44+
/// <typeparam name="TFieldValue">The type of the field's value to be prefetched.</typeparam>
45+
/// <param name="source">The source query.</param>
46+
/// <param name="expression">The expression specifying a field to be prefetched.</param>
47+
/// <returns>An <see cref="IEnumerable{TElement}"/> of source items.</returns>
48+
public static PrefetchQuery<TElement> Prefetch<TElement, TFieldValue>(
49+
this DelayedQuery<TElement> source,
50+
Expression<Func<TElement, TFieldValue>> expression)
51+
{
52+
return new PrefetchQuery<TElement>(source.Session, source).RegisterPath(expression);
53+
}
54+
55+
/// <summary>
56+
/// Registers fields specified by <paramref name="expression"/> for prefetch.
57+
/// </summary>
58+
/// <typeparam name="TElement">The type of the element of the source sequence.</typeparam>
59+
/// <typeparam name="TFieldValue">The type of the field's value to be prefetched.</typeparam>
60+
/// <param name="source">The source query.</param>
61+
/// <param name="expression">The expression specifying a field to be prefetched.</param>
62+
/// <returns>An <see cref="IEnumerable{TElement}"/> of source items.</returns>
63+
public static PrefetchQuery<TElement> Prefetch<TElement, TFieldValue>(
64+
this QueryResult<TElement> source,
65+
Expression<Func<TElement, TFieldValue>> expression)
66+
{
67+
var session = source.Session;
68+
if (session != null) {
69+
return new PrefetchQuery<TElement>(session, source).RegisterPath(expression);
70+
}
71+
return new PrefetchQuery<TElement>(Session.Demand(), source).RegisterPath(expression);
72+
}
73+
74+
75+
4076
/// <summary>
4177
/// Registers fields specified by <paramref name="expression"/> for prefetch.
4278
/// </summary>

Orm/Xtensive.Orm/Orm/QueryResult.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
// Copyright (C) 2020 Xtensive LLC.
1+
// Copyright (C) 2020-2021 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44

55
using System;
66
using System.Collections;
77
using System.Collections.Generic;
8+
using JetBrains.Annotations;
89
using Xtensive.Orm.Linq.Materialization;
910

1011
namespace Xtensive.Orm
@@ -19,6 +20,8 @@ private class EnumerableReader : IMaterializingReader<TItem>
1920
{
2021
private readonly IEnumerable<TItem> items;
2122

23+
public Session Session => null;
24+
2225
public IEnumerator<TItem> AsEnumerator() => items.GetEnumerator();
2326

2427
public IAsyncEnumerator<TItem> AsAsyncEnumerator() => throw new System.NotSupportedException();
@@ -29,8 +32,11 @@ public EnumerableReader(IEnumerable<TItem> items)
2932
}
3033
}
3134

32-
private readonly IMaterializingReader<TItem> reader;
3335
private readonly StateLifetimeToken lifetimeToken;
36+
private readonly IMaterializingReader<TItem> reader;
37+
38+
[CanBeNull]
39+
internal Session Session => reader.Session;
3440

3541
/// <inheritdoc/>
3642
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
@@ -70,7 +76,7 @@ internal QueryResult(IMaterializingReader<TItem> reader, StateLifetimeToken life
7076
internal QueryResult(IEnumerable<TItem> items)
7177
{
7278
reader = new EnumerableReader(items);
73-
this.lifetimeToken = default;
79+
lifetimeToken = default;
7480
}
7581
}
7682
}

0 commit comments

Comments
 (0)