Skip to content

Commit f17a147

Browse files
committed
async QueryBatchItemBase.ProcessResults
1 parent 5f0c449 commit f17a147

File tree

6 files changed

+70
-5
lines changed

6 files changed

+70
-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
@@ -168,7 +168,11 @@ protected async Task ExecuteBatchedAsync(CancellationToken cancellationToken)
168168

169169
foreach (var query in _queries)
170170
{
171-
query.ProcessResults();
171+
//TODO 6.0: Replace with query.ProcessResults();
172+
if (query is IQueryBatchItemWithAsyncProcessResults q)
173+
await (q.ProcessResultsAsync(cancellationToken)).ConfigureAwait(false);
174+
else
175+
query.ProcessResults();
172176
}
173177
}
174178
catch (OperationCanceledException) { throw; }

src/NHibernate/Async/Multi/QueryBatchItemBase.cs

Lines changed: 47 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 />
@@ -125,6 +125,38 @@ public async Task<int> ProcessResultsSetAsync(DbDataReader reader, CancellationT
125125
}
126126
}
127127

128+
/// <inheritdoc cref="IQueryBatchItem.ProcessResults" />
129+
public async Task ProcessResultsAsync(CancellationToken cancellationToken)
130+
{
131+
cancellationToken.ThrowIfCancellationRequested();
132+
ThrowIfNotInitialized();
133+
134+
await (InitializeEntitiesAndCollectionsAsync(_reader, _hydratedObjects, cancellationToken)).ConfigureAwait(false);
135+
for (var i = 0; i < _queryInfos.Count; i++)
136+
{
137+
var queryInfo = _queryInfos[i];
138+
if (_subselectResultKeys[i] != null)
139+
{
140+
queryInfo.Loader.CreateSubselects(_subselectResultKeys[i], queryInfo.Parameters, Session);
141+
}
142+
143+
if (queryInfo.IsCacheable)
144+
{
145+
if (queryInfo.IsResultFromCache)
146+
{
147+
var queryCacheBuilder = new QueryCacheResultBuilder(queryInfo.Loader);
148+
queryInfo.Result = queryCacheBuilder.GetResultList(queryInfo.Result);
149+
}
150+
151+
// This transformation must not be applied to ResultToCache.
152+
queryInfo.Result =
153+
queryInfo.Loader.TransformCacheableResults(
154+
queryInfo.Parameters, queryInfo.CacheKey.ResultTransformer, queryInfo.Result);
155+
}
156+
}
157+
AfterLoadCallback?.Invoke(GetResults());
158+
}
159+
128160
/// <inheritdoc />
129161
public async Task ExecuteNonBatchedAsync(CancellationToken cancellationToken)
130162
{
@@ -134,5 +166,19 @@ public async Task ExecuteNonBatchedAsync(CancellationToken cancellationToken)
134166
}
135167

136168
protected abstract Task<IList<TResult>> GetResultsNonBatchedAsync(CancellationToken cancellationToken);
169+
170+
private async Task InitializeEntitiesAndCollectionsAsync(DbDataReader reader, List<object>[] hydratedObjects, CancellationToken cancellationToken)
171+
{
172+
cancellationToken.ThrowIfCancellationRequested();
173+
for (var i = 0; i < _queryInfos.Count; i++)
174+
{
175+
var queryInfo = _queryInfos[i];
176+
if (queryInfo.IsResultFromCache)
177+
continue;
178+
await (queryInfo.Loader.InitializeEntitiesAndCollectionsAsync(
179+
hydratedObjects[i], reader, Session, queryInfo.Parameters.IsReadOnly(Session),
180+
queryInfo.CacheBatcher, cancellationToken)).ConfigureAwait(false);
181+
}
182+
}
137183
}
138184
}

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
@@ -180,7 +180,11 @@ protected void ExecuteBatched()
180180

181181
foreach (var query in _queries)
182182
{
183-
query.ProcessResults();
183+
//TODO 6.0: Replace with query.ProcessResults();
184+
if (query is IQueryBatchItemWithAsyncProcessResults q)
185+
q.ProcessResults();
186+
else
187+
query.ProcessResults();
184188
}
185189
}
186190
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;
@@ -269,7 +269,7 @@ public int ProcessResultsSet(DbDataReader reader)
269269
}
270270
}
271271

272-
/// <inheritdoc />
272+
/// <inheritdoc cref="IQueryBatchItem.ProcessResults" />
273273
public void ProcessResults()
274274
{
275275
ThrowIfNotInitialized();

0 commit comments

Comments
 (0)