Skip to content
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

Support caching queries with autodiscovered types #3184

Merged
merged 24 commits into from
Oct 27, 2022
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c59e60e
Support caching queries with autodiscovered types
fredericDelaporte Oct 2, 2022
94aa1de
Fix a typo
fredericDelaporte Oct 16, 2022
0bf7921
Generate async files
github-actions[bot] Oct 16, 2022
261ac75
Add the aliases directly into cached data
fredericDelaporte Oct 16, 2022
3f28ad9
Generate async files
github-actions[bot] Oct 16, 2022
8228b04
Minimize the change
fredericDelaporte Oct 16, 2022
81c1b18
Generate async files
github-actions[bot] Oct 16, 2022
b38af02
Fix whitespaces
fredericDelaporte Oct 16, 2022
b7bea83
Fix some tests
fredericDelaporte Oct 16, 2022
300ccee
Generate async files
github-actions[bot] Oct 16, 2022
c355c34
Simplify code
fredericDelaporte Oct 18, 2022
ec0c5a0
Implement breaking change minimizing suggestions
fredericDelaporte Oct 23, 2022
fda5a75
Generate async files
github-actions[bot] Oct 23, 2022
ee43157
Fix some more metadata reserved slots changes
fredericDelaporte Oct 23, 2022
594d694
Generate async files
github-actions[bot] Oct 23, 2022
b0a2fd2
Fix a regression fromminimizing breaking change
fredericDelaporte Oct 23, 2022
8bbd84e
Add a JsonSerializer test and fix it
fredericDelaporte Oct 23, 2022
8232bcf
Simplify metadata extraction
fredericDelaporte Oct 23, 2022
c07c6af
Add test for multi-columns cached queries
fredericDelaporte Oct 23, 2022
4b1d56b
Fix a typo
fredericDelaporte Oct 23, 2022
ae9c660
Apply code suggestions
fredericDelaporte Oct 24, 2022
72ee45c
Merge branch 'master' into GH3169
fredericDelaporte Oct 25, 2022
50078f0
Simplify code. Fix comment. Cleanup whitespaces
hazzik Oct 27, 2022
b165a88
Merge branch 'master' into GH3169
hazzik Oct 27, 2022
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
Prev Previous commit
Next Next commit
Fix a regression fromminimizing breaking change
And fix the empty case.
fredericDelaporte committed Oct 23, 2022
commit b0a2fd254f1ba1982f515c3ee43c3f412a62037c
28 changes: 14 additions & 14 deletions src/NHibernate.Test/Async/QueryTest/MultiCriteriaFixture.cs
Original file line number Diff line number Diff line change
@@ -139,7 +139,7 @@ public async Task CanUseSecondLevelCacheWithPositionalParametersAsync()

await (CreateItemsAsync());

await (DoMutiQueryAndAssertAsync());
await (DoMultiQueryAndAssertAsync());

