Skip to content

Commit 68976d9

Browse files
committed
Implement AutoFlush for new batcher and fix for multi criteria. Removed auto flush from GetTranslators
1 parent 07d99c1 commit 68976d9

26 files changed

+143
-135
lines changed

src/NHibernate.Test/Async/Futures/LinqFutureFixture.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,5 +504,26 @@ public async Task FutureCombineCachedAndNonCachedQueriesAsync()
504504
await (tx.CommitAsync());
505505
}
506506
}
507+
508+
[Test]
509+
public async Task FutureAutoFlushAsync()
510+
{
511+
using (var s = OpenSession())
512+
using (s.BeginTransaction())
513+
{
514+
s.FlushMode = FlushMode.Auto;
515+
var p1 = new Person
516+
{
517+
Name = "Person name",
518+
Age = 15
519+
};
520+
await (s.SaveAsync(p1));
521+
await (s.FlushAsync());
522+
523+
await (s.DeleteAsync(p1));
524+
var count = await (s.QueryOver<Person>().ToRowCountQuery().FutureValue<int>().GetValueAsync());
525+
Assert.That(count, Is.EqualTo(0), "Session wasn't auto flushed.");
526+
}
527+
}
507528
}
508529
}

src/NHibernate.Test/Futures/LinqFutureFixture.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,5 +493,26 @@ public void FutureCombineCachedAndNonCachedQueries()
493493
tx.Commit();
494494
}
495495
}
496+
497+
[Test]
498+
public void FutureAutoFlush()
499+
{
500+
using (var s = OpenSession())
501+
using (s.BeginTransaction())
502+
{
503+
s.FlushMode = FlushMode.Auto;
504+
var p1 = new Person
505+
{
506+
Name = "Person name",
507+
Age = 15
508+
};
509+
s.Save(p1);
510+
s.Flush();
511+
512+
s.Delete(p1);
513+
var count = s.QueryOver<Person>().ToRowCountQuery().FutureValue<int>().Value;
514+
Assert.That(count, Is.EqualTo(0), "Session wasn't auto flushed.");
515+
}
516+
}
496517
}
497518
}

src/NHibernate/Async/Engine/ISessionImplementor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ public partial interface ISessionImplementor
164164

165165
Task<IList<T>> ListCustomQueryAsync<T>(ICustomQuery customQuery, QueryParameters queryParameters, CancellationToken cancellationToken);
166166

167+
// Since v5.2
168+
[Obsolete("This method has no usages and will be removed in a future version")]
167169
Task<IQueryTranslator[]> GetQueriesAsync(IQueryExpression query, bool scalar, CancellationToken cancellationToken); // NH specific for MultiQuery
168170

169171
/// <summary>

src/NHibernate/Async/Impl/AbstractQueryImpl.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,5 @@ public abstract partial class AbstractQueryImpl : IQuery
5656
}
5757

5858
#endregion
59-
60-
protected internal abstract Task<IEnumerable<ITranslator>> GetTranslatorsAsync(ISessionImplementor sessionImplementor, QueryParameters queryParameters, CancellationToken cancellationToken);
6159
}
6260
}

src/NHibernate/Async/Impl/AbstractQueryImpl2.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,5 @@ public abstract partial class AbstractQueryImpl2 : AbstractQueryImpl
116116
After();
117117
}
118118
}
119-
120-
protected internal override async Task<IEnumerable<ITranslator>> GetTranslatorsAsync(ISessionImplementor sessionImplementor, QueryParameters queryParameters, CancellationToken cancellationToken)
121-
{
122-
cancellationToken.ThrowIfCancellationRequested();
123-
// NOTE: updates queryParameters.NamedParameters as (desired) side effect
124-
var queryExpression = ExpandParameters(queryParameters.NamedParameters);
125-
126-
return (await (sessionImplementor.GetQueriesAsync(queryExpression, false, cancellationToken)).ConfigureAwait(false))
127-
.Select(queryTranslator => new HqlTranslatorWrapper(queryTranslator));
128-
}
129119
}
130-
}
120+
}

src/NHibernate/Async/Impl/AbstractSessionImpl.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ public virtual async Task<IList<T>> ListCustomQueryAsync<T>(ICustomQuery customQ
160160
}
161161
}
162162

163+
// Since v5.2
164+
[Obsolete("This method has no usages and will be removed in a future version")]
163165
public abstract Task<IQueryTranslator[]> GetQueriesAsync(IQueryExpression query, bool scalar, CancellationToken cancellationToken);
164166
public abstract Task<object> GetEntityUsingInterceptorAsync(EntityKey key, CancellationToken cancellationToken);
165167
public abstract Task<int> ExecuteNativeUpdateAsync(NativeSQLQuerySpecification specification, QueryParameters queryParameters, CancellationToken cancellationToken);

