Skip to content

Commit

Permalink
Fix for indexing.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Apr 16, 2019
1 parent 8e5cf8b commit 842c38e
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 37 deletions.
15 changes: 5 additions & 10 deletions src/Squidex.Domain.Apps.Entities/Contents/Text/GrainTextIndexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ public async Task On(Envelope<IEvent> @event)
{
var index = grainFactory.GetGrain<ITextIndexerGrain>(contentEvent.SchemaId.Id);

if (index == null)
{
throw new InvalidOperationException("Cannot create reference to grain.");
}

var id = contentEvent.ContentId;

switch (@event.Payload)
Expand All @@ -70,13 +65,13 @@ public async Task On(Envelope<IEvent> @event)
await index.DeleteAsync(id);
break;
case ContentCreated contentCreated:
await index.IndexAsync(id, Data(contentCreated.Data), true);
await index.IndexAsync(Data(id, contentCreated.Data, true));
break;
case ContentUpdateProposed contentUpdateProposed:
await index.IndexAsync(id, Data(contentUpdateProposed.Data), true);
await index.IndexAsync(Data(id, contentUpdateProposed.Data, true));
break;
case ContentUpdated contentUpdated:
await index.IndexAsync(id, Data(contentUpdated.Data), false);
await index.IndexAsync(Data(id, contentUpdated.Data, false));
break;
case ContentChangesDiscarded _:
await index.CopyAsync(id, false);
Expand All @@ -89,9 +84,9 @@ public async Task On(Envelope<IEvent> @event)
}
}

private static J<IndexData> Data(NamedContentData data)
private static J<Update> Data(Guid contentId, NamedContentData data, bool onlySelf)
{
return new IndexData { Data = data };
return new Update { Id = contentId, Data = data, OnlyDraft = onlySelf };
}

public async Task<List<Guid>> SearchAsync(string queryText, IAppEntity app, Guid schemaId, Scope scope = Scope.Published)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text
{
public interface ITextIndexerGrain : IGrainWithGuidKey
{
Task DeleteAsync(Guid id);
Task<bool> DeleteAsync(Guid id);

Task IndexAsync(Guid id, J<IndexData> data, bool onlyDraft);
Task<bool> CopyAsync(Guid id, bool fromDraft);

Task CopyAsync(Guid id, bool fromDraft);
Task<bool> IndexAsync(J<Update> update);

Task<List<Guid>> SearchAsync(string queryText, SearchContext context);
}
Expand Down
27 changes: 18 additions & 9 deletions src/Squidex.Domain.Apps.Entities/Contents/Text/TextIndexerGrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,29 +81,34 @@ protected override async Task OnActivateAsync(Guid key)
}
}

public Task DeleteAsync(Guid id)
public Task<bool> IndexAsync(J<Update> update)
{
var content = new TextIndexContent(indexWriter, indexState, id);
return IndexInternalAsync(update);
}

content.Delete();
private Task<bool> IndexInternalAsync(Update update)
{
var content = new TextIndexContent(indexWriter, indexState, update.Id);

content.Index(update.Data, update.OnlyDraft);

return TryFlushAsync();
}

public Task IndexAsync(Guid id, J<IndexData> data, bool onlyDraft)
public Task<bool> CopyAsync(Guid id, bool fromDraft)
{
var content = new TextIndexContent(indexWriter, indexState, id);

content.Index(data.Value.Data, onlyDraft);
content.Copy(fromDraft);

return TryFlushAsync();
}

public Task CopyAsync(Guid id, bool fromDraft)
public Task<bool> DeleteAsync(Guid id)
{
var content = new TextIndexContent(indexWriter, indexState, id);

content.Copy(fromDraft);
content.Delete();

return TryFlushAsync();
}
Expand Down Expand Up @@ -164,7 +169,7 @@ private Query BuildQuery(string query, SearchContext context)
}
}

private async Task TryFlushAsync()
private async Task<bool> TryFlushAsync()
{
timer?.Dispose();

Expand All @@ -173,6 +178,8 @@ private async Task TryFlushAsync()
if (updates >= MaxUpdates)
{
await FlushAsync();

return true;
}
else
{
Expand All @@ -184,9 +191,11 @@ private async Task TryFlushAsync()
}
catch (InvalidOperationException)
{
return;
return false;
}
}

return false;
}

public async Task FlushAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

using System;
using Squidex.Domain.Apps.Core.Contents;

namespace Squidex.Domain.Apps.Entities.Contents.Text
{
public sealed class IndexData
public sealed class Update
{
public Guid Id { get; set; }

public NamedContentData Data { get; set; }

public bool OnlyDraft { get; set; }
}
}
28 changes: 23 additions & 5 deletions src/Squidex/Config/Domain/SerializationInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,48 @@
using Squidex.Areas.Api.Controllers.Rules.Models;
using Squidex.Domain.Apps.Core.HandleRules;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Json;
using Squidex.Infrastructure.MongoDb;
using Squidex.Infrastructure.Orleans;
using Squidex.Infrastructure.Tasks;

