Skip to content

Commit

Permalink
Revert "Ability to opt into identity map mechanics for lightweight se…
Browse files Browse the repository at this point in the history
…ssions"

This reverts commit afa49dc.
  • Loading branch information
jeremydmiller authored Jul 5, 2024
1 parent 603ae17 commit b793bdb
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 81 deletions.
19 changes: 0 additions & 19 deletions src/DocumentDbTests/SessionMechanics/identity_map_mechanics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Target>();

var target1 = await lightweight.LoadAsync<Target>(target.Id);
var target2 = await lightweight.LoadAsync<Target>(target.Id);
var target3 = await lightweight.LoadAsync<Target>(target.Id);

target1.ShouldBeTheSameAs(target2);
target1.ShouldBeTheSameAs(target3);
}

public identity_map_mechanics(DefaultStoreFixture fixture): base(fixture)
{
}
Expand Down
11 changes: 4 additions & 7 deletions src/DocumentDbTests/Writing/document_inserts.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Marten.Exceptions;
using Marten.Testing.Documents;
using Marten.Testing.Harness;
Expand All @@ -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<Target>().Count().ShouldBe(3);
query.Query<User>().Count().ShouldBe(4);
Expand Down
9 changes: 0 additions & 9 deletions src/Marten/IDocumentOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,4 @@ public interface IDocumentOperations: IQuerySession
/// <param name="sql"></param>
/// <param name="parameterValues"></param>
void QueueSqlCommand(string sql, params object[] parameterValues);


/// <summary>
/// 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
/// </summary>
/// <typeparam name="T"></typeparam>
public void UseIdentityMapFor<T>();
}
25 changes: 14 additions & 11 deletions src/Marten/Internal/Sessions/DocumentSessionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -365,19 +365,22 @@ private void store<T>(IEnumerable<T> entities) where T : notnull

private void storeEntity<T>(T entity, IDocumentStorage<T> 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)
Expand Down
6 changes: 0 additions & 6 deletions src/Marten/Internal/Sessions/LightweightSession.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#nullable enable
using System;
using JasperFx.Core;
using Marten.Internal.Storage;
using Marten.Services;

Expand All @@ -15,11 +14,6 @@ internal LightweightSession(DocumentStore store, SessionOptions sessionOptions,

internal override DocumentTracking TrackingMode => DocumentTracking.None;

public override void UseIdentityMapFor<T>()
{
_rememberedStorage = _rememberedStorage.AddOrUpdate(typeof(T), _providers.StorageFor<T>().IdentityMap);
}

protected internal override IDocumentStorage<T> selectStorage<T>(DocumentProvider<T> provider)
{
return provider.Lightweight;
Expand Down
16 changes: 1 addition & 15 deletions src/Marten/Internal/Sessions/QuerySession.Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Marten.Internal.Sessions;

public partial class QuerySession
{
protected readonly IProviderGraph _providers;
private readonly IProviderGraph _providers;

private ImHashMap<Type, IDocumentStorage> _byType = ImHashMap<Type, IDocumentStorage>.Empty;

Expand All @@ -27,25 +27,11 @@ public IDocumentStorage StorageFor(Type documentType)
return storage;
}

protected ImHashMap<Type, IDocumentStorage> _rememberedStorage = ImHashMap<Type, IDocumentStorage>.Empty;

public IDocumentStorage<T> StorageFor<T>() where T : notnull
{
if (_rememberedStorage.TryFind(typeof(T), out var storage)) return (IDocumentStorage<T>)storage;
return selectStorage(_providers.StorageFor<T>());
}

/// <summary>
/// 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
/// </summary>
/// <typeparam name="T"></typeparam>
public virtual void UseIdentityMapFor<T>()
{
// Nothing by default
}

public IEventStorage EventStorage()
{
return (IEventStorage)selectStorage(_providers.StorageFor<IEvent>());
Expand Down
29 changes: 15 additions & 14 deletions src/Marten/Internal/Sessions/QuerySession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);

Expand All @@ -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)}");
}
}
}
Expand Down

0 comments on commit b793bdb

Please sign in to comment.