Skip to content

Fix shortcomings of QueryBatcher initial implementation #1789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 11, 2018
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
110 changes: 110 additions & 0 deletions src/NHibernate.Test/Async/Futures/QueryBatchFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,116 @@ public async Task FutureForEagerMappedCollectionAsync()
}
}

[Test]
public async Task AutoDiscoverWorksWithFutureAsync()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var future =
s
.CreateSQLQuery("select count(*) as childCount from EntitySimpleChild where Name like :pattern")
.AddScalar("childCount", NHibernateUtil.Int64)
.SetString("pattern", "Chi%")
.SetCacheable(true)
.FutureValue<long>();

Assert.That(await (future.GetValueAsync()), Is.EqualTo(2L), "From DB");
await (t.CommitAsync());
}

using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var future =
s
.CreateSQLQuery("select count(*) as childCount from EntitySimpleChild where Name like :pattern")
.AddScalar("childCount", NHibernateUtil.Int64)
.SetString("pattern", "Chi%")
.SetCacheable(true)
.FutureValue<long>();

Assert.That(await (future.GetValueAsync()), Is.EqualTo(2L), "From cache");
await (t.CommitAsync());
}
}

[Test]
public async Task AutoFlushCacheInvalidationWorksWithFutureAsync()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var futureResults =
(await (s
.CreateQuery("from EntitySimpleChild")
.SetCacheable(true)
.Future<EntitySimpleChild>()
.GetEnumerableAsync()))
.ToList();

Assert.That(futureResults, Has.Count.EqualTo(2), "First call");

await (t.CommitAsync());
}

using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var deleted = await (s.Query<EntitySimpleChild>().FirstAsync());
// We need to get rid of a referencing entity for the delete.
deleted.Parent.Child1 = null;
deleted.Parent.Child2 = null;
await (s.DeleteAsync(deleted));

var future =
s
.CreateQuery("from EntitySimpleChild")
.SetCacheable(true)
.Future<EntitySimpleChild>();

Assert.That((await (future.GetEnumerableAsync())).ToList(), Has.Count.EqualTo(1), "After delete");
await (t.CommitAsync());
}
}

[Test]
public async Task UsingHqlToFutureWithCacheAndTransformerDoesntThrowAsync()
{
// Adapted from #383
using (var session = OpenSession())
using (var t = session.BeginTransaction())
{
//store values in cache
await (session
.CreateQuery("from EntitySimpleChild")
.SetResultTransformer(Transformers.DistinctRootEntity)
.SetCacheable(true)
.SetCacheMode(CacheMode.Normal)
.Future<EntitySimpleChild>()
.GetEnumerableAsync());
await (t.CommitAsync());
}

using (var session = OpenSession())
using (var t = session.BeginTransaction())
{
//get values from cache
var results =
(await (session
.CreateQuery("from EntitySimpleChild")
.SetResultTransformer(Transformers.DistinctRootEntity)
.SetCacheable(true)
.SetCacheMode(CacheMode.Normal)
.Future<EntitySimpleChild>()
.GetEnumerableAsync()))
.ToList();

Assert.That(results.Count, Is.EqualTo(2));
await (t.CommitAsync());
}
}

#region Test Setup

protected override HbmMapping GetMappings()
Expand Down
110 changes: 110 additions & 0 deletions src/NHibernate.Test/Futures/QueryBatchFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,116 @@ public void FutureForEagerMappedCollection()
}
}

[Test]
public void AutoDiscoverWorksWithFuture()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var future =
s
.CreateSQLQuery("select count(*) as childCount from EntitySimpleChild where Name like :pattern")
.AddScalar("childCount", NHibernateUtil.Int64)
.SetString("pattern", "Chi%")
.SetCacheable(true)
.FutureValue<long>();

Assert.That(future.Value, Is.EqualTo(2L), "From DB");
t.Commit();
}

using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var future =
s
.CreateSQLQuery("select count(*) as childCount from EntitySimpleChild where Name like :pattern")
.AddScalar("childCount", NHibernateUtil.Int64)
.SetString("pattern", "Chi%")
.SetCacheable(true)
.FutureValue<long>();

Assert.That(future.Value, Is.EqualTo(2L), "From cache");
t.Commit();
}
}

[Test]
public void AutoFlushCacheInvalidationWorksWithFuture()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var futureResults =
s
.CreateQuery("from EntitySimpleChild")
.SetCacheable(true)
.Future<EntitySimpleChild>()
.GetEnumerable()
.ToList();

Assert.That(futureResults, Has.Count.EqualTo(2), "First call");

t.Commit();
}

using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var deleted = s.Query<EntitySimpleChild>().First();
// We need to get rid of a referencing entity for the delete.
deleted.Parent.Child1 = null;
deleted.Parent.Child2 = null;
s.Delete(deleted);

var future =
s
.CreateQuery("from EntitySimpleChild")
.SetCacheable(true)
.Future<EntitySimpleChild>();

Assert.That(future.GetEnumerable().ToList(), Has.Count.EqualTo(1), "After delete");
t.Commit();
}
}

[Test]
public void UsingHqlToFutureWithCacheAndTransformerDoesntThrow()
{
// Adapted from #383
using (var session = OpenSession())
using (var t = session.BeginTransaction())
{
//store values in cache
session
.CreateQuery("from EntitySimpleChild")
.SetResultTransformer(Transformers.DistinctRootEntity)
.SetCacheable(true)
.SetCacheMode(CacheMode.Normal)
.Future<EntitySimpleChild>()
.GetEnumerable();
t.Commit();
}

using (var session = OpenSession())
using (var t = session.BeginTransaction())
{
//get values from cache
var results =
session
.CreateQuery("from EntitySimpleChild")
.SetResultTransformer(Transformers.DistinctRootEntity)
.SetCacheable(true)
.SetCacheMode(CacheMode.Normal)
.Future<EntitySimpleChild>()
.GetEnumerable()
.ToList();

Assert.That(results.Count, Is.EqualTo(2));
t.Commit();
}
}

#region Test Setup

protected override HbmMapping GetMappings()
Expand Down
17 changes: 14 additions & 3 deletions src/NHibernate/Async/Multi/IQueryBatchItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,30 @@ public partial interface IQueryBatchItem
{

/// <summary>
/// Returns commands generated by query
/// Get the commands to execute for getting the not-already cached results of this query.
/// </summary>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
/// <returns>The commands for obtaining the results not already cached.</returns>
Task<IEnumerable<ISqlCommand>> GetCommandsAsync(CancellationToken cancellationToken);

/// <summary>
/// Executed after all commands in batch are processed
/// Process the result sets generated by <see cref="GetCommands"/>. Advance the results set
/// to the next query, or to its end if this is the last query.
/// </summary>
/// <returns>The number of rows processed.</returns>
Task<int> ProcessResultsSetAsync(DbDataReader reader, CancellationToken cancellationToken);

/// <summary>
/// Process the results of the query, including cached results.
/// </summary>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
/// <remarks>Any result from the database must have been previously processed
/// through <see cref="ProcessResultsSet"/>.</remarks>
Task ProcessResultsAsync(CancellationToken cancellationToken);

/// <summary>
/// Immediate query execution in case the dialect does not support batches
/// Execute immediately the query as a single standalone query. Used in case the data-provider
/// does not support batches.
/// </summary>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
Task ExecuteNonBatchedAsync(CancellationToken cancellationToken);
Expand Down
Loading