Skip to content

Commit

Permalink
Faster migration.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Mar 7, 2019
1 parent b9f78ec commit 9fc698f
Show file tree
Hide file tree
Showing 49 changed files with 291 additions and 241 deletions.
2 changes: 2 additions & 0 deletions src/Squidex.Domain.Apps.Core.Model/SquidexCoreModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

using System.Reflection;

#pragma warning disable RECS0014 // If all fields, properties and methods members are static, the class can be made static.

namespace Squidex.Domain.Apps.Core
{
public sealed class SquidexCoreModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

using System.Reflection;

#pragma warning disable RECS0014 // If all fields, properties and methods members are static, the class can be made static.

namespace Squidex.Domain.Apps.Core
{
public static class SquidexCoreOperations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// ==========================================================================

using System;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver;
using Squidex.Domain.Apps.Entities.Assets.State;
Expand All @@ -18,7 +19,7 @@ namespace Squidex.Domain.Apps.Entities.MongoDb.Assets
{
public sealed partial class MongoAssetRepository : ISnapshotStore<AssetState, Guid>
{
public async Task<(AssetState Value, long Version)> ReadAsync(Guid key)
async Task<(AssetState Value, long Version)> ISnapshotStore<AssetState, Guid>.ReadAsync(Guid key)
{
using (Profiler.TraceMethod<MongoAssetRepository>())
{
Expand All @@ -35,7 +36,7 @@ await Collection.Find(x => x.Id == key)
}
}

public async Task WriteAsync(Guid key, AssetState value, long oldVersion, long newVersion)
async Task ISnapshotStore<AssetState, Guid>.WriteAsync(Guid key, AssetState value, long oldVersion, long newVersion)
{
using (Profiler.TraceMethod<MongoAssetRepository>())
{
Expand All @@ -48,7 +49,7 @@ public async Task WriteAsync(Guid key, AssetState value, long oldVersion, long n
}
}

Task ISnapshotStore<AssetState, Guid>.ReadAllAsync(Func<AssetState, long, Task> callback)
Task ISnapshotStore<AssetState, Guid>.ReadAllAsync(Func<AssetState, long, Task> callback, CancellationToken ct)
{
throw new NotSupportedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using NodaTime;
using Squidex.Domain.Apps.Core.Contents;
Expand Down Expand Up @@ -170,6 +171,18 @@ await Collection.Find(x => x.Id == key)
return (null, EtagVersion.NotFound);
}

public Task ReadAllAsync(Func<ContentState, long, Task> callback, Func<Guid, Guid, Task<ISchemaEntity>> getSchema, CancellationToken ct = default)
{
return Collection.Find(new BsonDocument()).ForEachPipelineAsync(async contentEntity =>
{
var schema = await getSchema(contentEntity.IndexedAppId, contentEntity.IndexedSchemaId);

contentEntity.ParseData(schema.SchemaDef, Serializer);

await callback(SimpleMapper.Map(contentEntity, new ContentState()), contentEntity.Version);
}, ct);
}

public Task CleanupAsync(Guid id)
{
return Collection.UpdateManyAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// ==========================================================================

using System;
using System.Threading;
using System.Threading.Tasks;
using Squidex.Domain.Apps.Entities.Contents.State;
using Squidex.Domain.Apps.Entities.Schemas;
Expand All @@ -18,15 +19,31 @@ namespace Squidex.Domain.Apps.Entities.MongoDb.Contents
{
public partial class MongoContentRepository : ISnapshotStore<ContentState, Guid>
{
public async Task<(ContentState Value, long Version)> ReadAsync(Guid key)
async Task ISnapshotStore<ContentState, Guid>.RemoveAsync(Guid key)
{
using (Profiler.TraceMethod<MongoContentRepository>())
{
await contents.RemoveAsync(key);
}
}

async Task ISnapshotStore<ContentState, Guid>.ReadAllAsync(Func<ContentState, long, Task> callback, CancellationToken ct)
{
using (Profiler.TraceMethod<MongoContentRepository>())
{
await contents.ReadAllAsync(callback, GetSchemaAsync, ct);
}
}

async Task<(ContentState Value, long Version)> ISnapshotStore<ContentState, Guid>.ReadAsync(Guid key)
{
using (Profiler.TraceMethod<MongoContentRepository>())
{
return await contents.ReadAsync(key, GetSchemaAsync);
}
}

public async Task WriteAsync(Guid key, ContentState value, long oldVersion, long newVersion)
async Task ISnapshotStore<ContentState, Guid>.WriteAsync(Guid key, ContentState value, long oldVersion, long newVersion)
{
using (Profiler.TraceMethod<MongoContentRepository>())
{
Expand Down Expand Up @@ -81,15 +98,5 @@ private async Task<ISchemaEntity> GetSchemaAsync(Guid appId, Guid schemaId)

return schema;
}

Task ISnapshotStore<ContentState, Guid>.RemoveAsync(Guid key)
{
throw new NotSupportedException();
}

Task ISnapshotStore<ContentState, Guid>.ReadAllAsync(Func<ContentState, long, Task> callback)
{
throw new NotSupportedException();
}
}
}
5 changes: 0 additions & 5 deletions src/Squidex.Domain.Apps.Entities/Apps/AppGrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,6 @@ private static AppContributorAssigned CreateInitialOwner(RefToken actor)
return new AppContributorAssigned { ContributorId = actor.Identifier, Role = Role.Owner };
}

protected override AppState OnEvent(Envelope<IEvent> @event)
{
return Snapshot.Apply(@event);
}

public Task<J<IAppEntity>> GetStateAsync()
{
return J.AsTask<IAppEntity>(Snapshot);
Expand Down
2 changes: 1 addition & 1 deletion src/Squidex.Domain.Apps.Entities/Apps/State/AppState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ protected void On(AppArchived @event)
IsArchived = true;
}

public AppState Apply(Envelope<IEvent> @event)
public override AppState Apply(Envelope<IEvent> @event)
{
var payload = (SquidexEvent)@event.Payload;

Expand Down
5 changes: 0 additions & 5 deletions src/Squidex.Domain.Apps.Entities/Assets/AssetGrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,6 @@ private void VerifyNotDeleted()
}
}

protected override AssetState OnEvent(Envelope<IEvent> @event)
{
return Snapshot.Apply(@event);
}

public Task<J<IAssetEntity>> GetStateAsync(long version = EtagVersion.Any)
{
return J.AsTask<IAssetEntity>(GetSnapshot(version));
Expand Down
4 changes: 2 additions & 2 deletions src/Squidex.Domain.Apps.Entities/Assets/BackupAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public override async Task RestoreAsync(Guid appId, BackupReader reader)
{
await RestoreTagsAsync(appId, reader);

await RebuildManyAsync(assetIds, id => RebuildAsync<AssetState, AssetGrain>(id, (e, s) => s.Apply(e)));
await RebuildManyAsync(assetIds, id => RebuildAsync<AssetState, AssetGrain>(id));
}

private async Task RestoreTagsAsync(Guid appId, BackupReader reader)
Expand Down Expand Up @@ -110,7 +110,7 @@ private Task ReadAssetAsync(Guid assetId, long fileVersion, BackupReader reader)
{
try
{
await assetStore.UploadAsync(assetId.ToString(), fileVersion, null, stream);
await assetStore.UploadAsync(assetId.ToString(), fileVersion, null, stream, true);
}
catch (AssetAlreadyExistsException)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected void On(AssetDeleted @event)
IsDeleted = true;
}

public AssetState Apply(Envelope<IEvent> @event)
public override AssetState Apply(Envelope<IEvent> @event)
{
var payload = (SquidexEvent)@event.Payload;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System.Threading.Tasks;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Commands;
using Squidex.Infrastructure.EventSourcing;
using Squidex.Infrastructure.States;

namespace Squidex.Domain.Apps.Entities.Backup
Expand Down Expand Up @@ -39,7 +38,7 @@ protected async Task RebuildManyAsync(IEnumerable<Guid> ids, Func<Guid, Task> ac
}
}

protected async Task RebuildAsync<TState, TGrain>(Guid key, Func<Envelope<IEvent>, TState, TState> func) where TState : IDomainState, new()
protected async Task RebuildAsync<TState, TGrain>(Guid key) where TState : IDomainState<TState>, new()
{
var state = new TState
{
Expand All @@ -48,7 +47,7 @@ protected async Task RebuildManyAsync(IEnumerable<Guid> ids, Func<Guid, Task> ac

var persistence = store.WithSnapshotsAndEventSourcing(typeof(TGrain), key, (TState s) => state = s, e =>
{
state = func(e, state);
state = state.Apply(e);

state.Version++;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

using Squidex.Infrastructure.EventSourcing;

namespace Squidex.Domain.Apps.Entities.Comments.State
{
public sealed class CommentsState : DomainObjectState<CommentsState>
{
public override CommentsState Apply(Envelope<IEvent> @event)
{
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public override Task RestoreAsync(Guid appId, BackupReader reader)
{
var contentIds = contentIdsBySchemaId.Values.SelectMany(x => x);

return RebuildManyAsync(contentIds, id => RebuildAsync<ContentState, ContentGrain>(id, (e, s) => s.Apply(e)));
return RebuildManyAsync(contentIds, id => RebuildAsync<ContentState, ContentGrain>(id));
}
}
}
5 changes: 0 additions & 5 deletions src/Squidex.Domain.Apps.Entities/Contents/ContentGrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,6 @@ private void VerifyNotDeleted()
}
}

protected override ContentState OnEvent(Envelope<IEvent> @event)
{
return Snapshot.Apply(@event);
}

private async Task<ContentOperationContext> CreateContext(Guid appId, Guid schemaId, Guid contentId, Func<string> message)
{
var operationContext =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected void On(ContentDeleted @event)
IsDeleted = true;
}

public ContentState Apply(Envelope<IEvent> @event)
public override ContentState Apply(Envelope<IEvent> @event)
{
var payload = (SquidexEvent)@event.Payload;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Orleans;
using Squidex.Domain.Apps.Core.Contents;
using Squidex.Domain.Apps.Entities.Apps;
using Squidex.Domain.Apps.Entities.Schemas;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Log;

Expand Down
5 changes: 4 additions & 1 deletion src/Squidex.Domain.Apps.Entities/DomainObjectState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
using NodaTime;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Commands;
using Squidex.Infrastructure.EventSourcing;

namespace Squidex.Domain.Apps.Entities
{
public abstract class DomainObjectState<T> : Cloneable<T>,
IDomainState,
IDomainState<T>,
IEntity,
IEntityWithCreatedBy,
IEntityWithLastModifiedBy,
Expand Down Expand Up @@ -42,6 +43,8 @@ public abstract class DomainObjectState<T> : Cloneable<T>,
[DataMember]
public long Version { get; set; } = EtagVersion.Empty;

public abstract T Apply(Envelope<IEvent> @event);

public T Clone()
{
return Clone(x => { });
Expand Down
5 changes: 0 additions & 5 deletions src/Squidex.Domain.Apps.Entities/Rules/RuleGrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,6 @@ private void VerifyNotDeleted()
}
}

protected override RuleState OnEvent(Envelope<IEvent> @event)
{
return Snapshot.Apply(@event);
}

public Task<J<IRuleEntity>> GetStateAsync()
{
return J.AsTask<IRuleEntity>(Snapshot);
Expand Down
2 changes: 1 addition & 1 deletion src/Squidex.Domain.Apps.Entities/Rules/State/RuleState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected void On(RuleDeleted @event)
IsDeleted = true;
}

public RuleState Apply(Envelope<IEvent> @event)
public override RuleState Apply(Envelope<IEvent> @event)
{
var payload = (SquidexEvent)@event.Payload;

Expand Down
5 changes: 0 additions & 5 deletions src/Squidex.Domain.Apps.Entities/Schemas/SchemaGrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,6 @@ private void VerifyNotDeleted()
}
}

protected override SchemaState OnEvent(Envelope<IEvent> @event)
{
return Snapshot.Apply(@event);
}

public Task<J<ISchemaEntity>> GetStateAsync()
{
return J.AsTask<ISchemaEntity>(Snapshot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ protected void On(SchemaDeleted @event)
IsDeleted = true;
}

public SchemaState Apply(Envelope<IEvent> @event)
public override SchemaState Apply(Envelope<IEvent> @event)
{
var payload = (SquidexEvent)@event.Payload;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Squidex.Domain.Apps.Entities
{
public abstract class SquidexDomainObjectGrain<T> : DomainObjectGrain<T> where T : IDomainState, new()
public abstract class SquidexDomainObjectGrain<T> : DomainObjectGrain<T> where T : IDomainState<T>, new()
{
protected SquidexDomainObjectGrain(IStore<Guid> store, ISemanticLog log)
: base(store, log)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Squidex.Domain.Apps.Entities
{
public abstract class SquidexDomainObjectGrainLogSnapshots<T> : LogSnapshotDomainObjectGrain<T> where T : IDomainState, new()
public abstract class SquidexDomainObjectGrainLogSnapshots<T> : LogSnapshotDomainObjectGrain<T> where T : IDomainState<T>, new()
{
protected SquidexDomainObjectGrainLogSnapshots(IStore<Guid> store, ISemanticLog log)
: base(store, log)
Expand Down
2 changes: 2 additions & 0 deletions src/Squidex.Domain.Apps.Entities/SquidexEntities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

using System.Reflection;

#pragma warning disable RECS0014 // If all fields, properties and methods members are static, the class can be made static.

namespace Squidex.Domain.Apps.Entities
{
public static class SquidexEntities
Expand Down
2 changes: 2 additions & 0 deletions src/Squidex.Domain.Apps.Events/SquidexEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

using System.Reflection;

#pragma warning disable RECS0014 // If all fields, properties and methods members are static, the class can be made static.

namespace Squidex.Domain.Apps.Events
{
public sealed class SquidexEvents
Expand Down
10 changes: 10 additions & 0 deletions src/Squidex.Infrastructure.MongoDb/MongoDb/MongoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public static class MongoExtensions
{
private static readonly UpdateOptions Upsert = new UpdateOptions { IsUpsert = true };

public static async Task<bool> CollectionExistsAsync(this IMongoDatabase database, string collectionName)
{
var options = new ListCollectionNamesOptions
{
Filter = new BsonDocument("name", collectionName)
};

return (await database.ListCollectionNamesAsync(options)).Any();
}

public static async Task<bool> InsertOneIfNotExistsAsync<T>(this IMongoCollection<T> collection, T document)
{
try
Expand Down
Loading

0 comments on commit 9fc698f

Please sign in to comment.