Browse Source

Temporary.

pull/169/head
Sebastian Stehle 8 years ago
parent
commit
a0ac9ac80b
  1. 31
      src/Squidex.Domain.Apps.Read/State/Orleans/Grains/Implementations/AppStateGrain.cs
  2. 74
      src/Squidex.Domain.Apps.Read/State/Orleans/Grains/Implementations/AppStateGrainState.cs
  3. 4
      src/Squidex.Domain.Apps.Read/State/Orleans/Grains/Implementations/JsonAppEntity.cs
  4. 8
      src/Squidex.Infrastructure/Json/Orleans/JsonExternalSerializer.cs
  5. 73
      src/Squidex.Infrastructure/Json/Orleans/JsonState.cs
  6. 6
      src/Squidex.Infrastructure/Reflection/SimpleCopier.cs

31
src/Squidex.Domain.Apps.Read/State/Orleans/Grains/Implementations/AppStateGrain.cs

@ -9,7 +9,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Orleans;
using Orleans.Providers;
using Squidex.Domain.Apps.Core.Schemas;
@ -23,72 +22,62 @@ using Squidex.Infrastructure.Json.Orleans;
namespace Squidex.Domain.Apps.Read.State.Orleans.Grains.Implementations
{
[StorageProvider(ProviderName = "Default")]
public sealed class AppStateGrain : Grain<JsonState<AppStateGrainState>>, IAppStateGrain
public sealed class AppStateGrain : Grain<AppStateGrainState>, IAppStateGrain
{
private readonly FieldRegistry fieldRegistry;
private readonly JsonSerializer serializer;
public AppStateGrain(FieldRegistry fieldRegistry, JsonSerializer serializer)
public AppStateGrain(FieldRegistry fieldRegistry)
{
Guard.NotNull(fieldRegistry, nameof(fieldRegistry));
Guard.NotNull(serializer, nameof(serializer));
this.fieldRegistry = fieldRegistry;
this.serializer = serializer;
}
public override Task OnActivateAsync()
{
State.SetSerializer(serializer);
return base.OnActivateAsync();
}
public Task<J<(IAppEntity, ISchemaEntity)>> GetAppWithSchemaAsync(Guid id)
{
var schema = State.Value.FindSchema(x => x.Id == id && !x.IsDeleted);
var schema = State.FindSchema(x => x.Id == id && !x.IsDeleted);
return J<(IAppEntity AppEntity, ISchemaEntity SchemaEntity)>.AsTask((State.Value.GetApp(), schema));
return J<(IAppEntity AppEntity, ISchemaEntity SchemaEntity)>.AsTask((State.GetApp(), schema));
}
public Task<J<IAppEntity>> GetAppAsync()
{
var value = State.Value.GetApp();
var value = State.GetApp();
return J<IAppEntity>.AsTask(value);
}
public Task<J<List<IRuleEntity>>> GetRulesAsync()
{
var value = State.Value.FindRules();
var value = State.FindRules();
return J<List<IRuleEntity>>.AsTask(value);
}
public Task<J<List<ISchemaEntity>>> GetSchemasAsync()
{
var value = State.Value.FindSchemas(x => !x.IsDeleted);
var value = State.FindSchemas(x => !x.IsDeleted);
return J<List<ISchemaEntity>>.AsTask(value);
}
public Task<J<ISchemaEntity>> GetSchemaAsync(Guid id, bool provideDeleted = false)
{
var value = State.Value.FindSchema(x => x.Id == id && (!x.IsDeleted || provideDeleted));
var value = State.FindSchema(x => x.Id == id && (!x.IsDeleted || provideDeleted));
return J<ISchemaEntity>.AsTask(value);
}
public Task<J<ISchemaEntity>> GetSchemaAsync(string name, bool provideDeleted = false)
{
var value = State.Value.FindSchema(x => string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase) && (!x.IsDeleted || provideDeleted));
var value = State.FindSchema(x => string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase) && (!x.IsDeleted || provideDeleted));
return J<ISchemaEntity>.AsTask(value);
}
public Task HandleAsync(J<Envelope<IEvent>> message)
{
State = State.Update(v => v.Apply(message, fieldRegistry));
State.Apply(message, fieldRegistry);
return WriteStateAsync();
}

74
src/Squidex.Domain.Apps.Read/State/Orleans/Grains/Implementations/AppStateGrainState.cs

