Skip to content

Commit

Permalink
Compiled query code generation mechanics improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy D. Miller authored and Jeremy D. Miller committed Feb 3, 2022
1 parent 145574c commit 4ba7b3a
Show file tree
Hide file tree
Showing 159 changed files with 6,864 additions and 3,199 deletions.
1 change: 1 addition & 0 deletions build/build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ private static void Main(string[] args)
RunTests("Marten.Testing");
RunTests("ConfigurationTests");
RunTests("CoreTests");
RunTests("DocumentDbTests");
});

Target("rebuild-database", () =>
Expand Down
8 changes: 4 additions & 4 deletions docs/configuration/storeoptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public class OrganizationRegistry: MartenRegistry
}
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/CoreFunctionality/MartenRegistryTests.cs#L137-L148' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_organizationregistry' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/CoreFunctionality/MartenRegistryTests.cs#L136-L147' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_organizationregistry' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

To apply your new `MartenRegistry`, just include it when you bootstrap the `IDocumentStore` as in this example:
Expand All @@ -109,7 +109,7 @@ var store = DocumentStore.For(opts =>
opts.Connection(ConnectionSource.ConnectionString);
});
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/CoreFunctionality/MartenRegistryTests.cs#L170-L179' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_including_a_custom_martenregistry' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/CoreFunctionality/MartenRegistryTests.cs#L169-L178' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_including_a_custom_martenregistry' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Do note that you could happily use multiple `MartenRegistry` classes in larger applications if that is advantageous.
Expand All @@ -131,7 +131,7 @@ var store = DocumentStore.For(opts =>
.For<User>().Duplicate(x => x.UserName);
});
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/CoreFunctionality/MartenRegistryTests.cs#L152-L164' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_storeoptions_schema' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/CoreFunctionality/MartenRegistryTests.cs#L151-L163' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_storeoptions_schema' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Custom Attributes
Expand Down Expand Up @@ -328,5 +328,5 @@ var store = DocumentStore.For(_ =>
_.NameDataLength = 100;
});
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/CoreFunctionality/StoreOptionsTests.cs#L257-L266' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_setting-name-data-length' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/ConfigurationTests/StoreOptionsTests.cs#L284-L293' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_setting-name-data-length' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
18 changes: 8 additions & 10 deletions docs/diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ using (var store = SeparateStore(_ =>
_.Listeners.Add(stub2);
}))
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/CoreFunctionality/Using_Global_DocumentSessionListener_Tests.cs#L27-L39' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_registering-a-document-session-listener' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/CoreFunctionality/Using_Global_DocumentSessionListener_Tests.cs#L26-L38' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_registering-a-document-session-listener' title='Start of snippet'>anchor</a></sup>
<a id='snippet-sample_registering-a-document-session-listener-1'></a>
```cs
var stub1 = new StubDocumentSessionListener();
Expand All @@ -97,7 +97,7 @@ using (var store = SeparateStore(_ =>
_.AutoCreateSchemaObjects = AutoCreate.All;
}))
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/CoreFunctionality/Using_Local_DocumentSessionListener_Tests.cs#L24-L33' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_registering-a-document-session-listener-1' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/CoreFunctionality/Using_Local_DocumentSessionListener_Tests.cs#L20-L29' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_registering-a-document-session-listener-1' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

The listeners can be used to modify an `IDocumentSession` and its related unit of work just before persisting. Marten itself will be using this mechanism
Expand Down Expand Up @@ -229,21 +229,19 @@ var store = DocumentStore.For(_ =>
_.Logger(new ConsoleMartenLogger());
});
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/CoreFunctionality/StoreOptionsTests.cs#L115-L122' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_plugging-in-marten-logger' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/ConfigurationTests/StoreOptionsTests.cs#L144-L151' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_plugging-in-marten-logger' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

You can also directly apply a session logger to any `IQuerySession` or `IDocumentSession` like this:

<!-- snippet: sample_plugging-in-session-logger -->
<a id='snippet-sample_plugging-in-session-logger'></a>
```cs
using (var session = store.OpenSession())
{
// Replace the logger for only this one session
session.Logger = new RecordingLogger();
}
using var session = store.OpenSession();
// Replace the logger for only this one session
session.Logger = new RecordingLogger();
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/CoreFunctionality/StoreOptionsTests.cs#L124-L132' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_plugging-in-session-logger' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/ConfigurationTests/StoreOptionsTests.cs#L153-L159' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_plugging-in-session-logger' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