namespace Squidex.Config.Domain
{
public sealed class SerializationInitializer : IInitializable
{
private readonly JsonSerializer jsonSerializer;
private readonly JsonSerializer jsonNetSerializer;
private readonly IJsonSerializer jsonSerializer;
private readonly RuleRegistry ruleRegistry;

public SerializationInitializer(JsonSerializer jsonSerializer, RuleRegistry ruleRegistry)
public SerializationInitializer(JsonSerializer jsonNetSerializer, IJsonSerializer jsonSerializer, RuleRegistry ruleRegistry)
{
this.jsonNetSerializer = jsonNetSerializer;
this.jsonSerializer = jsonSerializer;

this.ruleRegistry = ruleRegistry;
}

public Task InitializeAsync(CancellationToken ct = default)
{
BsonJsonConvention.Register(jsonSerializer);
SetupBson();
SetupOrleans();
SetupActions();

return TaskHelper.Done;
}

private void SetupActions()
{
RuleActionConverter.Mapping = ruleRegistry.Actions.ToDictionary(x => x.Key, x => x.Value.Type);
}

return TaskHelper.Done;
private void SetupBson()
{
BsonJsonConvention.Register(jsonNetSerializer);
}

private void SetupOrleans()
{
J.DefaultSerializer = jsonSerializer;
}
}
}
4 changes: 2 additions & 2 deletions src/Squidex/Config/Logging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static void AddFilter(this ILoggingBuilder builder)

if (category.StartsWith("Orleans.Runtime.Scheduler", StringComparison.OrdinalIgnoreCase))
{
return level >= LogLevel.Error;
return level >= LogLevel.Warning;
}

if (category.StartsWith("Orleans.", StringComparison.OrdinalIgnoreCase))
Expand All @@ -45,7 +45,7 @@ public static void AddFilter(this ILoggingBuilder builder)

if (category.StartsWith("Microsoft.AspNetCore.", StringComparison.OrdinalIgnoreCase))
{
return level > LogLevel.Information;
return level >= LogLevel.Warning;
}
#if LOG_ALL_IDENTITY_SERVER
if (category.StartsWith("IdentityServer4.", StringComparison.OrdinalIgnoreCase))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task Should_call_grain_when_content_created()
{
await sut.On(E(new ContentCreated { Data = data }));

A.CallTo(() => grain.IndexAsync(contentId, A<J<IndexData>>.That.Matches(x => x.Value.Data == data), true))
A.CallTo(() => grain.IndexAsync(A<J<Update>>.That.Matches(x => x.Value.Data == data && x.Value.Id == contentId && x.Value.OnlyDraft)))
.MustHaveHappened();
}

Expand All @@ -61,7 +61,7 @@ public async Task Should_call_grain_when_content_updated()
{
await sut.On(E(new ContentUpdated { Data = data }));

A.CallTo(() => grain.IndexAsync(contentId, A<J<IndexData>>.That.Matches(x => x.Value.Data == data), false))
A.CallTo(() => grain.IndexAsync(A<J<Update>>.That.Matches(x => x.Value.Data == data && x.Value.Id == contentId && !x.Value.OnlyDraft)))
.MustHaveHappened();
}

Expand All @@ -70,7 +70,7 @@ public async Task Should_call_grain_when_content_change_proposed()
{
await sut.On(E(new ContentUpdateProposed { Data = data }));

A.CallTo(() => grain.IndexAsync(contentId, A<J<IndexData>>.That.Matches(x => x.Value.Data == data), true))
A.CallTo(() => grain.IndexAsync(A<J<Update>>.That.Matches(x => x.Value.Data == data && x.Value.Id == contentId && x.Value.OnlyDraft)))
.MustHaveHappened();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ private async Task AddLocalizedContent()
new ContentFieldData()
.AddValue("en", "City and Surroundings und sonstiges"));

await sut.IndexAsync(ids1[0], new IndexData { Data = germanData }, true);
await sut.IndexAsync(ids2[0], new IndexData { Data = englishData }, true);
await sut.IndexAsync(new Update { Id = ids1[0], Data = germanData, OnlyDraft = true });
await sut.IndexAsync(new Update { Id = ids2[0], Data = englishData, OnlyDraft = true });
}

private async Task AddInvariantContent(string text1, string text2, bool onlyDraft = false)
Expand All @@ -229,8 +229,8 @@ private async Task AddInvariantContent(string text1, string text2, bool onlyDraf
new ContentFieldData()
.AddValue("iv", text2));

await sut.IndexAsync(ids1[0], new IndexData { Data = data1 }, onlyDraft);
await sut.IndexAsync(ids2[0], new IndexData { Data = data2 }, onlyDraft);
await sut.IndexAsync(new Update { Id = ids1[0], Data = data1, OnlyDraft = onlyDraft });
await sut.IndexAsync(new Update { Id = ids2[0], Data = data2, OnlyDraft = onlyDraft });
}

private async Task DeleteAsync(Guid id)
Expand Down

0 comments on commit 842c38e

Please sign in to comment.