Skip to content

Commit e509920

Browse files
committed
async QueryBatchItemBase.ProcessResults
1 parent 3f1f025 commit e509920

File tree

6 files changed

+64
-5
lines changed

6 files changed

+64
-5
lines changed

src/NHibernate/Async/Multi/IQueryBatchItem.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@ public partial interface IQueryBatchItem
3636
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
3737
Task ExecuteNonBatchedAsync(CancellationToken cancellationToken);
3838
}
39+
40+
internal partial interface IQueryBatchItemWithAsyncProcessResults
41+
{
42+
Task ProcessResultsAsync(CancellationToken cancellationToken);
43+
}
3944
}

src/NHibernate/Async/Multi/QueryBatch.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,11 @@ protected async Task ExecuteBatchedAsync(CancellationToken cancellationToken)
156156

157157
foreach (var query in _queries)
158158
{
159-
query.ProcessResults();
159+
//TODO 6.0: Replace with query.ProcessResults();
160+
if (query is IQueryBatchItemWithAsyncProcessResults q)
161+
await (q.ProcessResultsAsync(cancellationToken)).ConfigureAwait(false);
162+
else
163+
query.ProcessResults();
160164
}
161165
}
162166
catch (OperationCanceledException) { throw; }

src/NHibernate/Async/Multi/QueryBatchItemBase.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace NHibernate.Multi
2323
{
2424
using System.Threading.Tasks;
2525
using System.Threading;
26-
public abstract partial class QueryBatchItemBase<TResult> : IQueryBatchItem<TResult>
26+
public abstract partial class QueryBatchItemBase<TResult> : IQueryBatchItem<TResult>, IQueryBatchItemWithAsyncProcessResults
2727
{
2828

2929
/// <inheritdoc />
@@ -115,6 +115,32 @@ public async Task<int> ProcessResultsSetAsync(DbDataReader reader, CancellationT
115115
}
116116
}
117117

118+
/// <inheritdoc cref="IQueryBatchItem.ProcessResults" />
119+
public async Task ProcessResultsAsync(CancellationToken cancellationToken)
120+
{
121+
cancellationToken.ThrowIfCancellationRequested();
122+
ThrowIfNotInitialized();
123+
124+
await (InitializeEntitiesAndCollectionsAsync(_reader, _hydratedObjects, cancellationToken)).ConfigureAwait(false);
125+
for (var i = 0; i < _queryInfos.Count; i++)
126+
{
127+
var queryInfo = _queryInfos[i];
128+
if (_subselectResultKeys[i] != null)
129+
{
130+
queryInfo.Loader.CreateSubselects(_subselectResultKeys[i], queryInfo.Parameters, Session);
131+
}
132+
133+
if (queryInfo.IsCacheable)
134+
{
135+
// This transformation must not be applied to ResultToCache.
136+
queryInfo.Result =
137+
queryInfo.Loader.TransformCacheableResults(
138+
queryInfo.Parameters, queryInfo.CacheKey.ResultTransformer, queryInfo.Result);
139+
}
140+
}
141+
AfterLoadCallback?.Invoke(GetResults());
142+
}
143+
118144
/// <inheritdoc />
119145
public async Task ExecuteNonBatchedAsync(CancellationToken cancellationToken)
120146
{
@@ -124,5 +150,19 @@ public async Task ExecuteNonBatchedAsync(CancellationToken cancellationToken)
124150
}
125151

126152
protected abstract Task<IList<TResult>> GetResultsNonBatchedAsync(CancellationToken cancellationToken);
153+
154+
private async Task InitializeEntitiesAndCollectionsAsync(DbDataReader reader, List<object>[] hydratedObjects, CancellationToken cancellationToken)
155+
{
156+
cancellationToken.ThrowIfCancellationRequested();
157+
for (var i = 0; i < _queryInfos.Count; i++)
158+
{
159+
var queryInfo = _queryInfos[i];
160+
if (queryInfo.IsResultFromCache)
161+
continue;
162+
await (queryInfo.Loader.InitializeEntitiesAndCollectionsAsync(
163+
hydratedObjects[i], reader, Session, queryInfo.Parameters.IsReadOnly(Session),
164+
queryInfo.CacheBatcher, cancellationToken)).ConfigureAwait(false);
165+
}
166+
}
127167
}
128168
}

src/NHibernate/Multi/IQueryBatchItem.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,10 @@ public partial interface IQueryBatchItem
8080
/// </summary>
8181
void ExecuteNonBatched();
8282
}
83+
84+
//TODO 6.0: Remove along with ignore rule for IQueryBatchItem.ProcessResults in AsyncGenerator.yml
85+
internal partial interface IQueryBatchItemWithAsyncProcessResults
86+
{
87+
void ProcessResults();
88+
}
8389
}

src/NHibernate/Multi/QueryBatch.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,11 @@ protected void ExecuteBatched()
182182

183183
foreach (var query in _queries)
184184
{
185-
query.ProcessResults();
185+
//TODO 6.0: Replace with query.ProcessResults();
186+
if (query is IQueryBatchItemWithAsyncProcessResults q)
187+
q.ProcessResults();
188+
else
189+
query.ProcessResults();
186190
}
187191
}
188192
catch (Exception sqle)

src/NHibernate/Multi/QueryBatchItemBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace NHibernate.Multi
1414
/// <summary>
1515
/// Base class for both ICriteria and IQuery queries
1616
/// </summary>
17-
public abstract partial class QueryBatchItemBase<TResult> : IQueryBatchItem<TResult>
17+
public abstract partial class QueryBatchItemBase<TResult> : IQueryBatchItem<TResult>, IQueryBatchItemWithAsyncProcessResults
1818
{
1919
protected ISessionImplementor Session;
2020
private List<EntityKey[]>[] _subselectResultKeys;
@@ -256,7 +256,7 @@ public int ProcessResultsSet(DbDataReader reader)
256256
}
257257
}
258258

259-
/// <inheritdoc />
259+
/// <inheritdoc cref="IQueryBatchItem.ProcessResults" />
260260
public void ProcessResults()
261261
{
262262
ThrowIfNotInitialized();

0 commit comments

Comments
 (0)