The session logging is a different abstraction specifically so that you _could_ track database commands issued per session. In effect, my own shop is going to use this capability to understand what HTTP endpoints or service bus message handlers are being unnecessarily chatty in their database interactions. We also hope that the contextual logging of commands per document session makes it easier to understand how our systems behave.
Expand Down Expand Up @@ -427,5 +425,5 @@ Marten provides a helper method to fetch the PostgreSQL server version exposed v
```cs
var pgVersion = theStore.Diagnostics.GetPostgresVersion();
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/CoreFunctionality/ability_to_fetch_postgres_server_version.cs#L11-L13' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_get_postgres_version' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/Diagnostics/ability_to_fetch_postgres_server_version.cs#L11-L13' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_get_postgres_version' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
128 changes: 60 additions & 68 deletions docs/documents/deletes.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ theSession.DeleteWhere<Target>(x => x.Double == 578);

theSession.SaveChanges();
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/CoreFunctionality/delete_many_documents_by_query_Tests.cs#L28-L32' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_deletewhere' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Deleting/delete_many_documents_by_query.cs#L27-L31' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_deletewhere' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

A couple things to note:
Expand Down Expand Up @@ -159,26 +159,24 @@ public void query_soft_deleted_docs()
var user3 = new User { UserName = "baz" };
var user4 = new User { UserName = "jack" };

using (var session = theStore.OpenSession())
{
session.Store(user1, user2, user3, user4);
session.SaveChanges();
using var session = theStore.OpenSession();
session.Store(user1, user2, user3, user4);
session.SaveChanges();

// Deleting 'bar' and 'baz'
session.DeleteWhere<User>(x => x.UserName.StartsWith("b"));
session.SaveChanges();
// Deleting 'bar' and 'baz'
session.DeleteWhere<User>(x => x.UserName.StartsWith("b"));
session.SaveChanges();

// no where clause, deleted docs should be filtered out
session.Query<User>().OrderBy(x => x.UserName).Select(x => x.UserName)
.ToList().ShouldHaveTheSameElementsAs("foo", "jack");
// no where clause, deleted docs should be filtered out
session.Query<User>().OrderBy(x => x.UserName).Select(x => x.UserName)
.ToList().ShouldHaveTheSameElementsAs("foo", "jack");

// with a where clause
session.Query<User>().Where(x => x.UserName != "jack")
// with a where clause
session.Query<User>().Where(x => x.UserName != "jack")
.ToList().Single().UserName.ShouldBe("foo");
}
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Acceptance/soft_deletes.cs#L285-L313' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_soft_deleted_docs' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Deleting/soft_deletes.cs#L275-L301' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_soft_deleted_docs' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

The SQL generated for the first call to `Query<User>()` above would be:
Expand All @@ -203,28 +201,26 @@ public void query_maybe_soft_deleted_docs()
var user3 = new User { UserName = "baz" };
var user4 = new User { UserName = "jack" };

using (var session = theStore.OpenSession())
{
session.Store(user1, user2, user3, user4);
session.SaveChanges();

session.DeleteWhere<User>(x => x.UserName.StartsWith("b"));
session.SaveChanges();

// no where clause, all documents are returned
session.Query<User>().Where(x => x.MaybeDeleted()).OrderBy(x => x.UserName).Select(x => x.UserName)
.ToList().ShouldHaveTheSameElementsAs("bar", "baz", "foo", "jack");

// with a where clause, all documents are returned
session.Query<User>().Where(x => x.UserName != "jack" && x.MaybeDeleted())
.OrderBy(x => x.UserName)
.ToList()
.Select(x => x.UserName)
.ShouldHaveTheSameElementsAs("bar", "baz", "foo");
}
using var session = theStore.OpenSession();
session.Store(user1, user2, user3, user4);
session.SaveChanges();

session.DeleteWhere<User>(x => x.UserName.StartsWith("b"));
session.SaveChanges();

// no where clause, all documents are returned
session.Query<User>().Where(x => x.MaybeDeleted()).OrderBy(x => x.UserName).Select(x => x.UserName)
.ToList().ShouldHaveTheSameElementsAs("bar", "baz", "foo", "jack");

// with a where clause, all documents are returned
session.Query<User>().Where(x => x.UserName != "jack" && x.MaybeDeleted())
.OrderBy(x => x.UserName)
.ToList()
.Select(x => x.UserName)
.ShouldHaveTheSameElementsAs("bar", "baz", "foo");
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Acceptance/soft_deletes.cs#L315-L345' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_maybe_soft_deleted_docs' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Deleting/soft_deletes.cs#L303-L331' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_maybe_soft_deleted_docs' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

### Fetching Only Deleted Documents
Expand All @@ -243,28 +239,26 @@ public void query_is_soft_deleted_docs()
var user3 = new User { UserName = "baz" };
var user4 = new User { UserName = "jack" };

using (var session = theStore.OpenSession())
{
session.Store(user1, user2, user3, user4);
session.SaveChanges();

session.DeleteWhere<User>(x => x.UserName.StartsWith("b"));
session.SaveChanges();

// no where clause
session.Query<User>().Where(x => x.IsDeleted()).OrderBy(x => x.UserName).Select(x => x.UserName)
.ToList().ShouldHaveTheSameElementsAs("bar", "baz");

// with a where clause
session.Query<User>().Where(x => x.UserName != "baz" && x.IsDeleted())
.OrderBy(x => x.UserName)
.ToList()
.Select(x => x.UserName)
.Single().ShouldBe("bar");
}
using var session = theStore.OpenSession();
session.Store(user1, user2, user3, user4);
session.SaveChanges();

session.DeleteWhere<User>(x => x.UserName.StartsWith("b"));
session.SaveChanges();

// no where clause
session.Query<User>().Where(x => x.IsDeleted()).OrderBy(x => x.UserName).Select(x => x.UserName)
.ToList().ShouldHaveTheSameElementsAs("bar", "baz");

// with a where clause
session.Query<User>().Where(x => x.UserName != "baz" && x.IsDeleted())
.OrderBy(x => x.UserName)
.ToList()
.Select(x => x.UserName)
.Single().ShouldBe("bar");
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Acceptance/soft_deletes.cs#L347-L377' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_is_soft_deleted_docs' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Deleting/soft_deletes.cs#L333-L361' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_is_soft_deleted_docs' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

### Fetching Documents Deleted Before or After a Specific Time
Expand All @@ -283,24 +277,22 @@ public void query_is_soft_deleted_since_docs()
var user3 = new User { UserName = "baz" };
var user4 = new User { UserName = "jack" };

using (var session = theStore.OpenSession())
{
session.Store(user1, user2, user3, user4);
session.SaveChanges();
using var session = theStore.OpenSession();
session.Store(user1, user2, user3, user4);
session.SaveChanges();

session.Delete(user3);
session.SaveChanges();
session.Delete(user3);
session.SaveChanges();

var epoch = session.MetadataFor(user3).DeletedAt;
session.Delete(user4);
session.SaveChanges();
var epoch = session.MetadataFor(user3).DeletedAt;
session.Delete(user4);
session.SaveChanges();

session.Query<User>().Where(x => x.DeletedSince(epoch.Value)).Select(x => x.UserName)
.ToList().ShouldHaveTheSameElementsAs("jack");
}
session.Query<User>().Where(x => x.DeletedSince(epoch.Value)).Select(x => x.UserName)
.ToList().ShouldHaveTheSameElementsAs("jack");
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Acceptance/soft_deletes.cs#L379-L405' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_soft_deleted_since' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Deleting/soft_deletes.cs#L363-L387' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_soft_deleted_since' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

_Neither `DeletedSince` nor `DeletedBefore` are inclusive searches as shown_below:
Expand Down
Loading

0 comments on commit 4ba7b3a

Please sign in to comment.