Assert.AreEqual(1, cacheHashtable.Count);
}
@@ -148,20 +148,20 @@ public async Task CanUseSecondLevelCacheWithPositionalParametersAsync()
public async Task CanGetMultiQueryFromSecondLevelCacheAsync()
{
await (CreateItemsAsync());
//set the query in the cache
await (DoMutiQueryAndAssertAsync());
// Set the query in the cache.
await (DoMultiQueryAndAssertAsync());

var cacheHashtable = MultipleQueriesFixtureAsync.GetHashTableUsedAsQueryCache(Sfi);
var cachedListEntry = (IList)new ArrayList(cacheHashtable.Values)[0];
var cachedListEntry = (IList) new ArrayList(cacheHashtable.Values)[0];
// The first element is a timestamp, then only we have the cached data.
var cachedQuery = (IList)cachedListEntry[1] ?? throw new InvalidOperationException("Cached data is null");
var cachedQuery = (IList) cachedListEntry[1] ?? throw new InvalidOperationException("Cached data is null");

var firstQueryResults = (IList)cachedQuery[0];
var firstQueryResults = (IList) cachedQuery[0];
firstQueryResults.Clear();
firstQueryResults.Add(3);
firstQueryResults.Add(4);

var secondQueryResults = (IList)cachedQuery[1];
var secondQueryResults = (IList) cachedQuery[1];
secondQueryResults[0] = 2;

using (var s = Sfi.OpenSession())
@@ -173,9 +173,9 @@ public async Task CanGetMultiQueryFromSecondLevelCacheAsync()
.Add(CriteriaTransformer.Clone(criteria).SetProjection(Projections.RowCount()));
multiCriteria.SetCacheable(true);
var results = await (multiCriteria.ListAsync());
var items = (IList)results[0];
var items = (IList) results[0];
Assert.AreEqual(2, items.Count);
var count = (int)((IList)results[1])[0];
var count = (int) ((IList) results[1])[0];
Assert.AreEqual(2L, count);
}
}
@@ -185,12 +185,12 @@ public async Task CanUpdateStatisticsWhenGetMultiQueryFromSecondLevelCacheAsync(
{
await (CreateItemsAsync());

await (DoMutiQueryAndAssertAsync());
await (DoMultiQueryAndAssertAsync());
Assert.AreEqual(0, Sfi.Statistics.QueryCacheHitCount);
Assert.AreEqual(1, Sfi.Statistics.QueryCacheMissCount);
Assert.AreEqual(1, Sfi.Statistics.QueryCachePutCount);

await (DoMutiQueryAndAssertAsync());
await (DoMultiQueryAndAssertAsync());
Assert.AreEqual(1, Sfi.Statistics.QueryCacheHitCount);
Assert.AreEqual(1, Sfi.Statistics.QueryCacheMissCount);
Assert.AreEqual(1, Sfi.Statistics.QueryCachePutCount);
@@ -391,7 +391,7 @@ public async Task CanNotRetrieveDetachedCriteriaResultWithUnknownKeyAsync()
}
}

private async Task DoMutiQueryAndAssertAsync(CancellationToken cancellationToken = default(CancellationToken))
private async Task DoMultiQueryAndAssertAsync(CancellationToken cancellationToken = default(CancellationToken))
{
using (var s = OpenSession())
{
@@ -402,9 +402,9 @@ public async Task CanNotRetrieveDetachedCriteriaResultWithUnknownKeyAsync()
.Add(CriteriaTransformer.Clone(criteria).SetProjection(Projections.RowCount()));
multiCriteria.SetCacheable(true);
var results = await (multiCriteria.ListAsync(cancellationToken));
var items = (IList)results[0];
var items = (IList) results[0];
Assert.AreEqual(89, items.Count);
var count = (int)((IList)results[1])[0];
var count = (int) ((IList) results[1])[0];
Assert.AreEqual(99L, count);
}
}
22 changes: 11 additions & 11 deletions src/NHibernate.Test/Async/QueryTest/MultipleMixedQueriesFixture.cs
Original file line number Diff line number Diff line change
@@ -78,20 +78,20 @@ public void NH_1085_WillGiveReasonableErrorIfBadParameterNameAsync()
public async Task CanGetMultiQueryFromSecondLevelCacheAsync()
{
await (CreateItemsAsync());
//set the query in the cache
await (DoMutiQueryAndAssertAsync());
// Set the query in the cache.
await (DoMultiQueryAndAssertAsync());

var cacheHashtable = MultipleQueriesFixtureAsync.GetHashTableUsedAsQueryCache(Sfi);
var cachedListEntry = (IList)new ArrayList(cacheHashtable.Values)[0];
var cachedListEntry = (IList) new ArrayList(cacheHashtable.Values)[0];
// The first element is a timestamp, then only we have the cached data.
var cachedQuery = (IList) cachedListEntry[1] ?? throw new InvalidOperationException("Cached data is null");

var firstQueryResults = (IList)cachedQuery[0];
var firstQueryResults = (IList) cachedQuery[0];
firstQueryResults.Clear();
firstQueryResults.Add(3);
firstQueryResults.Add(4);

var secondQueryResults = (IList)cachedQuery[1];
var secondQueryResults = (IList) cachedQuery[1];
secondQueryResults[0] = 2L;

using (var s = Sfi.OpenSession())
@@ -104,9 +104,9 @@ public async Task CanGetMultiQueryFromSecondLevelCacheAsync()
.SetInt32(0, 50));
multiQuery.SetCacheable(true);
var results = await (multiQuery.ListAsync());
var items = (IList)results[0];
var items = (IList) results[0];
Assert.AreEqual(2, items.Count);
var count = (long)((IList)results[1])[0];
var count = (long) ((IList) results[1])[0];
Assert.AreEqual(2L, count);
}
}
@@ -186,12 +186,12 @@ public async Task CanUseSecondLevelCacheWithPositionalParametersAsync()

