diff --git a/docs/documents/storing.md b/docs/documents/storing.md index 77b69f51bd..0a852f9914 100644 --- a/docs/documents/storing.md +++ b/docs/documents/storing.md @@ -101,7 +101,7 @@ await theStore.BulkInsertAsync(data, batchSize: 500); // And just checking that the data is actually there;) theSession.Query().Count().ShouldBe(data.Length); ``` -snippet source | anchor +snippet source | anchor The bulk insert is done with a single transaction. For really large document collections, you may need to page the calls to `IDocumentStore.BulkInsert()`. @@ -126,7 +126,7 @@ await theStore.BulkInsertAsync(data, batchSize: 500); // And just checking that the data is actually there;) theSession.Query().Count().ShouldBe(data.Length); ``` -snippet source | anchor +snippet source | anchor By default, bulk insert will fail if there are any duplicate id's between the documents being inserted and the existing database data. You can alter this behavior through the `BulkInsertMode` enumeration as shown below: @@ -150,8 +150,11 @@ await store.BulkInsertDocumentsAsync(data, BulkInsertMode.InsertsOnly); // Overwrite any existing documents with the same identity as the documents // being loaded await store.BulkInsertDocumentsAsync(data, BulkInsertMode.OverwriteExisting); + +// Overwrite any existing documents when the expected version matches +await store.BulkInsertDocumentsAsync(data, BulkInsertMode.OverwriteIfVersionMatches); ``` -snippet source | anchor +snippet source | anchor The bulk insert feature can also be used with multi-tenanted documents, but in that @@ -173,5 +176,20 @@ using var store = DocumentStore.For(opts => // If multi-tenanted await store.BulkInsertDocumentsAsync("a tenant id", data); ``` -snippet source | anchor +snippet source | anchor + + +### Bulk Loading with Expected Versions + +There is also a bulk insert mode that will only overwrite the documents _only if_ the version of the document being updated +matches the version being supplied: + + + +```cs +await store.BulkInsertDocumentsAsync(data, BulkInsertMode.OverwriteIfVersionMatches); +``` +snippet source | anchor + +See [the documentation on optimistic concurrency and document versioning](/documents/concurrency) for more information. diff --git a/docs/events/projections/async-daemon.md b/docs/events/projections/async-daemon.md index 42d097c1e1..c8b64b1f07 100644 --- a/docs/events/projections/async-daemon.md +++ b/docs/events/projections/async-daemon.md @@ -220,7 +220,7 @@ You can see the usage below from one of the Marten tests where we use that metho daemon has caught up: - + ```cs [Fact] public async Task run_simultaneously() @@ -241,7 +241,7 @@ public async Task run_simultaneously() await CheckExpectedResults(); } ``` -snippet source | anchor +snippet source | anchor The basic idea in your tests is to: @@ -289,7 +289,7 @@ public async Task run_simultaneously() The following code shows the diagnostics support for the async daemon as it is today: - + ```cs public static async Task ShowDaemonDiagnostics(IDocumentStore store) { @@ -308,7 +308,7 @@ public static async Task ShowDaemonDiagnostics(IDocumentStore store) Console.WriteLine($"The daemon high water sequence mark is {daemonHighWaterMark}"); } ``` -snippet source | anchor +snippet source | anchor ## Command Line Support @@ -429,7 +429,7 @@ from systems using Marten. If your system is configured to export metrics and Open Telemetry data from Marten like this: - + ```cs // This is passed in by Project Aspire. The exporter usage is a little // different for other tools like Prometheus or SigNoz @@ -448,7 +448,7 @@ builder.Services.AddOpenTelemetry() metrics.AddMeter("Marten"); }); ``` -snippet source | anchor +snippet source | anchor *And* you are running the async daemon in your system, you should see potentially activities for each running projection diff --git a/docs/events/versioning.md b/docs/events/versioning.md index 29138bbc5a..541facf35f 100644 --- a/docs/events/versioning.md +++ b/docs/events/versioning.md @@ -307,7 +307,7 @@ public record ShoppingCartOpened( Guid ClientId ); ``` -snippet source | anchor +snippet source | anchor We want to enrich it with shopping cart status and client name. To have a more straightforward structure, we'd like to group the client id and name into a nested object. @@ -334,7 +334,7 @@ public enum ShoppingCartStatus Cancelled = 4 } ``` -snippet source | anchor +snippet source | anchor Marten provides extended capabilities around that and enables different styles for handling the upcasting transformations. @@ -360,7 +360,7 @@ options.Events ) ); ``` -snippet source | anchor +snippet source | anchor It will default take the event type name based on the old CLR type. You can also define it explicitly. It can be helpful if you changed the event schema more than once, and the old CLR class doesn't represent the initial event type name. You can do that with: @@ -379,7 +379,7 @@ options.Events ) ); ``` -snippet source | anchor +snippet source | anchor #### Raw JSON transformation with Json .NET: @@ -403,7 +403,7 @@ options.Events ) ); ``` -snippet source | anchor +snippet source | anchor Add also static import of helper classes to get a concise syntax as above: @@ -413,7 +413,7 @@ Add also static import of helper classes to get a concise syntax as above: ```cs using static Marten.Services.Json.Transformations.JsonNet.JsonTransformations; ``` -snippet source | anchor +snippet source | anchor #### Raw JSON transformation with System.Text.Json: @@ -440,7 +440,7 @@ options.Events }) ); ``` -snippet source | anchor +snippet source | anchor Add also static import of helper classes to get a concise syntax as above: @@ -450,7 +450,7 @@ Add also static import of helper classes to get a concise syntax as above: ```cs using static Marten.Services.Json.Transformations.SystemTextJson.JsonTransformations; ``` -snippet source | anchor +snippet source | anchor ### Upcasting with classes @@ -478,7 +478,7 @@ public class ShoppingCartOpenedUpcaster: ); } ``` -snippet source | anchor +snippet source | anchor Just like with functions, by default, it takes the event type name based on the old CLR type. You can also define it explicitly. It can be helpful if you changed the event schema more than once, and the old CLR class doesn't represent the initial event type name. You can do that with: @@ -501,7 +501,7 @@ public class ShoppingCartOpenedUpcaster: ); } ``` -snippet source | anchor +snippet source | anchor #### Raw JSON transformation with Json .NET: @@ -524,7 +524,7 @@ public class ShoppingCartOpenedUpcaster: ); } ``` -snippet source | anchor +snippet source | anchor To use it, add the following using: @@ -534,7 +534,7 @@ To use it, add the following using: ```cs using Marten.Services.Json.Transformations.JsonNet; ``` -snippet source | anchor +snippet source | anchor #### Raw JSON transformation with System.Text.Json: @@ -561,7 +561,7 @@ public class ShoppingCartOpenedUpcaster: } } ``` -snippet source | anchor +snippet source | anchor To use it, add the following using: @@ -571,7 +571,7 @@ To use it, add the following using: ```cs using Marten.Services.Json.Transformations.SystemTextJson; ``` -snippet source | anchor +snippet source | anchor #### Registering upcaster class @@ -581,7 +581,7 @@ using Marten.Services.Json.Transformations.SystemTextJson; ```cs options.Events.Upcast(); ``` -snippet source | anchor +snippet source | anchor ### Async Only Upcasters @@ -605,7 +605,7 @@ public interface IClientRepository Task GetClientName(Guid clientId, CancellationToken ct); } ``` -snippet source | anchor +snippet source | anchor You can use it in all the ways presented above. @@ -633,7 +633,7 @@ options.Events } ); ``` -snippet source | anchor +snippet source | anchor #### Function with CLR types and explicit event type name @@ -660,7 +660,7 @@ options.Events } ); ``` -snippet source | anchor +snippet source | anchor #### Function with raw JSON transformation with Json .NET: @@ -691,7 +691,7 @@ options.Events ) ); ``` -snippet source | anchor +snippet source | anchor Add also static import of helper classes to get a concise syntax as above: @@ -701,7 +701,7 @@ Add also static import of helper classes to get a concise syntax as above: ```cs using static Marten.Services.Json.Transformations.JsonNet.JsonTransformations; ``` -snippet source | anchor +snippet source | anchor #### Function with raw JSON transformation with System.Text.Json: @@ -734,7 +734,7 @@ options.Events }) ); ``` -snippet source | anchor +snippet source | anchor Add also static import of helper classes to get a concise syntax as above: @@ -744,7 +744,7 @@ Add also static import of helper classes to get a concise syntax as above: ```cs using static Marten.Services.Json.Transformations.SystemTextJson.JsonTransformations; ``` -snippet source | anchor +snippet source | anchor #### Class with CLR types @@ -779,7 +779,7 @@ public class ShoppingCartOpenedAsyncOnlyUpcaster: } } ``` -snippet source | anchor +snippet source | anchor #### Class with CLR types and explicit event type name @@ -818,7 +818,7 @@ public class ShoppingCartOpenedAsyncOnlyUpcaster: } } ``` -snippet source | anchor +snippet source | anchor #### Class with raw JSON transformation with Json .NET: @@ -856,7 +856,7 @@ public class ShoppingCartOpenedAsyncOnlyUpcaster: } } ``` -snippet source | anchor +snippet source | anchor To use it, add the following using: @@ -866,7 +866,7 @@ To use it, add the following using: ```cs using Marten.Services.Json.Transformations.JsonNet; ``` -snippet source | anchor +snippet source | anchor #### Class with raw JSON transformation with System.Text.Json: @@ -906,7 +906,7 @@ public class ShoppingCartOpenedAsyncOnlyUpcaster: } } ``` -snippet source | anchor +snippet source | anchor To use it, add the following using: @@ -916,7 +916,7 @@ To use it, add the following using: ```cs using Marten.Services.Json.Transformations.SystemTextJson; ``` -snippet source | anchor +snippet source | anchor #### Registering Upcaster class @@ -926,7 +926,7 @@ using Marten.Services.Json.Transformations.SystemTextJson; ```cs options.Events.Upcast(new ShoppingCartOpenedAsyncOnlyUpcaster(clientRepository)); ``` -snippet source | anchor +snippet source | anchor ## Working with multiple Event type versions diff --git a/src/DocumentDbTests/Writing/bulk_loading.cs b/src/DocumentDbTests/Writing/bulk_loading.cs index ade74ea262..dcc69bec78 100644 --- a/src/DocumentDbTests/Writing/bulk_loading.cs +++ b/src/DocumentDbTests/Writing/bulk_loading.cs @@ -351,6 +351,12 @@ internal async Task BulkInsertModeSamples() await store.BulkInsertDocumentsAsync(data, BulkInsertMode.OverwriteIfVersionMatches); #endregion + + #region sample_bulk_insert_with_version_matches + + await store.BulkInsertDocumentsAsync(data, BulkInsertMode.OverwriteIfVersionMatches); + + #endregion } internal async Task MultiTenancySample()