src/NHibernate/Async/Impl/MultiCriteriaImpl.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Collections;
1313
using System.Collections.Generic;
1414
using System.Diagnostics;
15+
using System.Linq;
1516
using NHibernate.Cache;
1617
using NHibernate.Criterion;
1718
using NHibernate.Driver;
@@ -49,9 +50,14 @@ public partial class MultiCriteriaImpl : IMultiCriteria
4950
}
5051
}
5152

53+
var querySpaces = new HashSet<string>(loaders.SelectMany(l => l.QuerySpaces));
54+
if (resultSetsCommand.HasQueries)
55+
{
56+
await (session.AutoFlushIfRequiredAsync(querySpaces, cancellationToken)).ConfigureAwait(false);
57+
}
5258
if (cacheable)
5359
{
54-
criteriaResults = await (ListUsingQueryCacheAsync(cancellationToken)).ConfigureAwait(false);
60+
criteriaResults = await (ListUsingQueryCacheAsync(querySpaces, cancellationToken)).ConfigureAwait(false);
5561
}
5662
else
5763
{
@@ -62,20 +68,18 @@ public partial class MultiCriteriaImpl : IMultiCriteria
6268
}
6369
}
6470

65-
private async Task<IList> ListUsingQueryCacheAsync(CancellationToken cancellationToken)
71+
private async Task<IList> ListUsingQueryCacheAsync(HashSet<string> querySpaces, CancellationToken cancellationToken)
6672
{
6773
cancellationToken.ThrowIfCancellationRequested();
6874
IQueryCache queryCache = session.Factory.GetQueryCache(cacheRegion);
6975

7076
ISet<FilterKey> filterKeys = FilterKey.CreateFilterKeys(session.EnabledFilters);
7177

72-
ISet<string> querySpaces = new HashSet<string>();
7378
List<IType[]> resultTypesList = new List<IType[]>();
7479
int[] maxRows = new int[loaders.Count];
7580
int[] firstRows = new int[loaders.Count];
7681
for (int i = 0; i < loaders.Count; i++)
7782
{
78-
querySpaces.UnionWith(loaders[i].QuerySpaces);
7983
resultTypesList.Add(loaders[i].ResultTypes);
8084
firstRows[i] = parameters[i].RowSelection.FirstRow;
8185
maxRows[i] = parameters[i].RowSelection.MaxRows;

src/NHibernate/Async/Impl/MultiQueryImpl.cs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Collections;
1313
using System.Collections.Generic;
1414
using System.Diagnostics;
15+
using System.Linq;
1516
using NHibernate.Cache;
1617
using NHibernate.Driver;
1718
using NHibernate.Engine;
@@ -56,7 +57,14 @@ public partial class MultiQueryImpl : IMultiQuery
5657
try
5758
{
5859
Before();
59-
return cacheable ? await (ListUsingQueryCacheAsync(cancellationToken)).ConfigureAwait(false) : await (ListIgnoreQueryCacheAsync(cancellationToken)).ConfigureAwait(false);
60+
61+
var querySpaces = new HashSet<string>(Translators.SelectMany(t => t.QuerySpaces));
62+
if (resultSetsCommand.HasQueries)
63+
{
64+
await (session.AutoFlushIfRequiredAsync(querySpaces, cancellationToken)).ConfigureAwait(false);
65+
}
66+
67+
return cacheable ? await (ListUsingQueryCacheAsync(querySpaces, cancellationToken)).ConfigureAwait(false) : await (ListIgnoreQueryCacheAsync(cancellationToken)).ConfigureAwait(false);
6068
}
6169
finally
6270
{
@@ -188,27 +196,6 @@ protected async Task<List<object>> DoListAsync(CancellationToken cancellationTok
188196
return results;
189197
}
190198

191-
private async Task AggregateQueriesInformationAsync(CancellationToken cancellationToken)
192-
{
193-
cancellationToken.ThrowIfCancellationRequested();
194-
int queryIndex = 0;
195-
foreach (AbstractQueryImpl query in queries)
196-
{
197-
query.VerifyParameters();
198-
QueryParameters queryParameters = query.GetQueryParameters();
199-
queryParameters.ValidateParameters();
200-
foreach (var translator in await (query.GetTranslatorsAsync(session, queryParameters, cancellationToken)).ConfigureAwait(false))
201-
{
202-
translators.Add(translator);
203-
translatorQueryMap.Add(queryIndex);
204-
parameters.Add(queryParameters);
205-
ISqlCommand singleCommand = translator.Loader.CreateSqlCommand(queryParameters, session);
206-
resultSetsCommand.Append(singleCommand);
207-
}
208-
queryIndex++;
209-
}
210-
}
211-
212199
public async Task<object> GetResultAsync(string key, CancellationToken cancellationToken = default(CancellationToken))
213200
{
214201
cancellationToken.ThrowIfCancellationRequested();
@@ -232,19 +219,17 @@ private async Task<IList> ListIgnoreQueryCacheAsync(CancellationToken cancellati
232219
return GetResultList(await (DoListAsync(cancellationToken)).ConfigureAwait(false));
233220
}
234221

235-
private async Task<IList> ListUsingQueryCacheAsync(CancellationToken cancellationToken)
222+
private async Task<IList> ListUsingQueryCacheAsync(HashSet<string> querySpaces, CancellationToken cancellationToken)
236223
{
237224
cancellationToken.ThrowIfCancellationRequested();
238225
IQueryCache queryCache = session.Factory.GetQueryCache(cacheRegion);
239226

240227
ISet<FilterKey> filterKeys = FilterKey.CreateFilterKeys(session.EnabledFilters);
241228

242-
ISet<string> querySpaces = new HashSet<string>();
243229
List<IType[]> resultTypesList = new List<IType[]>(Translators.Count);
244230
for (int i = 0; i < Translators.Count; i++)
245231
{
246232
ITranslator queryTranslator = Translators[i];
247-
querySpaces.UnionWith(queryTranslator.QuerySpaces);
248233
resultTypesList.Add(queryTranslator.ReturnTypes);
249234
}
250235
int[] firstRows = new int[Parameters.Count];

src/NHibernate/Async/Impl/SessionImpl.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ namespace NHibernate.Impl
3737
{
3838
using System.Threading.Tasks;
3939
using System.Threading;
40+
internal static partial class SessionImplExtensions
41+
{
42+
internal static async Task AutoFlushIfRequiredAsync(this ISessionImplementor implementor, ISet<string> querySpaces, CancellationToken cancellationToken)
43+
{
44+
cancellationToken.ThrowIfCancellationRequested();
45+
var autoFlushIfRequiredTask = (implementor as SessionImpl)?.AutoFlushIfRequiredAsync(querySpaces, cancellationToken);
46+
if (autoFlushIfRequiredTask != null)
47+
{
48+
await (autoFlushIfRequiredTask).ConfigureAwait(false);
49+
}
50+
}
51+
}
4052
public sealed partial class SessionImpl : AbstractSessionImpl, IEventSource, ISerializable, IDeserializationCallback
4153
{
4254

@@ -286,6 +298,8 @@ private async Task ListAsync(IQueryExpression queryExpression, QueryParameters q
286298
}
287299
}
288300

301+
// Since v5.2
302+
[Obsolete("This method has no usages and will be removed in a future version")]
289303
public override async Task<IQueryTranslator[]> GetQueriesAsync(IQueryExpression query, bool scalar, CancellationToken cancellationToken)
290304
{
291305
cancellationToken.ThrowIfCancellationRequested();
@@ -678,7 +692,7 @@ public override async Task<object> GetEntityUsingInterceptorAsync(EntityKey key,
678692
/// <param name="querySpaces"></param>
679693
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
680694
/// <returns></returns>
681-
private async Task<bool> AutoFlushIfRequiredAsync(ISet<string> querySpaces, CancellationToken cancellationToken)
695+
internal async Task<bool> AutoFlushIfRequiredAsync(ISet<string> querySpaces, CancellationToken cancellationToken)
682696
{
683697
cancellationToken.ThrowIfCancellationRequested();
684698
using (BeginProcess())

src/NHibernate/Async/Impl/SqlQueryImpl.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,5 @@ public partial class SqlQueryImpl : AbstractQueryImpl, ISQLQuery
105105
After();
106106
}
107107
}
108-
109-
protected internal override Task<IEnumerable<ITranslator>> GetTranslatorsAsync(ISessionImplementor sessionImplementor, QueryParameters queryParameters, CancellationToken cancellationToken)
110-
{
111-
if (cancellationToken.IsCancellationRequested)
112-
{
113-
return Task.FromCanceled<IEnumerable<ITranslator>>(cancellationToken);
114-
}
115-
try
116-
{
117-
return Task.FromResult<IEnumerable<ITranslator>>(GetTranslators(sessionImplementor, queryParameters));
118-
}
119-
catch (Exception ex)
120-
{
121-
return Task.FromException<IEnumerable<ITranslator>>(ex);
122-
}
123-
}
124108
}
125109
}

src/NHibernate/Async/Impl/StatelessSessionImpl.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ public override async Task ListCustomQueryAsync(ICustomQuery customQuery, QueryP
277277
}
278278
}
279279

280+
// Since v5.2
281+
[Obsolete("This method has no usages and will be removed in a future version")]
280282
public override Task<IQueryTranslator[]> GetQueriesAsync(IQueryExpression query, bool scalar, CancellationToken cancellationToken)
281283
{
282284
if (cancellationToken.IsCancellationRequested)

src/NHibernate/Async/Multi/IMultiAnyQuery.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ namespace NHibernate
2121

2222
public partial interface IMultiAnyQuery
2323
{
24-
/// <summary>
25-
/// Method is called right before batch execution.
26-
/// Can be used for various delayed initialization logic.
27-
/// </summary>
28-
/// <param name="session"></param>
29-
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
30-
Task InitAsync(ISessionImplementor session, CancellationToken cancellationToken = default(CancellationToken));
3124

3225
/// <summary>
3326
/// Executed after all commands are processed

src/NHibernate/Async/Multi/MultiAnyCriteriaQuery.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,6 @@ namespace NHibernate
2020
public partial class MultiAnyCriteriaQuery<T> : MultiAnyQueryBase<T>, IMultiAnyQuery<T>
2121
{
2222

23-
protected override Task<List<QueryLoadInfo>> GetQueryLoadInfoAsync(CancellationToken cancellationToken = default(CancellationToken))
24-
{
25-
if (cancellationToken.IsCancellationRequested)
26-
{
27-
return Task.FromCanceled<List<QueryLoadInfo>>(cancellationToken);
28-
}
29-
try
30-
{
31-
return Task.FromResult<List<QueryLoadInfo>>(GetQueryLoadInfo());
32-
}
33-
catch (System.Exception ex)
34-
{
35-
return Task.FromException<List<QueryLoadInfo>>(ex);
36-
}
37-
}
38-
3923
protected override Task<IList<T>> ExecuteQueryNowAsync(CancellationToken cancellationToken = default(CancellationToken))
4024
{
4125
if (cancellationToken.IsCancellationRequested)

src/NHibernate/Async/Multi/MultiAnyQuery.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,6 @@ namespace NHibernate
2020
public partial class MultiAnyQuery<TResult> : MultiAnyQueryBase<TResult>, IMultiAnyQuery<TResult>
2121
{
2222

23-
protected override async Task<List<QueryLoadInfo>> GetQueryLoadInfoAsync(CancellationToken cancellationToken = default(CancellationToken))
24-
{
25-
cancellationToken.ThrowIfCancellationRequested();
26-
Query.VerifyParameters();
27-
QueryParameters queryParameters = Query.GetQueryParameters();
28-
queryParameters.ValidateParameters();
29-
30-
return (await (Query.GetTranslatorsAsync(Session, queryParameters, cancellationToken)).ConfigureAwait(false)).Select(
31-
t => new QueryLoadInfo()
32-
{
33-
Loader = t.Loader,
34-
Parameters = queryParameters,
35-
QuerySpaces = new HashSet<string>(t.QuerySpaces),
36-
}).ToList();
37-
}
38-
3923
protected override Task<IList<TResult>> ExecuteQueryNowAsync(CancellationToken cancellationToken = default(CancellationToken))
4024
{
4125
if (cancellationToken.IsCancellationRequested)

src/NHibernate/Async/Multi/MultiAnyQueryBase.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,6 @@ namespace NHibernate
2626
public abstract partial class MultiAnyQueryBase<TResult> : IMultiAnyQuery, IMultiAnyQuery<TResult>
2727
{
2828

29-
protected abstract Task<List<QueryLoadInfo>> GetQueryLoadInfoAsync(CancellationToken cancellationToken = default(CancellationToken));
30-
31-
public virtual async Task InitAsync(ISessionImplementor session, CancellationToken cancellationToken = default(CancellationToken))
32-
{
33-
cancellationToken.ThrowIfCancellationRequested();
34-
Session = session;
35-
36-
_queryInfos = await (GetQueryLoadInfoAsync(cancellationToken)).ConfigureAwait(false);
37-
38-
var count = _queryInfos.Count;
39-
NewArray(count, out _hydratedObjects);
40-
NewArray(count, out _subselectResultKeys);
41-
NewArray(count, out _loaderResults);
42-
}
43-
4429
public async Task PostProcessAsync(CancellationToken cancellationToken = default(CancellationToken))
4530
{
4631
cancellationToken.ThrowIfCancellationRequested();

0 commit comments

Comments
 (0)