@ -12,6 +12,7 @@ using System.Linq;
using Newtonsoft.Json;
using Squidex.Domain.Apps.Core.Apps;
using Squidex.Domain.Apps.Core.Schemas;
using Squidex.Domain.Apps.Events;
using Squidex.Domain.Apps.Events.Apps;
using Squidex.Domain.Apps.Events.Apps.Utils;
using Squidex.Domain.Apps.Events.Rules;
@ -90,70 +91,70 @@ namespace Squidex.Domain.Apps.Read.State.Orleans.Grains.Implementations
}
case AppPlanChanged @event:
App.Update(@event, envelope.Headers, a =>
UpdateApp(envelope, a =>
{
SimpleMapper.Map(@event, a);
});
break;
case AppClientAttached @event:
App.Update(@event, envelope.Headers, a =>
UpdateApp(envelope, a =>
{
a.Clients.Apply(@event);
});
break;
case AppClientRevoked @event:
App.Update(@event, envelope.Headers, a =>
UpdateApp(envelope, a =>
{
a.Clients.Apply(@event);
});
break;
case AppClientRenamed @event:
App.Update(@event, envelope.Headers, a =>
UpdateApp(envelope, a =>
{
a.Clients.Apply(@event);
});
break;
case AppClientUpdated @event:
App.Update(@event, envelope.Headers, a =>
UpdateApp(envelope, a =>
{
a.Clients.Apply(@event);
});
break;
case AppContributorRemoved @event:
App.Update(@event, envelope.Headers, a =>
UpdateApp(envelope, a =>
{
a.Contributors.Apply(@event);
});
break;
case AppContributorAssigned @event:
App.Update(@event, envelope.Headers, a =>
UpdateApp(envelope, a =>
{
a.Contributors.Apply(@event);
});
break;
case AppLanguageAdded @event:
App.Update(@event, envelope.Headers, a =>
UpdateApp(envelope, a =>
{
a.LanguagesConfig.Apply(@event);
});
break;
case AppLanguageRemoved @event:
App.Update(@event, envelope.Headers, a =>
UpdateApp(envelope, a =>
{
a.LanguagesConfig.Apply(@event);
});
break;
case AppLanguageUpdated @event:
App.Update(@event, envelope.Headers, a =>
UpdateApp(envelope, a =>
{
a.LanguagesConfig.Apply(@event);
});
@ -167,14 +168,14 @@ namespace Squidex.Domain.Apps.Read.State.Orleans.Grains.Implementations
break;
case RuleUpdated @event:
Rules[@event.RuleId].Update(@event, envelope.Headers, r =>
UpdateRule(envelope, r =>
{
r.Rule.Apply(@event);
});
break;
case RuleEnabled @event:
Rules[@event.RuleId].Update(@event, envelope.Headers, r =>
UpdateRule(envelope, r =>
{
r.Rule.Apply(@event);
});
@ -194,84 +195,84 @@ namespace Squidex.Domain.Apps.Read.State.Orleans.Grains.Implementations
break;
case FieldAdded @event:
Schemas[@event.SchemaId.Id].Update(@event, envelope.Headers, s =>
UpdateSchema(envelope, s =>
{
s.SchemaDef.Apply(@event, registry);
});
break;
case FieldDeleted @event:
Schemas[@event.SchemaId.Id].Update(@event, envelope.Headers, s =>
UpdateSchema(envelope, s =>
{
s.SchemaDef.Apply(@event);
});
break;
case FieldLocked @event:
Schemas[@event.SchemaId.Id].Update(@event, envelope.Headers, s =>
UpdateSchema(envelope, s =>
{
s.SchemaDef.Apply(@event);
});
break;
case FieldHidden @event:
Schemas[@event.SchemaId.Id].Update(@event, envelope.Headers, s =>
UpdateSchema(envelope, s =>
{
s.SchemaDef.Apply(@event);
});
break;
case FieldShown @event:
Schemas[@event.SchemaId.Id].Update(@event, envelope.Headers, s =>
UpdateSchema(envelope, s =>
{
s.SchemaDef.Apply(@event);
});
break;
case FieldDisabled @event:
Schemas[@event.SchemaId.Id].Update(@event, envelope.Headers, s =>
UpdateSchema(envelope, s =>
{
s.SchemaDef.Apply(@event);
});
break;
case FieldEnabled @event:
Schemas[@event.SchemaId.Id].Update(@event, envelope.Headers, s =>
UpdateSchema(envelope, s =>
{
s.SchemaDef.Apply(@event);
});
break;
case FieldUpdated @event:
Schemas[@event.SchemaId.Id].Update(@event, envelope.Headers, s =>
UpdateSchema(envelope, s =>
{
s.SchemaDef.Apply(@event);
});
break;
case SchemaFieldsReordered @event:
Schemas[@event.SchemaId.Id].Update(@event, envelope.Headers, s =>
UpdateSchema(envelope, s =>
{
s.SchemaDef.Apply(@event);
});
break;
case SchemaUpdated @event:
Schemas[@event.SchemaId.Id].Update(@event, envelope.Headers, s =>
UpdateSchema(envelope, s =>
{
s.SchemaDef.Apply(@event);
});
break;
case SchemaPublished @event:
Schemas[@event.SchemaId.Id].Update(@event, envelope.Headers, s =>
UpdateSchema(envelope, s =>
{
s.SchemaDef.Apply(@event);
});
break;
case ScriptsConfigured @event:
Schemas[@event.SchemaId.Id].Update(@event, envelope.Headers, s =>
UpdateSchema(envelope, s =>
{
s.SchemaDef.Apply(@event);
});
@ -282,11 +283,11 @@ namespace Squidex.Domain.Apps.Read.State.Orleans.Grains.Implementations
break;
case WebhookAdded @event:
Schemas[@event.SchemaId.Id].Update(@event, envelope.Headers);
UpdateSchema(envelope);
break;
case WebhookDeleted @event:
Schemas[@event.SchemaId.Id].Update(@event, envelope.Headers);
UpdateSchema(envelope);
break;
}
@ -295,5 +296,26 @@ namespace Squidex.Domain.Apps.Read.State.Orleans.Grains.Implementations
App.Etag = Guid.NewGuid().ToString();
}
}
private void UpdateApp(Envelope<IEvent> envelope, Action<JsonAppEntity> updater = null)
{
var e = envelope.To<AppEvent>();
App.Update(e.Payload, e.Headers, updater);
}
private void UpdateRule(Envelope<IEvent> envelope, Action<JsonRuleEntity> updater = null)
{
var e = envelope.To<RuleEvent>();
Rules[e.Payload.RuleId].Update(e.Payload, e.Headers, updater);
}
private void UpdateSchema(Envelope<IEvent> envelope, Action<JsonSchemaEntity> updater = null)
{
var e = envelope.To<SchemaEvent>();
Schemas[e.Payload.SchemaId.Id].Copy().Update(e.Payload, e.Headers, updater);
}
}
}

4
src/Squidex.Domain.Apps.Read/State/Orleans/Grains/Implementations/JsonAppEntity.cs

@ -6,9 +6,13 @@
// All rights reserved.
// ==========================================================================
using System;
using Newtonsoft.Json;
using Squidex.Domain.Apps.Core.Apps;
using Squidex.Domain.Apps.Events;
using Squidex.Domain.Apps.Read.Apps;
using Squidex.Infrastructure.CQRS.Events;
using Squidex.Infrastructure.Reflection;
namespace Squidex.Domain.Apps.Read.State.Orleans.Grains.Implementations
{

8
src/Squidex.Infrastructure/Json/Orleans/JsonExternalSerializer.cs

@ -9,8 +9,8 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Orleans.Runtime;
using Orleans.Serialization;
@ -38,7 +38,7 @@ namespace Squidex.Infrastructure.Json.Orleans
public object DeepCopy(object source, ICopyContext context)
{
return source;
return source != null ? JObject.FromObject(source, jsonSerializer).ToObject(source.GetType(), jsonSerializer) : null;
}
public object Deserialize(Type expectedType, IDeserializationContext context)
@ -46,8 +46,6 @@ namespace Squidex.Infrastructure.Json.Orleans
var outLength = context.StreamReader.ReadInt();
var outBytes = context.StreamReader.ReadBytes(outLength);
var json = Encoding.Default.GetString(outBytes);
var stream = new MemoryStream(outBytes);
using (var reader = new JsonTextReader(new StreamReader(stream)))
@ -69,8 +67,6 @@ namespace Squidex.Infrastructure.Json.Orleans
var outBytes = stream.ToArray();
var json = Encoding.Default.GetString(outBytes, 0, outBytes.Length);
context.StreamWriter.Write(outBytes.Length);
context.StreamWriter.Write(outBytes);
}

73
src/Squidex.Infrastructure/Json/Orleans/JsonState.cs

@ -1,73 +0,0 @@
// ==========================================================================
// JsonState.cs
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex Group
// All rights reserved.
// ==========================================================================
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Squidex.Infrastructure.Json.Orleans
{
public sealed class JsonState<TValue> where TValue : class, new()
{
private JsonSerializer serializer = JsonSerializer.CreateDefault();
private TValue value;
[JsonProperty]
public JToken Json { get; set; }
[JsonIgnore]
public TValue Value
{
get
{
if (value == null)
{
value = ReadState();
}
return value;
}
set
{
this.value = value;
}
}
public void SetSerializer(JsonSerializer serializer)
{
if (serializer != null)
{
this.serializer = serializer;
}
}
public JsonState<TValue> Update(Action<TValue> updater)
{
var value = ReadState();
updater(value);
var json = JObject.FromObject(value, serializer);
return new JsonState<TValue> { serializer = serializer, Json = json, Value = value };
}
private TValue ReadState()
{
if (Json != null)
{
return Json.ToObject<TValue>(serializer);
}
else
{
return new TValue();
}
}
}
}

6
src/Squidex.Infrastructure/Reflection/SimpleCopier.cs

@ -62,7 +62,7 @@ namespace Squidex.Infrastructure.Reflection
}
}
public static T Copy(T source)
public static T CopyThis(T source)
{
var destination = new T();
@ -75,11 +75,11 @@ namespace Squidex.Infrastructure.Reflection
}
}
public static T Clone<T>(T source) where T : class, new()
public static T Copy<T>(this T source) where T : class, new()
{
Guard.NotNull(source, nameof(source));
return ClassCopier<T>.Copy(source);
return ClassCopier<T>.CopyThis(source);
}
}
}

Loading…
Cancel
Save