await (CreateItemsAsync());

await (DoMutiQueryAndAssertAsync());
await (DoMultiQueryAndAssertAsync());

Assert.AreEqual(1, cacheHashtable.Count);
}

private async Task DoMutiQueryAndAssertAsync(CancellationToken cancellationToken = default(CancellationToken))
private async Task DoMultiQueryAndAssertAsync(CancellationToken cancellationToken = default(CancellationToken))
{
using (var s = OpenSession())
{
@@ -203,9 +203,9 @@ public async Task CanUseSecondLevelCacheWithPositionalParametersAsync()
.SetInt32(0, 50));
multiQuery.SetCacheable(true);
var results = await (multiQuery.ListAsync(cancellationToken));
var items = (IList)results[0];
var items = (IList) results[0];
Assert.AreEqual(89, items.Count);
var count = (long)((IList)results[1])[0];
var count = (long) ((IList) results[1])[0];
Assert.AreEqual(99L, count);
}
}
22 changes: 11 additions & 11 deletions src/NHibernate.Test/Async/QueryTest/MultipleQueriesFixture.cs
Original file line number Diff line number Diff line change
@@ -81,20 +81,20 @@ public void NH_1085_WillGiveReasonableErrorIfBadParameterNameAsync()
public async Task CanGetMultiQueryFromSecondLevelCacheAsync()
{
await (CreateItemsAsync());
//set the query in the cache
await (DoMutiQueryAndAssertAsync());
// Set the query in the cache.
await (DoMultiQueryAndAssertAsync());

var cacheHashtable = GetHashTableUsedAsQueryCache(Sfi);
var cachedListEntry = (IList)new ArrayList(cacheHashtable.Values)[0];
var cachedListEntry = (IList) new ArrayList(cacheHashtable.Values)[0];
// The first element is a timestamp, then only we have the cached data.
var cachedQuery = (IList) cachedListEntry[1] ?? throw new InvalidOperationException("Cached data is null");

var firstQueryResults = (IList)cachedQuery[0];
var firstQueryResults = (IList) cachedQuery[0];
firstQueryResults.Clear();
firstQueryResults.Add(3);
firstQueryResults.Add(4);

var secondQueryResults = (IList)cachedQuery[1];
var secondQueryResults = (IList) cachedQuery[1];
secondQueryResults[0] = 2L;

using (var s = Sfi.OpenSession())
@@ -107,9 +107,9 @@ public async Task CanGetMultiQueryFromSecondLevelCacheAsync()
.SetInt32(0, 50));
multiQuery.SetCacheable(true);
var results = await (multiQuery.ListAsync());
var items = (IList)results[0];
var items = (IList) results[0];
Assert.AreEqual(2, items.Count);
var count = (long)((IList)results[1])[0];
var count = (long) ((IList) results[1])[0];
Assert.AreEqual(2L, count);
}
}
@@ -188,12 +188,12 @@ public async Task CanUseSecondLevelCacheWithPositionalParametersAsync()

await (CreateItemsAsync());

await (DoMutiQueryAndAssertAsync());
await (DoMultiQueryAndAssertAsync());

Assert.AreEqual(1, cacheHashtable.Count);
}

