Rebuilding Projections
Projections can be completely rebuilt with the async daemon subsystem. Both inline and asynchronous projections can be rebuilt with the async daemon.
public class DistanceProjection: EventProjection
{
public DistanceProjection()
{
ProjectionName = "Distance";
}
// Create a new Distance document based on a Travel event
public Distance Create(Travel travel, IEvent e)
{
return new Distance {Id = e.Id, Day = travel.Day, Total = travel.TotalDistance()};
}
}
StoreOptions(x => x.Projections.Add(new DistanceProjection(), ProjectionLifecycle.Async));
var agent = await StartDaemon();
// setup test data
await PublishSingleThreaded();
// rebuild projection `Distance`
await agent.RebuildProjectionAsync("Distance", CancellationToken.None);
Optimized Projection Rebuilds 7.30
TIP
This optimization will be turned on by default in Marten 8, but we didn't want to force anyone using Marten 7 to have to upgrade their database without the explicit opt in configuration.
Marten can optimize the projection rebuilds of single stream projections by opting into this flag in your configuration:
builder.Services.AddMarten(opts =>
{
opts.Connection("some connection string");
// Opts into a mode where Marten is able to rebuild single
// stream projections faster by building one stream at a time
// Does require new table migrations for Marten 7 users though
opts.Events.UseOptimizedProjectionRebuilds = true;
});
In this mode, Marten will rebuild single stream projection documents stream by stream in the reverse order that the streams were last modified. This was conceived of as being combined with the FetchForWriting()
usage with asynchronous single stream projections for zero downtime deployments while trying to create less load on the database than the original "left fold" / "from zero" rebuild would be.
Rebuilding a Single Stream 7.28
A long standing request has been to be able to rebuild only a single stream or subset of streams by stream id (or string key). Marten now has a (admittedly crude) ability to do so with this syntax on IDocumentStore
:
await theStore.Advanced.RebuildSingleStreamAsync<SimpleAggregate>(streamId);