diff --git a/src/DocumentDbTests/SessionMechanics/identity_map_mechanics.cs b/src/DocumentDbTests/SessionMechanics/identity_map_mechanics.cs index 9b023f32be..3ace77bdc5 100644 --- a/src/DocumentDbTests/SessionMechanics/identity_map_mechanics.cs +++ b/src/DocumentDbTests/SessionMechanics/identity_map_mechanics.cs @@ -192,25 +192,6 @@ public async Task given_record_with_same_id_already_added_then_map_should_be_upd } - [Fact] - public async Task opt_into_identity_map_with_lightweight_sessions() - { - var target = Target.Random(); - - theSession.Store(target); - await theSession.SaveChangesAsync(); - - await using var lightweight = theStore.LightweightSession(); - lightweight.UseIdentityMapFor(); - - var target1 = await lightweight.LoadAsync(target.Id); - var target2 = await lightweight.LoadAsync(target.Id); - var target3 = await lightweight.LoadAsync(target.Id); - - target1.ShouldBeTheSameAs(target2); - target1.ShouldBeTheSameAs(target3); - } - public identity_map_mechanics(DefaultStoreFixture fixture): base(fixture) { } diff --git a/src/DocumentDbTests/Writing/document_inserts.cs b/src/DocumentDbTests/Writing/document_inserts.cs index c9e80f2cb9..eea441451f 100644 --- a/src/DocumentDbTests/Writing/document_inserts.cs +++ b/src/DocumentDbTests/Writing/document_inserts.cs @@ -1,6 +1,5 @@ using System; using System.Linq; -using System.Threading.Tasks; using Marten.Exceptions; using Marten.Testing.Documents; using Marten.Testing.Harness; @@ -27,22 +26,20 @@ public void can_insert_all_new_documents() } [Fact] - public async Task can_insert_a_mixed_bag_of_documents() + public void can_insert_a_mixed_bag_of_documents() { - await theStore.Advanced.Clean.DeleteDocumentsByTypeAsync(typeof(Target)); - var docs = new object[] { Target.Random(), Target.Random(), Target.Random(), new User(), new User(), new User(), new User() }; - await using (var session = theStore.LightweightSession()) + using (var session = theStore.LightweightSession()) { session.InsertObjects(docs); - await session.SaveChangesAsync(); + session.SaveChanges(); } - await using (var query = theStore.QuerySession()) + using (var query = theStore.QuerySession()) { query.Query().Count().ShouldBe(3); query.Query().Count().ShouldBe(4); diff --git a/src/Marten/IDocumentOperations.cs b/src/Marten/IDocumentOperations.cs index 9910bf8914..65cadb2a3c 100644 --- a/src/Marten/IDocumentOperations.cs +++ b/src/Marten/IDocumentOperations.cs @@ -231,13 +231,4 @@ public interface IDocumentOperations: IQuerySession /// /// void QueueSqlCommand(string sql, params object[] parameterValues); - - - /// - /// In the case of a lightweight session, this will direct Marten to opt into identity map mechanics - /// for only the document type T. This is a micro-optimization added for the event sourcing + projections - /// support - /// - /// - public void UseIdentityMapFor(); } diff --git a/src/Marten/Internal/Sessions/DocumentSessionBase.cs b/src/Marten/Internal/Sessions/DocumentSessionBase.cs index 21964051f5..4b7c99b556 100644 --- a/src/Marten/Internal/Sessions/DocumentSessionBase.cs +++ b/src/Marten/Internal/Sessions/DocumentSessionBase.cs @@ -304,7 +304,7 @@ public void SetHeader(string key, object value) return tenantSession; } - protected override IQueryEventStore createEventStore(DocumentStore store, Tenant tenant) + protected override IQueryEventStore CreateEventStore(DocumentStore store, Tenant tenant) { return new EventStore(this, store, tenant); } @@ -365,19 +365,22 @@ private void store(IEnumerable entities) where T : notnull private void storeEntity(T entity, IDocumentStorage storage) where T : notnull { - switch (entity) + if (entity is IVersioned versioned) { - case IVersioned v when v.Version != Guid.Empty: - storage.Store(this, entity, v.Version); - return; - case IRevisioned r when r.Version != 0: - storage.Store(this, entity, r.Version); + if (versioned.Version != Guid.Empty) + { + storage.Store(this, entity, versioned.Version); return; - default: - // Put it in the identity map -- if necessary - storage.Store(this, entity); - break; + } } + else if (entity is IRevisioned revisioned && revisioned.Version != 0) + { + storage.Store(this, entity, revisioned.Version); + return; + } + + // Put it in the identity map -- if necessary + storage.Store(this, entity); } public void EjectPatchedTypes(IUnitOfWork changes) diff --git a/src/Marten/Internal/Sessions/LightweightSession.cs b/src/Marten/Internal/Sessions/LightweightSession.cs index 6c3ec0bd53..bb9d048d24 100644 --- a/src/Marten/Internal/Sessions/LightweightSession.cs +++ b/src/Marten/Internal/Sessions/LightweightSession.cs @@ -1,6 +1,5 @@ #nullable enable using System; -using JasperFx.Core; using Marten.Internal.Storage; using Marten.Services; @@ -15,11 +14,6 @@ internal LightweightSession(DocumentStore store, SessionOptions sessionOptions, internal override DocumentTracking TrackingMode => DocumentTracking.None; - public override void UseIdentityMapFor() - { - _rememberedStorage = _rememberedStorage.AddOrUpdate(typeof(T), _providers.StorageFor().IdentityMap); - } - protected internal override IDocumentStorage selectStorage(DocumentProvider provider) { return provider.Lightweight; diff --git a/src/Marten/Internal/Sessions/QuerySession.Storage.cs b/src/Marten/Internal/Sessions/QuerySession.Storage.cs index 3b0890eb83..7ee16f1416 100644 --- a/src/Marten/Internal/Sessions/QuerySession.Storage.cs +++ b/src/Marten/Internal/Sessions/QuerySession.Storage.cs @@ -10,7 +10,7 @@ namespace Marten.Internal.Sessions; public partial class QuerySession { - protected readonly IProviderGraph _providers; + private readonly IProviderGraph _providers; private ImHashMap _byType = ImHashMap.Empty; @@ -27,25 +27,11 @@ public IDocumentStorage StorageFor(Type documentType) return storage; } - protected ImHashMap _rememberedStorage = ImHashMap.Empty; - public IDocumentStorage StorageFor() where T : notnull { - if (_rememberedStorage.TryFind(typeof(T), out var storage)) return (IDocumentStorage)storage; return selectStorage(_providers.StorageFor()); } - /// - /// In the case of a lightweight session, this will direct Marten to opt into identity map mechanics - /// for only the document type T. This is a micro-optimization added for the event sourcing + projections - /// support - /// - /// - public virtual void UseIdentityMapFor() - { - // Nothing by default - } - public IEventStorage EventStorage() { return (IEventStorage)selectStorage(_providers.StorageFor()); diff --git a/src/Marten/Internal/Sessions/QuerySession.cs b/src/Marten/Internal/Sessions/QuerySession.cs index 3f22d8d34c..8fd16b60de 100644 --- a/src/Marten/Internal/Sessions/QuerySession.cs +++ b/src/Marten/Internal/Sessions/QuerySession.cs @@ -22,7 +22,7 @@ public partial class QuerySession: IMartenSession, IQuerySession public StoreOptions Options { get; } public IQueryEventStore Events { get; } - protected virtual IQueryEventStore createEventStore(DocumentStore store, Tenant tenant) + protected virtual IQueryEventStore CreateEventStore(DocumentStore store, Tenant tenant) { return new QueryEventStore(this, store, tenant); } @@ -71,7 +71,7 @@ internal QuerySession( Serializer = store.Serializer; Options = store.Options; - Events = createEventStore(store, tenant ?? sessionOptions.Tenant); + Events = CreateEventStore(store, tenant ?? sessionOptions.Tenant); Logger = store.Options.Logger().StartSession(this); @@ -84,19 +84,20 @@ public NpgsqlConnection Connection { get { - switch (_connection) + if (_connection is IAlwaysConnectedLifetime lifetime) { - case IAlwaysConnectedLifetime lifetime: - return lifetime.Connection; - case ITransactionStarter starter: - { - var l = starter.Start(); - _connection = l; - return l.Connection; - } - default: - throw new InvalidOperationException( - $"The current lifetime {_connection} is neither a {nameof(IAlwaysConnectedLifetime)} nor a {nameof(ITransactionStarter)}"); + return lifetime.Connection; + } + else if (_connection is ITransactionStarter starter) + { + var l = starter.Start(); + _connection = l; + return l.Connection; + } + else + { + throw new InvalidOperationException( + $"The current lifetime {_connection} is neither a {nameof(IAlwaysConnectedLifetime)} nor a {nameof(ITransactionStarter)}"); } } }