private async Task DoMutiQueryAndAssertAsync(CancellationToken cancellationToken = default(CancellationToken))
private async Task DoMultiQueryAndAssertAsync(CancellationToken cancellationToken = default(CancellationToken))
{
using (var s = OpenSession())
{
@@ -205,9 +205,9 @@ public async Task CanUseSecondLevelCacheWithPositionalParametersAsync()
.SetInt32(0, 50));
multiQuery.SetCacheable(true);
var results = await (multiQuery.ListAsync(cancellationToken));
var items = (IList)results[0];
var items = (IList) results[0];
Assert.AreEqual(89, items.Count);
var count = (long)((IList)results[1])[0];
var count = (long) ((IList) results[1])[0];
Assert.AreEqual(99L, count);
}
}
73 changes: 70 additions & 3 deletions src/NHibernate.Test/Async/SqlTest/Query/NativeSQLQueriesFixture.cs
Original file line number Diff line number Diff line change
@@ -208,7 +208,7 @@ public async Task SQLQueryInterfaceCacheableAsync()
}

[Test(Description = "GH-2904")]
public async Task CacheableScalarSQLQueryAsync()
public async Task CacheableScalarSqlQueryAsync()
{
Organization ifa = new Organization("IFA");
Organization jboss = new Organization("JBoss");
@@ -270,7 +270,7 @@ class ResultDto
}

[Test(Description = "GH-3169")]
public async Task CacheableScalarSQLQueryWithTransformerAsync()
public async Task CacheableScalarSqlQueryWithTransformerAsync()
{
Organization ifa = new Organization("IFA");

@@ -306,7 +306,34 @@ async Task AssertQueryAsync(bool fromCache)
}

[Test(Description = "GH-3169")]
public async Task CacheableScalarSQLMultiQueryWithTransformerAsync()
public async Task CacheableScalarSqlEmptyQueryWithTransformerAsync()
{
async Task AssertQueryAsync(bool fromCache)
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
using (EnableStatisticsScope())
{
var l = await (s.CreateSQLQuery("select org.NAME as regionCode from ORGANIZATION org")
.AddScalar("regionCode", NHibernateUtil.String)
.SetResultTransformer(Transformers.AliasToBean<ResultDto>())
.SetCacheable(true)
.ListAsync());
await (t.CommitAsync());

Assert.That(l.Count, Is.EqualTo(0));
var msg = "Results are expected from " + (fromCache ? "cache" : "DB");
Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(fromCache ? 0 : 1), msg);
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(fromCache ? 1 : 0), msg);
}
}

await (AssertQueryAsync(false));
await (AssertQueryAsync(true));
}

[Test(Description = "GH-3169")]
public async Task CacheableScalarSqlMultiQueryWithTransformerAsync()
{
Organization ifa = new Organization("IFA");

@@ -354,6 +381,46 @@ async Task AssertQueryAsync(bool fromCache)
await (AssertQueryAsync(true));
}

[Test(Description = "GH-3169")]
public async Task CacheableScalarSqlEmptyMultiQueryWithTransformerAsync()
{
async Task AssertQueryAsync(bool fromCache)
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
using (EnableStatisticsScope())
{
var q1 = s.CreateSQLQuery("select org.NAME as regionCode from ORGANIZATION org")
.AddScalar("regionCode", NHibernateUtil.String)
.SetResultTransformer(Transformers.AliasToBean<ResultDto>())
.SetCacheable(true);
var q2 = s.CreateSQLQuery("select org.ORGID as orgId from ORGANIZATION org")
.AddScalar("orgId", NHibernateUtil.Int64)
.SetResultTransformer(Transformers.AliasToBean<ResultDto>())
.SetCacheable(true);

var batch = s.CreateQueryBatch();
batch.Add<ResultDto>(q1);
batch.Add<ResultDto>(q2);
await (batch.ExecuteAsync());

var l1 = await (batch.GetResultAsync<ResultDto>(0));
var l2 = await (batch.GetResultAsync<ResultDto>(1));

await (t.CommitAsync());

Assert.That(l1.Count, Is.EqualTo(0), "Unexpected results count for the first query.");
Assert.That(l2.Count, Is.EqualTo(0), "Unexpected results count for the second query.");
var msg = "Results are expected from " + (fromCache ? "cache" : "DB");
Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(fromCache ? 0 : 2), msg);
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(fromCache ? 2 : 0), msg);
}
}

