From e8807b1ca8522838fbd81f26304bca17919cee8f Mon Sep 17 00:00:00 2001 From: gliljas Date: Wed, 12 Apr 2023 17:30:52 +0200 Subject: [PATCH] Corrections in review --- .../HiLoForcedTableSequenceTest.cs | 12 ++++++++ .../PooledForcedTableSequenceTest.cs | 13 +++++++- .../Enhanced/Sequence/HiLoSequenceTest.cs | 12 ++++++++ .../Enhanced/Sequence/PooledSequenceTest.cs | 10 +++++++ .../IdGen/Enhanced/Table/HiLoTableTest.cs | 12 ++++++++ .../IdGen/Enhanced/Table/PooledLoTableTest.cs | 10 +++++++ .../IdGen/Enhanced/Table/PooledTableTest.cs | 10 +++++++ .../HiLoForcedTableSequenceTest.cs | 12 ++++++++ .../PooledForcedTableSequenceTest.cs | 13 +++++++- .../Enhanced/Sequence/HiLoSequenceTest.cs | 12 ++++++++ .../Enhanced/Sequence/PooledSequenceTest.cs | 10 +++++++ .../IdGen/Enhanced/Table/HiLoTableTest.cs | 12 ++++++++ .../IdGen/Enhanced/Table/PooledLoTableTest.cs | 10 +++++++ .../IdGen/Enhanced/Table/PooledTableTest.cs | 10 +++++++ .../Async/Id/Enhanced/OptimizerFactory.cs | 22 +++++++------- .../Async/Id/SequenceHiLoGenerator.cs | 7 +++-- src/NHibernate/Async/Id/TableHiLoGenerator.cs | 7 +++-- .../Id/Enhanced/OptimizerFactory.cs | 30 +++++++++++-------- src/NHibernate/Id/SequenceHiLoGenerator.cs | 10 +++---- src/NHibernate/Id/TableHiLoGenerator.cs | 10 +++---- src/NHibernate/Id/TenantStateStore.cs | 18 +++++------ 21 files changed, 211 insertions(+), 51 deletions(-) diff --git a/src/NHibernate.Test/Async/IdGen/Enhanced/Forcedtable/HiLoForcedTableSequenceTest.cs b/src/NHibernate.Test/Async/IdGen/Enhanced/Forcedtable/HiLoForcedTableSequenceTest.cs index abf9104e83f..0cff9650e6a 100644 --- a/src/NHibernate.Test/Async/IdGen/Enhanced/Forcedtable/HiLoForcedTableSequenceTest.cs +++ b/src/NHibernate.Test/Async/IdGen/Enhanced/Forcedtable/HiLoForcedTableSequenceTest.cs @@ -58,6 +58,12 @@ public async Task TestNormalBoundaryAsync() long expectedId = i + 1; Assert.That(entities[i].Id, Is.EqualTo(expectedId)); Assert.That(generator.DatabaseStructure.TimesAccessed, Is.EqualTo(1)); // initialization + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(1)); // initialization + Assert.That(optimizer.LastValue, Is.EqualTo(i + 1)); + Assert.That(optimizer.HiValue, Is.EqualTo(increment + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(1)); // initialization Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(i + 1)); Assert.That(optimizer.GetHiValue(TenantIdentifier), Is.EqualTo(increment + 1)); @@ -68,6 +74,12 @@ public async Task TestNormalBoundaryAsync() await (session.SaveAsync(entities[increment])); Assert.That(entities[optimizer.IncrementSize].Id, Is.EqualTo(optimizer.IncrementSize + 1)); Assert.That(generator.DatabaseStructure.TimesAccessed, Is.EqualTo(2)); // initialization + clock-over + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(2)); // initialization + clock-over + Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1)); + Assert.That(optimizer.HiValue, Is.EqualTo(increment * 2 + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(2)); // initialization + clock-over Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(increment + 1)); Assert.That(optimizer.GetHiValue(TenantIdentifier), Is.EqualTo(increment * 2 + 1)); diff --git a/src/NHibernate.Test/Async/IdGen/Enhanced/Forcedtable/PooledForcedTableSequenceTest.cs b/src/NHibernate.Test/Async/IdGen/Enhanced/Forcedtable/PooledForcedTableSequenceTest.cs index 9f5f3a4168a..03924789aaa 100644 --- a/src/NHibernate.Test/Async/IdGen/Enhanced/Forcedtable/PooledForcedTableSequenceTest.cs +++ b/src/NHibernate.Test/Async/IdGen/Enhanced/Forcedtable/PooledForcedTableSequenceTest.cs @@ -59,6 +59,11 @@ public async Task TestNormalBoundaryAsync() Assert.That(entities[i].Id, Is.EqualTo(expectedId)); // NOTE : initialization calls table twice Assert.That(generator.DatabaseStructure.TimesAccessed, Is.EqualTo(2)); // initialization + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(increment + 1)); // initialization + Assert.That(optimizer.LastValue, Is.EqualTo(i + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(increment + 1)); // initialization Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(i + 1)); } @@ -70,9 +75,15 @@ public async Task TestNormalBoundaryAsync() Assert.That(entities[optimizer.IncrementSize].Id, Is.EqualTo(optimizer.IncrementSize + 1)); // initialization (2) + clock over Assert.That(generator.DatabaseStructure.TimesAccessed, Is.EqualTo(3)); - Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(increment*2 + 1)); + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(increment * 2 + 1)); + Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1)); + } + Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(increment * 2 + 1)); Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(increment + 1)); + await (transaction.CommitAsync()); } diff --git a/src/NHibernate.Test/Async/IdGen/Enhanced/Sequence/HiLoSequenceTest.cs b/src/NHibernate.Test/Async/IdGen/Enhanced/Sequence/HiLoSequenceTest.cs index 4b1606cf330..9942b263e6a 100644 --- a/src/NHibernate.Test/Async/IdGen/Enhanced/Sequence/HiLoSequenceTest.cs +++ b/src/NHibernate.Test/Async/IdGen/Enhanced/Sequence/HiLoSequenceTest.cs @@ -56,6 +56,12 @@ public async Task TestNormalBoundaryAsync() entities[i] = new Entity("" + (i + 1)); await (session.SaveAsync(entities[i])); Assert.That(generator.DatabaseStructure.TimesAccessed, Is.EqualTo(1)); // initialization + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(1)); // initialization + Assert.That(optimizer.LastValue, Is.EqualTo(i + 1)); + Assert.That(optimizer.HiValue, Is.EqualTo(increment + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(1)); // initialization Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(i + 1)); Assert.That(optimizer.GetHiValue(TenantIdentifier), Is.EqualTo(increment + 1)); @@ -65,6 +71,12 @@ public async Task TestNormalBoundaryAsync() entities[increment] = new Entity("" + increment); await (session.SaveAsync(entities[increment])); Assert.That(generator.DatabaseStructure.TimesAccessed, Is.EqualTo(2)); + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(2)); + Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1)); + Assert.That(optimizer.HiValue, Is.EqualTo(increment * 2 + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(2)); Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(increment + 1)); Assert.That(optimizer.GetHiValue(TenantIdentifier), Is.EqualTo(increment * 2 + 1)); diff --git a/src/NHibernate.Test/Async/IdGen/Enhanced/Sequence/PooledSequenceTest.cs b/src/NHibernate.Test/Async/IdGen/Enhanced/Sequence/PooledSequenceTest.cs index 1d06ae11d89..47c2ca1a82d 100644 --- a/src/NHibernate.Test/Async/IdGen/Enhanced/Sequence/PooledSequenceTest.cs +++ b/src/NHibernate.Test/Async/IdGen/Enhanced/Sequence/PooledSequenceTest.cs @@ -55,6 +55,11 @@ public async Task TestNormalBoundaryAsync() entities[i] = new Entity("" + (i + 1)); await (session.SaveAsync(entities[i])); Assert.That(generator.DatabaseStructure.TimesAccessed, Is.EqualTo(2)); // initialization calls seq twice + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(increment + 1)); // initialization calls seq twice + Assert.That(optimizer.LastValue, Is.EqualTo(i + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(increment + 1)); // initialization calls seq twice Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(i + 1)); } @@ -63,6 +68,11 @@ public async Task TestNormalBoundaryAsync() entities[increment] = new Entity("" + increment); await (session.SaveAsync(entities[increment])); Assert.That(generator.DatabaseStructure.TimesAccessed, Is.EqualTo(3)); // initialization (2) + clock over + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(increment * 2 + 1)); // initialization (2) + clock over + Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(increment * 2 + 1)); // initialization (2) + clock over Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(increment + 1)); diff --git a/src/NHibernate.Test/Async/IdGen/Enhanced/Table/HiLoTableTest.cs b/src/NHibernate.Test/Async/IdGen/Enhanced/Table/HiLoTableTest.cs index 2e53c916aea..9938d691506 100644 --- a/src/NHibernate.Test/Async/IdGen/Enhanced/Table/HiLoTableTest.cs +++ b/src/NHibernate.Test/Async/IdGen/Enhanced/Table/HiLoTableTest.cs @@ -55,6 +55,12 @@ public async Task TestNormalBoundaryAsync() entities[i] = new Entity("" + (i + 1)); await (s.SaveAsync(entities[i])); Assert.That(generator.TableAccessCount, Is.EqualTo(1)); // initialization + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(1)); // initialization + Assert.That(optimizer.LastValue, Is.EqualTo(i + 1)); + Assert.That(optimizer.HiValue, Is.EqualTo(increment + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(1)); // initialization Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(i + 1)); Assert.That(optimizer.GetHiValue(TenantIdentifier), Is.EqualTo(increment + 1)); @@ -64,6 +70,12 @@ public async Task TestNormalBoundaryAsync() entities[increment] = new Entity("" + increment); await (s.SaveAsync(entities[increment])); Assert.That(generator.TableAccessCount, Is.EqualTo(2)); + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(2)); + Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1)); + Assert.That(optimizer.HiValue, Is.EqualTo((increment * 2) + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(2)); Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(increment + 1)); Assert.That(optimizer.GetHiValue(TenantIdentifier), Is.EqualTo((increment * 2) + 1)); diff --git a/src/NHibernate.Test/Async/IdGen/Enhanced/Table/PooledLoTableTest.cs b/src/NHibernate.Test/Async/IdGen/Enhanced/Table/PooledLoTableTest.cs index af309699513..a5afc87bb50 100644 --- a/src/NHibernate.Test/Async/IdGen/Enhanced/Table/PooledLoTableTest.cs +++ b/src/NHibernate.Test/Async/IdGen/Enhanced/Table/PooledLoTableTest.cs @@ -55,6 +55,11 @@ public async Task TestNormalBoundaryAsync() entities[i] = new Entity("" + (i + 1)); await (s.SaveAsync(entities[i])); Assert.That(generator.TableAccessCount, Is.EqualTo(1)); // initialization + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(1)); // initialization + Assert.That(optimizer.LastValue, Is.EqualTo(i + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(1)); // initialization Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(i + 1)); } @@ -63,6 +68,11 @@ public async Task TestNormalBoundaryAsync() entities[increment] = new Entity("" + increment); await (s.SaveAsync(entities[increment])); Assert.That(generator.TableAccessCount, Is.EqualTo(2)); + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(increment + 1)); + Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(increment + 1)); Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(increment + 1)); diff --git a/src/NHibernate.Test/Async/IdGen/Enhanced/Table/PooledTableTest.cs b/src/NHibernate.Test/Async/IdGen/Enhanced/Table/PooledTableTest.cs index 15be48dccaa..439de62e4cd 100644 --- a/src/NHibernate.Test/Async/IdGen/Enhanced/Table/PooledTableTest.cs +++ b/src/NHibernate.Test/Async/IdGen/Enhanced/Table/PooledTableTest.cs @@ -54,6 +54,11 @@ public async Task TestNormalBoundaryAsync() entities[i] = new Entity("" + (i + 1)); await (s.SaveAsync(entities[i])); Assert.That(generator.TableAccessCount, Is.EqualTo(2)); // initialization calls seq twice + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(increment + 1)); // initialization calls seq twice + Assert.That(optimizer.LastValue, Is.EqualTo(i + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(increment + 1)); // initialization calls seq twice Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(i + 1)); } @@ -62,6 +67,11 @@ public async Task TestNormalBoundaryAsync() entities[increment] = new Entity("" + increment); await (s.SaveAsync(entities[increment])); Assert.That(generator.TableAccessCount, Is.EqualTo(3)); // initialization (2) + clock over + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo((increment * 2) + 1)); // initialization (2) + clock over + Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo((increment * 2) + 1)); // initialization (2) + clock over Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(increment + 1)); diff --git a/src/NHibernate.Test/IdGen/Enhanced/Forcedtable/HiLoForcedTableSequenceTest.cs b/src/NHibernate.Test/IdGen/Enhanced/Forcedtable/HiLoForcedTableSequenceTest.cs index e8647f3858f..2411dfada16 100644 --- a/src/NHibernate.Test/IdGen/Enhanced/Forcedtable/HiLoForcedTableSequenceTest.cs +++ b/src/NHibernate.Test/IdGen/Enhanced/Forcedtable/HiLoForcedTableSequenceTest.cs @@ -47,6 +47,12 @@ public void TestNormalBoundary() long expectedId = i + 1; Assert.That(entities[i].Id, Is.EqualTo(expectedId)); Assert.That(generator.DatabaseStructure.TimesAccessed, Is.EqualTo(1)); // initialization + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(1)); // initialization + Assert.That(optimizer.LastValue, Is.EqualTo(i + 1)); + Assert.That(optimizer.HiValue, Is.EqualTo(increment + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(1)); // initialization Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(i + 1)); Assert.That(optimizer.GetHiValue(TenantIdentifier), Is.EqualTo(increment + 1)); @@ -57,6 +63,12 @@ public void TestNormalBoundary() session.Save(entities[increment]); Assert.That(entities[optimizer.IncrementSize].Id, Is.EqualTo(optimizer.IncrementSize + 1)); Assert.That(generator.DatabaseStructure.TimesAccessed, Is.EqualTo(2)); // initialization + clock-over + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(2)); // initialization + clock-over + Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1)); + Assert.That(optimizer.HiValue, Is.EqualTo(increment * 2 + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(2)); // initialization + clock-over Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(increment + 1)); Assert.That(optimizer.GetHiValue(TenantIdentifier), Is.EqualTo(increment * 2 + 1)); diff --git a/src/NHibernate.Test/IdGen/Enhanced/Forcedtable/PooledForcedTableSequenceTest.cs b/src/NHibernate.Test/IdGen/Enhanced/Forcedtable/PooledForcedTableSequenceTest.cs index 39598c9d591..4add7229bba 100644 --- a/src/NHibernate.Test/IdGen/Enhanced/Forcedtable/PooledForcedTableSequenceTest.cs +++ b/src/NHibernate.Test/IdGen/Enhanced/Forcedtable/PooledForcedTableSequenceTest.cs @@ -48,6 +48,11 @@ public void TestNormalBoundary() Assert.That(entities[i].Id, Is.EqualTo(expectedId)); // NOTE : initialization calls table twice Assert.That(generator.DatabaseStructure.TimesAccessed, Is.EqualTo(2)); // initialization + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(increment + 1)); // initialization + Assert.That(optimizer.LastValue, Is.EqualTo(i + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(increment + 1)); // initialization Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(i + 1)); } @@ -59,9 +64,15 @@ public void TestNormalBoundary() Assert.That(entities[optimizer.IncrementSize].Id, Is.EqualTo(optimizer.IncrementSize + 1)); // initialization (2) + clock over Assert.That(generator.DatabaseStructure.TimesAccessed, Is.EqualTo(3)); - Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(increment*2 + 1)); + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(increment * 2 + 1)); + Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1)); + } + Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(increment * 2 + 1)); Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(increment + 1)); + transaction.Commit(); } diff --git a/src/NHibernate.Test/IdGen/Enhanced/Sequence/HiLoSequenceTest.cs b/src/NHibernate.Test/IdGen/Enhanced/Sequence/HiLoSequenceTest.cs index 7cd6161bc02..10db766ed8b 100644 --- a/src/NHibernate.Test/IdGen/Enhanced/Sequence/HiLoSequenceTest.cs +++ b/src/NHibernate.Test/IdGen/Enhanced/Sequence/HiLoSequenceTest.cs @@ -45,6 +45,12 @@ public void TestNormalBoundary() entities[i] = new Entity("" + (i + 1)); session.Save(entities[i]); Assert.That(generator.DatabaseStructure.TimesAccessed, Is.EqualTo(1)); // initialization + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(1)); // initialization + Assert.That(optimizer.LastValue, Is.EqualTo(i + 1)); + Assert.That(optimizer.HiValue, Is.EqualTo(increment + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(1)); // initialization Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(i + 1)); Assert.That(optimizer.GetHiValue(TenantIdentifier), Is.EqualTo(increment + 1)); @@ -54,6 +60,12 @@ public void TestNormalBoundary() entities[increment] = new Entity("" + increment); session.Save(entities[increment]); Assert.That(generator.DatabaseStructure.TimesAccessed, Is.EqualTo(2)); + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(2)); + Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1)); + Assert.That(optimizer.HiValue, Is.EqualTo(increment * 2 + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(2)); Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(increment + 1)); Assert.That(optimizer.GetHiValue(TenantIdentifier), Is.EqualTo(increment * 2 + 1)); diff --git a/src/NHibernate.Test/IdGen/Enhanced/Sequence/PooledSequenceTest.cs b/src/NHibernate.Test/IdGen/Enhanced/Sequence/PooledSequenceTest.cs index b18c211b7ba..669c03376db 100644 --- a/src/NHibernate.Test/IdGen/Enhanced/Sequence/PooledSequenceTest.cs +++ b/src/NHibernate.Test/IdGen/Enhanced/Sequence/PooledSequenceTest.cs @@ -44,6 +44,11 @@ public void TestNormalBoundary() entities[i] = new Entity("" + (i + 1)); session.Save(entities[i]); Assert.That(generator.DatabaseStructure.TimesAccessed, Is.EqualTo(2)); // initialization calls seq twice + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(increment + 1)); // initialization calls seq twice + Assert.That(optimizer.LastValue, Is.EqualTo(i + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(increment + 1)); // initialization calls seq twice Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(i + 1)); } @@ -52,6 +57,11 @@ public void TestNormalBoundary() entities[increment] = new Entity("" + increment); session.Save(entities[increment]); Assert.That(generator.DatabaseStructure.TimesAccessed, Is.EqualTo(3)); // initialization (2) + clock over + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(increment * 2 + 1)); // initialization (2) + clock over + Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(increment * 2 + 1)); // initialization (2) + clock over Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(increment + 1)); diff --git a/src/NHibernate.Test/IdGen/Enhanced/Table/HiLoTableTest.cs b/src/NHibernate.Test/IdGen/Enhanced/Table/HiLoTableTest.cs index 014788d6570..d25c772661c 100644 --- a/src/NHibernate.Test/IdGen/Enhanced/Table/HiLoTableTest.cs +++ b/src/NHibernate.Test/IdGen/Enhanced/Table/HiLoTableTest.cs @@ -44,6 +44,12 @@ public void TestNormalBoundary() entities[i] = new Entity("" + (i + 1)); s.Save(entities[i]); Assert.That(generator.TableAccessCount, Is.EqualTo(1)); // initialization + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(1)); // initialization + Assert.That(optimizer.LastValue, Is.EqualTo(i + 1)); + Assert.That(optimizer.HiValue, Is.EqualTo(increment + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(1)); // initialization Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(i + 1)); Assert.That(optimizer.GetHiValue(TenantIdentifier), Is.EqualTo(increment + 1)); @@ -53,6 +59,12 @@ public void TestNormalBoundary() entities[increment] = new Entity("" + increment); s.Save(entities[increment]); Assert.That(generator.TableAccessCount, Is.EqualTo(2)); + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(2)); + Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1)); + Assert.That(optimizer.HiValue, Is.EqualTo((increment * 2) + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(2)); Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(increment + 1)); Assert.That(optimizer.GetHiValue(TenantIdentifier), Is.EqualTo((increment * 2) + 1)); diff --git a/src/NHibernate.Test/IdGen/Enhanced/Table/PooledLoTableTest.cs b/src/NHibernate.Test/IdGen/Enhanced/Table/PooledLoTableTest.cs index e56c2fed120..4181a25372e 100644 --- a/src/NHibernate.Test/IdGen/Enhanced/Table/PooledLoTableTest.cs +++ b/src/NHibernate.Test/IdGen/Enhanced/Table/PooledLoTableTest.cs @@ -44,6 +44,11 @@ public void TestNormalBoundary() entities[i] = new Entity("" + (i + 1)); s.Save(entities[i]); Assert.That(generator.TableAccessCount, Is.EqualTo(1)); // initialization + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(1)); // initialization + Assert.That(optimizer.LastValue, Is.EqualTo(i + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(1)); // initialization Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(i + 1)); } @@ -52,6 +57,11 @@ public void TestNormalBoundary() entities[increment] = new Entity("" + increment); s.Save(entities[increment]); Assert.That(generator.TableAccessCount, Is.EqualTo(2)); + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(increment + 1)); + Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(increment + 1)); Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(increment + 1)); diff --git a/src/NHibernate.Test/IdGen/Enhanced/Table/PooledTableTest.cs b/src/NHibernate.Test/IdGen/Enhanced/Table/PooledTableTest.cs index 4f357bc6066..383b2925b03 100644 --- a/src/NHibernate.Test/IdGen/Enhanced/Table/PooledTableTest.cs +++ b/src/NHibernate.Test/IdGen/Enhanced/Table/PooledTableTest.cs @@ -43,6 +43,11 @@ public void TestNormalBoundary() entities[i] = new Entity("" + (i + 1)); s.Save(entities[i]); Assert.That(generator.TableAccessCount, Is.EqualTo(2)); // initialization calls seq twice + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo(increment + 1)); // initialization calls seq twice + Assert.That(optimizer.LastValue, Is.EqualTo(i + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(increment + 1)); // initialization calls seq twice Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(i + 1)); } @@ -51,6 +56,11 @@ public void TestNormalBoundary() entities[increment] = new Entity("" + increment); s.Save(entities[increment]); Assert.That(generator.TableAccessCount, Is.EqualTo(3)); // initialization (2) + clock over + if (TenantIdentifier == null) + { + Assert.That(optimizer.LastSourceValue, Is.EqualTo((increment * 2) + 1)); // initialization (2) + clock over + Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1)); + } Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo((increment * 2) + 1)); // initialization (2) + clock over Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(increment + 1)); diff --git a/src/NHibernate/Async/Id/Enhanced/OptimizerFactory.cs b/src/NHibernate/Async/Id/Enhanced/OptimizerFactory.cs index f18744e6c08..1e9350f3e94 100644 --- a/src/NHibernate/Async/Id/Enhanced/OptimizerFactory.cs +++ b/src/NHibernate/Async/Id/Enhanced/OptimizerFactory.cs @@ -9,7 +9,6 @@ using System; -using System.Collections.Concurrent; using System.Reflection; using NHibernate.Util; @@ -28,10 +27,11 @@ public partial class HiLoOptimizer : OptimizerSupport public override async Task GenerateAsync(IAccessCallback callback, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); - using (await (_asyncLock.LockAsync()).ConfigureAwait(false)) - { - var generationState = _stateStore.LocateGenerationState(callback.GetTenantIdentifier()); + var generationState = _stateStore.LocateGenerationState(callback.GetTenantIdentifier()); + cancellationToken.ThrowIfCancellationRequested(); + using (await (generationState.AsyncLock.LockAsync()).ConfigureAwait(false)) + { if (generationState.LastSourceValue < 0) { generationState.LastSourceValue = await (callback.GetNextValueAsync(cancellationToken)).ConfigureAwait(false); @@ -106,10 +106,11 @@ public partial class PooledOptimizer : OptimizerSupport, IInitialValueAwareOptim public override async Task GenerateAsync(IAccessCallback callback, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); - using (await (_asyncLock.LockAsync()).ConfigureAwait(false)) - { - var generationState = _stateStore.LocateGenerationState(callback.GetTenantIdentifier()); + var generationState = _stateStore.LocateGenerationState(callback.GetTenantIdentifier()); + cancellationToken.ThrowIfCancellationRequested(); + using (await (generationState.AsyncLock.LockAsync()).ConfigureAwait(false)) + { if (generationState.HiValue < 0) { generationState.Value = await (callback.GetNextValueAsync(cancellationToken)).ConfigureAwait(false); @@ -150,10 +151,11 @@ public partial class PooledLoOptimizer : OptimizerSupport public override async Task GenerateAsync(IAccessCallback callback, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); - using (await (_asyncLock.LockAsync()).ConfigureAwait(false)) - { - var generationState = _stateStore.LocateGenerationState(callback.GetTenantIdentifier()); + var generationState = _stateStore.LocateGenerationState(callback.GetTenantIdentifier()); + cancellationToken.ThrowIfCancellationRequested(); + using (await (generationState.AsyncLock.LockAsync()).ConfigureAwait(false)) + { if (generationState.LastSourceValue < 0 || generationState.Value >= (generationState.LastSourceValue + IncrementSize)) { generationState.LastSourceValue = await (callback.GetNextValueAsync(cancellationToken)).ConfigureAwait(false); diff --git a/src/NHibernate/Async/Id/SequenceHiLoGenerator.cs b/src/NHibernate/Async/Id/SequenceHiLoGenerator.cs index 7ed2b4a2ef4..ddf62bb98f6 100644 --- a/src/NHibernate/Async/Id/SequenceHiLoGenerator.cs +++ b/src/NHibernate/Async/Id/SequenceHiLoGenerator.cs @@ -38,10 +38,11 @@ public partial class SequenceHiLoGenerator : SequenceGenerator public override async Task GenerateAsync(ISessionImplementor session, object obj, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); - using (await (_asyncLock.LockAsync()).ConfigureAwait(false)) - { - var generationState = _stateStore.LocateGenerationState(session.GetTenantIdentifier()); + var generationState = _stateStore.LocateGenerationState(session.GetTenantIdentifier()); + cancellationToken.ThrowIfCancellationRequested(); + using (await (generationState.AsyncLock.LockAsync()).ConfigureAwait(false)) + { if (maxLo < 1) { //keep the behavior consistent even for boundary usages diff --git a/src/NHibernate/Async/Id/TableHiLoGenerator.cs b/src/NHibernate/Async/Id/TableHiLoGenerator.cs index 760556da853..8ee8ab67c50 100644 --- a/src/NHibernate/Async/Id/TableHiLoGenerator.cs +++ b/src/NHibernate/Async/Id/TableHiLoGenerator.cs @@ -37,10 +37,11 @@ public partial class TableHiLoGenerator : TableGenerator public override async Task GenerateAsync(ISessionImplementor session, object obj, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); - using (await (_asyncLock.LockAsync()).ConfigureAwait(false)) - { - var generationState = _stateStore.LocateGenerationState(session.GetTenantIdentifier()); + var generationState = _stateStore.LocateGenerationState(session.GetTenantIdentifier()); + cancellationToken.ThrowIfCancellationRequested(); + using (await (generationState.AsyncLock.LockAsync()).ConfigureAwait(false)) + { if (maxLo < 1) { //keep the behavior consistent even for boundary usages diff --git a/src/NHibernate/Id/Enhanced/OptimizerFactory.cs b/src/NHibernate/Id/Enhanced/OptimizerFactory.cs index d599ce49521..fc239a27145 100644 --- a/src/NHibernate/Id/Enhanced/OptimizerFactory.cs +++ b/src/NHibernate/Id/Enhanced/OptimizerFactory.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Concurrent; using System.Reflection; using NHibernate.Util; @@ -98,7 +97,6 @@ public static IOptimizer BuildOptimizer(string type, System.Type returnClass, in public partial class HiLoOptimizer : OptimizerSupport { - private readonly AsyncLock _asyncLock = new AsyncLock(); private readonly TenantStateStore _stateStore = new TenantStateStore(); public HiLoOptimizer(System.Type returnClass, int incrementSize) : base(returnClass, incrementSize) @@ -115,6 +113,7 @@ public HiLoOptimizer(System.Type returnClass, int incrementSize) : base(returnCl public class GenerationState { + public AsyncLock AsyncLock { get; } = new AsyncLock(); public long LastSourceValue { get; internal set; } = -1; public long Value { get; internal set; } public long UpperLimit { get; internal set; } @@ -162,10 +161,10 @@ public override bool ApplyIncrementSizeToSourceValues public override object Generate(IAccessCallback callback) { - using (_asyncLock.Lock()) - { - var generationState = _stateStore.LocateGenerationState(callback.GetTenantIdentifier()); + var generationState = _stateStore.LocateGenerationState(callback.GetTenantIdentifier()); + using (generationState.AsyncLock.Lock()) + { if (generationState.LastSourceValue < 0) { generationState.LastSourceValue = callback.GetNextValue(); @@ -290,7 +289,6 @@ protected virtual object Make(long value) public partial class PooledOptimizer : OptimizerSupport, IInitialValueAwareOptimizer { private long _initialValue; - private readonly AsyncLock _asyncLock = new AsyncLock(); private readonly TenantStateStore _stateStore = new TenantStateStore(); public PooledOptimizer(System.Type returnClass, int incrementSize) : base(returnClass, incrementSize) { @@ -306,6 +304,7 @@ public PooledOptimizer(System.Type returnClass, int incrementSize) : base(return public class GenerationState { + public AsyncLock AsyncLock { get; } = new AsyncLock(); public long Value { get; internal set; } public long HiValue { get; internal set; } = -1; } @@ -319,6 +318,11 @@ public override long LastSourceValue /// Exposure intended for testing purposes. /// /// + + public long LastValue + { + get { return _stateStore.NoTenantGenerationState.Value - 1; } + } public long GetLastSourceValue(string tenantIdentifier) { return _stateStore.LocateGenerationState(tenantIdentifier).HiValue; @@ -340,10 +344,10 @@ public void InjectInitialValue(long initialValue) public override object Generate(IAccessCallback callback) { - using (_asyncLock.Lock()) - { - var generationState = _stateStore.LocateGenerationState(callback.GetTenantIdentifier()); + var generationState = _stateStore.LocateGenerationState(callback.GetTenantIdentifier()); + using (generationState.AsyncLock.Lock()) + { if (generationState.HiValue < 0) { generationState.Value = callback.GetNextValue(); @@ -380,7 +384,6 @@ public override object Generate(IAccessCallback callback) public partial class PooledLoOptimizer : OptimizerSupport { - private readonly AsyncLock _asyncLock = new AsyncLock(); private readonly TenantStateStore _stateStore = new TenantStateStore(); public PooledLoOptimizer(System.Type returnClass, int incrementSize) : base(returnClass, incrementSize) @@ -397,16 +400,17 @@ public PooledLoOptimizer(System.Type returnClass, int incrementSize) : base(retu public class GenerationState { + public AsyncLock AsyncLock { get; } = new AsyncLock(); public long LastSourceValue { get; internal set; } = -1; public long Value { get; internal set; } } public override object Generate(IAccessCallback callback) { - using (_asyncLock.Lock()) - { - var generationState = _stateStore.LocateGenerationState(callback.GetTenantIdentifier()); + var generationState = _stateStore.LocateGenerationState(callback.GetTenantIdentifier()); + using (generationState.AsyncLock.Lock()) + { if (generationState.LastSourceValue < 0 || generationState.Value >= (generationState.LastSourceValue + IncrementSize)) { generationState.LastSourceValue = callback.GetNextValue(); diff --git a/src/NHibernate/Id/SequenceHiLoGenerator.cs b/src/NHibernate/Id/SequenceHiLoGenerator.cs index 475027dde93..ec20886f66f 100644 --- a/src/NHibernate/Id/SequenceHiLoGenerator.cs +++ b/src/NHibernate/Id/SequenceHiLoGenerator.cs @@ -46,10 +46,10 @@ public partial class SequenceHiLoGenerator : SequenceGenerator private int maxLo; private System.Type returnClass; private TenantStateStore _stateStore; - private readonly AsyncLock _asyncLock = new AsyncLock(); - + private class GenerationState { + public AsyncLock AsyncLock { get; } = new AsyncLock(); public long Lo { get; internal set; } public long Hi { get; internal set; } } @@ -84,10 +84,10 @@ public override void Configure(IType type, IDictionary parms, Di /// The new identifier as a , , or . public override object Generate(ISessionImplementor session, object obj) { - using (_asyncLock.Lock()) - { - var generationState = _stateStore.LocateGenerationState(session.GetTenantIdentifier()); + var generationState = _stateStore.LocateGenerationState(session.GetTenantIdentifier()); + using (generationState.AsyncLock.Lock()) + { if (maxLo < 1) { //keep the behavior consistent even for boundary usages diff --git a/src/NHibernate/Id/TableHiLoGenerator.cs b/src/NHibernate/Id/TableHiLoGenerator.cs index 1629fc7430e..9cf03db31ac 100644 --- a/src/NHibernate/Id/TableHiLoGenerator.cs +++ b/src/NHibernate/Id/TableHiLoGenerator.cs @@ -52,10 +52,10 @@ public partial class TableHiLoGenerator : TableGenerator private long maxLo; private System.Type returnClass; private TenantStateStore _stateStore; - private readonly AsyncLock _asyncLock = new AsyncLock(); - + private class GenerationState { + public AsyncLock AsyncLock { get; } = new AsyncLock(); public long Lo { get; internal set; } public long Hi { get; internal set; } } @@ -89,10 +89,10 @@ public override void Configure(IType type, IDictionary parms, Di /// The new identifier as a . public override object Generate(ISessionImplementor session, object obj) { - using (_asyncLock.Lock()) - { - var generationState = _stateStore.LocateGenerationState(session.GetTenantIdentifier()); + var generationState = _stateStore.LocateGenerationState(session.GetTenantIdentifier()); + using (generationState.AsyncLock.Lock()) + { if (maxLo < 1) { //keep the behavior consistent even for boundary usages diff --git a/src/NHibernate/Id/TenantStateStore.cs b/src/NHibernate/Id/TenantStateStore.cs index fbb4d8372a6..276df6a3e81 100644 --- a/src/NHibernate/Id/TenantStateStore.cs +++ b/src/NHibernate/Id/TenantStateStore.cs @@ -5,36 +5,34 @@ namespace NHibernate.Id { internal class TenantStateStore where TState : new() { - private TState _noTenantState; + private Lazy _noTenantState; private Action _initializer; - private readonly Lazy> _tenantSpecificState = new Lazy>(() => new ConcurrentDictionary()); + private ConcurrentDictionary _tenantSpecificState = new ConcurrentDictionary(); - public TenantStateStore(Action initializer) + public TenantStateStore(Action initializer) : this() { _initializer = initializer; } public TenantStateStore() { + _noTenantState = new Lazy(() => CreateNewState()); } internal TState LocateGenerationState(string tenantIdentifier) { if (tenantIdentifier == null) { - if (_noTenantState == null) - { - _noTenantState = CreateNewState(); - } - return _noTenantState; + return _noTenantState.Value; } else { - return _tenantSpecificState.Value.GetOrAdd(tenantIdentifier, _ => CreateNewState()); + return _tenantSpecificState.GetOrAdd(tenantIdentifier, _ => CreateNewState()); } } - internal TState NoTenantGenerationState => _noTenantState ?? + internal TState NoTenantGenerationState => _noTenantState.IsValueCreated ? + _noTenantState.Value : throw new HibernateException("Could not locate previous generation state for no-tenant");