Browse Source

A few minor performance improvements.

pull/492/head
Sebastian 7 years ago
parent
commit
91aa1f737a
  1. 6
      backend/src/Squidex.Domain.Apps.Entities/DomainObjectState.cs
  2. 19
      backend/src/Squidex.Infrastructure.MongoDb/EventSourcing/StreamPosition.cs
  3. 13
      backend/src/Squidex.Infrastructure.MongoDb/MongoDb/BsonHelper.cs
  4. 4
      backend/src/Squidex.Infrastructure/Json/Newtonsoft/NamedStringIdConverter.cs
  5. 4
      backend/src/Squidex.Infrastructure/Language.cs
  6. 8
      backend/src/Squidex.Infrastructure/NamedId{T}.cs

6
backend/src/Squidex.Domain.Apps.Entities/DomainObjectState.cs

@ -69,13 +69,15 @@ namespace Squidex.Domain.Apps.Entities
clone.Id = headers.AggregateId(); clone.Id = headers.AggregateId();
} }
var timestamp = headers.Timestamp();
if (clone.CreatedBy == null) if (clone.CreatedBy == null)
{ {
clone.Created = headers.Timestamp(); clone.Created = timestamp;
clone.CreatedBy = payload.Actor; clone.CreatedBy = payload.Actor;
} }
clone.LastModified = headers.Timestamp(); clone.LastModified = timestamp;
clone.LastModifiedBy = payload.Actor; clone.LastModifiedBy = payload.Actor;
return (clone as T)!; return (clone as T)!;

19
backend/src/Squidex.Infrastructure.MongoDb/EventSourcing/StreamPosition.cs

@ -5,6 +5,7 @@
// All rights reserved. Licensed under the MIT license. // All rights reserved. Licensed under the MIT license.
// ========================================================================== // ==========================================================================
using System.Text;
using MongoDB.Bson; using MongoDB.Bson;
namespace Squidex.Infrastructure.EventSourcing namespace Squidex.Infrastructure.EventSourcing
@ -34,15 +35,17 @@ namespace Squidex.Infrastructure.EventSourcing
public static implicit operator string(StreamPosition position) public static implicit operator string(StreamPosition position)
{ {
var parts = new object[] var sb = new StringBuilder();
{
position.Timestamp.Timestamp, sb.Append(position.Timestamp.Timestamp);
position.Timestamp.Increment, sb.Append(",");
position.CommitOffset, sb.Append(position.Timestamp.Increment);
position.CommitSize sb.Append(",");
}; sb.Append(position.CommitOffset);
sb.Append(",");
sb.Append(position.CommitSize);
return string.Join("-", parts); return sb.ToString();
} }
public static implicit operator StreamPosition(string? position) public static implicit operator StreamPosition(string? position)

13
backend/src/Squidex.Infrastructure.MongoDb/MongoDb/BsonHelper.cs

@ -9,13 +9,26 @@ namespace Squidex.Infrastructure.MongoDb
{ {
public static class BsonHelper public static class BsonHelper
{ {
private const string TypeBson = "§type";
private const string TypeJson = "$json";
public static string UnescapeBson(this string value) public static string UnescapeBson(this string value)
{ {
if (value == TypeBson)
{
return TypeJson;
}
return ReplaceFirstCharacter(value, '§', '$'); return ReplaceFirstCharacter(value, '§', '$');
} }
public static string EscapeJson(this string value) public static string EscapeJson(this string value)
{ {
if (value == TypeJson)
{
return TypeBson;
}
return ReplaceFirstCharacter(value, '$', '§'); return ReplaceFirstCharacter(value, '$', '§');
} }

4
backend/src/Squidex.Infrastructure/Json/Newtonsoft/NamedStringIdConverter.cs

@ -29,9 +29,9 @@ namespace Squidex.Infrastructure.Json.Newtonsoft
return result; return result;
} }
private static bool ParseString(string value, out string result) private static bool ParseString(ReadOnlySpan<char> value, out string result)
{ {
result = value; result = new string(value);
return true; return true;
} }

4
backend/src/Squidex.Infrastructure/Language.cs

@ -56,14 +56,14 @@ namespace Squidex.Infrastructure
public static bool IsValidLanguage(string iso2Code) public static bool IsValidLanguage(string iso2Code)
{ {
Guard.NotNullOrEmpty(iso2Code); Guard.NotNull(iso2Code);
return AllLanguagesField.ContainsKey(iso2Code); return AllLanguagesField.ContainsKey(iso2Code);
} }
public static bool TryGetLanguage(string iso2Code, [MaybeNullWhen(false)] out Language language) public static bool TryGetLanguage(string iso2Code, [MaybeNullWhen(false)] out Language language)
{ {
Guard.NotNullOrEmpty(iso2Code); Guard.NotNull(iso2Code);
return AllLanguagesField.TryGetValue(iso2Code, out language!); return AllLanguagesField.TryGetValue(iso2Code, out language!);
} }

8
backend/src/Squidex.Infrastructure/NamedId{T}.cs

@ -12,7 +12,7 @@ using System.Diagnostics.CodeAnalysis;
namespace Squidex.Infrastructure namespace Squidex.Infrastructure
{ {
public delegate bool Parser<T>(string input, out T result); public delegate bool Parser<T>(ReadOnlySpan<char> input, out T result);
[Equals(DoNotAddEqualityOperators = true)] [Equals(DoNotAddEqualityOperators = true)]
public sealed class NamedId<T> where T : notnull public sealed class NamedId<T> where T : notnull
@ -42,11 +42,13 @@ namespace Squidex.Infrastructure
{ {
if (value != null) if (value != null)
{ {
var span = value.AsSpan();
if (typeof(T) == typeof(Guid)) if (typeof(T) == typeof(Guid))
{ {
if (value.Length > GuidLength + 1 && value[GuidLength] == ',') if (value.Length > GuidLength + 1 && value[GuidLength] == ',')
{ {
if (parser(value.Substring(0, GuidLength), out var id)) if (parser(span.Slice(0, GuidLength), out var id))
{ {
result = new NamedId<T>(id, value.Substring(GuidLength + 1)); result = new NamedId<T>(id, value.Substring(GuidLength + 1));
@ -60,7 +62,7 @@ namespace Squidex.Infrastructure
if (index > 0 && index < value.Length - 1) if (index > 0 && index < value.Length - 1)
{ {
if (parser(value.Substring(0, index), out var id)) if (parser(span.Slice(0, index), out var id))
{ {
result = new NamedId<T>(id, value.Substring(index + 1)); result = new NamedId<T>(id, value.Substring(index + 1));

Loading…
Cancel
Save