await (AssertQueryAsync(false));
await (AssertQueryAsync(true));
}

[Test]
public async Task ResultSetMappingDefinitionAsync()
{
28 changes: 14 additions & 14 deletions src/NHibernate.Test/QueryTest/MultiCriteriaFixture.cs
Original file line number Diff line number Diff line change
@@ -127,7 +127,7 @@ public void CanUseSecondLevelCacheWithPositionalParameters()

CreateItems();

DoMutiQueryAndAssert();
DoMultiQueryAndAssert();

Assert.AreEqual(1, cacheHashtable.Count);
}
@@ -136,20 +136,20 @@ public void CanUseSecondLevelCacheWithPositionalParameters()
public void CanGetMultiQueryFromSecondLevelCache()
{
CreateItems();
//set the query in the cache
DoMutiQueryAndAssert();
// Set the query in the cache.
DoMultiQueryAndAssert();

var cacheHashtable = MultipleQueriesFixture.GetHashTableUsedAsQueryCache(Sfi);
var cachedListEntry = (IList)new ArrayList(cacheHashtable.Values)[0];
var cachedListEntry = (IList) new ArrayList(cacheHashtable.Values)[0];
// The first element is a timestamp, then only we have the cached data.
var cachedQuery = (IList)cachedListEntry[1] ?? throw new InvalidOperationException("Cached data is null");
var cachedQuery = (IList) cachedListEntry[1] ?? throw new InvalidOperationException("Cached data is null");

var firstQueryResults = (IList)cachedQuery[0];
var firstQueryResults = (IList) cachedQuery[0];
firstQueryResults.Clear();
firstQueryResults.Add(3);
firstQueryResults.Add(4);

var secondQueryResults = (IList)cachedQuery[1];
var secondQueryResults = (IList) cachedQuery[1];
secondQueryResults[0] = 2;

using (var s = Sfi.OpenSession())
@@ -161,9 +161,9 @@ public void CanGetMultiQueryFromSecondLevelCache()
.Add(CriteriaTransformer.Clone(criteria).SetProjection(Projections.RowCount()));
multiCriteria.SetCacheable(true);
var results = multiCriteria.List();
var items = (IList)results[0];
var items = (IList) results[0];
Assert.AreEqual(2, items.Count);
var count = (int)((IList)results[1])[0];
var count = (int) ((IList) results[1])[0];
Assert.AreEqual(2L, count);
}
}
@@ -173,12 +173,12 @@ public void CanUpdateStatisticsWhenGetMultiQueryFromSecondLevelCache()
{
CreateItems();

DoMutiQueryAndAssert();
DoMultiQueryAndAssert();
Assert.AreEqual(0, Sfi.Statistics.QueryCacheHitCount);
Assert.AreEqual(1, Sfi.Statistics.QueryCacheMissCount);
Assert.AreEqual(1, Sfi.Statistics.QueryCachePutCount);

DoMutiQueryAndAssert();
DoMultiQueryAndAssert();
Assert.AreEqual(1, Sfi.Statistics.QueryCacheHitCount);
Assert.AreEqual(1, Sfi.Statistics.QueryCacheMissCount);
Assert.AreEqual(1, Sfi.Statistics.QueryCachePutCount);
@@ -437,7 +437,7 @@ public void CanNotRetrieveDetachedCriteriaResultWithUnknownKey()
}
}

private void DoMutiQueryAndAssert()
private void DoMultiQueryAndAssert()
{
using (var s = OpenSession())
{
@@ -448,9 +448,9 @@ private void DoMutiQueryAndAssert()
.Add(CriteriaTransformer.Clone(criteria).SetProjection(Projections.RowCount()));
multiCriteria.SetCacheable(true);
var results = multiCriteria.List();
var items = (IList)results[0];
var items = (IList) results[0];
Assert.AreEqual(89, items.Count);
var count = (int)((IList)results[1])[0];
var count = (int) ((IList) results[1])[0];
Assert.AreEqual(99L, count);
}
}
Loading