Browse Source

Fix for indexing.

pull/352/head
Sebastian Stehle 8 years ago
parent
commit
842c38e20d
  1. 15
      src/Squidex.Domain.Apps.Entities/Contents/Text/GrainTextIndexer.cs
  2. 6
      src/Squidex.Domain.Apps.Entities/Contents/Text/ITextIndexerGrain.cs
  3. 27
      src/Squidex.Domain.Apps.Entities/Contents/Text/TextIndexerGrain.cs
  4. 7
      src/Squidex.Domain.Apps.Entities/Contents/Text/Update.cs
  5. 28
      src/Squidex/Config/Domain/SerializationInitializer.cs
  6. 4
      src/Squidex/Config/Logging.cs
  7. 6
      tests/Squidex.Domain.Apps.Entities.Tests/Contents/Text/GrainTextIndexerTests.cs
  8. 8
      tests/Squidex.Domain.Apps.Entities.Tests/Contents/Text/TextIndexerGrainTests.cs

15
src/Squidex.Domain.Apps.Entities/Contents/Text/GrainTextIndexer.cs

@ -57,11 +57,6 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text
{ {
var index = grainFactory.GetGrain<ITextIndexerGrain>(contentEvent.SchemaId.Id); var index = grainFactory.GetGrain<ITextIndexerGrain>(contentEvent.SchemaId.Id);
if (index == null)
{
throw new InvalidOperationException("Cannot create reference to grain.");
}
var id = contentEvent.ContentId; var id = contentEvent.ContentId;
switch (@event.Payload) switch (@event.Payload)
@ -70,13 +65,13 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text
await index.DeleteAsync(id); await index.DeleteAsync(id);
break; break;
case ContentCreated contentCreated: case ContentCreated contentCreated:
await index.IndexAsync(id, Data(contentCreated.Data), true); await index.IndexAsync(Data(id, contentCreated.Data, true));
break; break;
case ContentUpdateProposed contentUpdateProposed: case ContentUpdateProposed contentUpdateProposed:
await index.IndexAsync(id, Data(contentUpdateProposed.Data), true); await index.IndexAsync(Data(id, contentUpdateProposed.Data, true));
break; break;
case ContentUpdated contentUpdated: case ContentUpdated contentUpdated:
await index.IndexAsync(id, Data(contentUpdated.Data), false); await index.IndexAsync(Data(id, contentUpdated.Data, false));
break; break;
case ContentChangesDiscarded _: case ContentChangesDiscarded _:
await index.CopyAsync(id, false); await index.CopyAsync(id, false);
@ -89,9 +84,9 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text
} }
} }
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) public async Task<List<Guid>> SearchAsync(string queryText, IAppEntity app, Guid schemaId, Scope scope = Scope.Published)

6
src/Squidex.Domain.Apps.Entities/Contents/Text/ITextIndexerGrain.cs

@ -15,11 +15,11 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text
{ {
public interface ITextIndexerGrain : IGrainWithGuidKey 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); Task<List<Guid>> SearchAsync(string queryText, SearchContext context);
} }

27
src/Squidex.Domain.Apps.Entities/Contents/Text/TextIndexerGrain.cs

@ -81,29 +81,34 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text
} }
} }
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(); 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); var content = new TextIndexContent(indexWriter, indexState, id);
content.Index(data.Value.Data, onlyDraft); content.Copy(fromDraft);
return TryFlushAsync(); return TryFlushAsync();
} }
public Task CopyAsync(Guid id, bool fromDraft) public Task<bool> DeleteAsync(Guid id)
{ {
var content = new TextIndexContent(indexWriter, indexState, id); var content = new TextIndexContent(indexWriter, indexState, id);
content.Copy(fromDraft); content.Delete();
return TryFlushAsync(); return TryFlushAsync();
} }
@ -164,7 +169,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text
} }
} }
private async Task TryFlushAsync() private async Task<bool> TryFlushAsync()
{ {
timer?.Dispose(); timer?.Dispose();
@ -173,6 +178,8 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text
if (updates >= MaxUpdates) if (updates >= MaxUpdates)
{ {
await FlushAsync(); await FlushAsync();
return true;
} }
else else
{ {
@ -184,9 +191,11 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text
} }
catch (InvalidOperationException) catch (InvalidOperationException)
{ {
return; return false;
} }
} }
return false;
} }
public async Task FlushAsync() public async Task FlushAsync()

7
src/Squidex.Domain.Apps.Entities/Contents/Text/IndexData.cs → src/Squidex.Domain.Apps.Entities/Contents/Text/Update.cs

@ -5,12 +5,17 @@
// All rights reserved. Licensed under the MIT license. // All rights reserved. Licensed under the MIT license.
// ========================================================================== // ==========================================================================
using System;
using Squidex.Domain.Apps.Core.Contents; using Squidex.Domain.Apps.Core.Contents;
namespace Squidex.Domain.Apps.Entities.Contents.Text 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 NamedContentData Data { get; set; }
public bool OnlyDraft { get; set; }
} }
} }

28
src/Squidex/Config/Domain/SerializationInitializer.cs

@ -12,30 +12,48 @@ using Newtonsoft.Json;
using Squidex.Areas.Api.Controllers.Rules.Models; using Squidex.Areas.Api.Controllers.Rules.Models;
using Squidex.Domain.Apps.Core.HandleRules; using Squidex.Domain.Apps.Core.HandleRules;
using Squidex.Infrastructure; using Squidex.Infrastructure;
using Squidex.Infrastructure.Json;
using Squidex.Infrastructure.MongoDb; using Squidex.Infrastructure.MongoDb;
using Squidex.Infrastructure.Orleans;
using Squidex.Infrastructure.Tasks; using Squidex.Infrastructure.Tasks;
namespace Squidex.Config.Domain namespace Squidex.Config.Domain
{ {
public sealed class SerializationInitializer : IInitializable public sealed class SerializationInitializer : IInitializable
{ {
private readonly JsonSerializer jsonSerializer; private readonly JsonSerializer jsonNetSerializer;
private readonly IJsonSerializer jsonSerializer;
private readonly RuleRegistry ruleRegistry; 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.jsonSerializer = jsonSerializer;
this.ruleRegistry = ruleRegistry; this.ruleRegistry = ruleRegistry;
} }
public Task InitializeAsync(CancellationToken ct = default) 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); 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
src/Squidex/Config/Logging.cs

@ -30,7 +30,7 @@ namespace Squidex.Config
if (category.StartsWith("Orleans.Runtime.Scheduler", StringComparison.OrdinalIgnoreCase)) if (category.StartsWith("Orleans.Runtime.Scheduler", StringComparison.OrdinalIgnoreCase))
{ {
return level >= LogLevel.Error; return level >= LogLevel.Warning;
} }
if (category.StartsWith("Orleans.", StringComparison.OrdinalIgnoreCase)) if (category.StartsWith("Orleans.", StringComparison.OrdinalIgnoreCase))
@ -45,7 +45,7 @@ namespace Squidex.Config
if (category.StartsWith("Microsoft.AspNetCore.", StringComparison.OrdinalIgnoreCase)) if (category.StartsWith("Microsoft.AspNetCore.", StringComparison.OrdinalIgnoreCase))
{ {
return level > LogLevel.Information; return level >= LogLevel.Warning;
} }
#if LOG_ALL_IDENTITY_SERVER #if LOG_ALL_IDENTITY_SERVER
if (category.StartsWith("IdentityServer4.", StringComparison.OrdinalIgnoreCase)) if (category.StartsWith("IdentityServer4.", StringComparison.OrdinalIgnoreCase))

6
tests/Squidex.Domain.Apps.Entities.Tests/Contents/Text/GrainTextIndexerTests.cs

@ -52,7 +52,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text
{ {
await sut.On(E(new ContentCreated { Data = data })); 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(); .MustHaveHappened();
} }
@ -61,7 +61,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text
{ {
await sut.On(E(new ContentUpdated { Data = data })); 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(); .MustHaveHappened();
} }
@ -70,7 +70,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text
{ {
await sut.On(E(new ContentUpdateProposed { Data = data })); 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(); .MustHaveHappened();
} }

8
tests/Squidex.Domain.Apps.Entities.Tests/Contents/Text/TextIndexerGrainTests.cs

@ -211,8 +211,8 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text
new ContentFieldData() new ContentFieldData()
.AddValue("en", "City and Surroundings und sonstiges")); .AddValue("en", "City and Surroundings und sonstiges"));
await sut.IndexAsync(ids1[0], new IndexData { Data = germanData }, true); await sut.IndexAsync(new Update { Id = ids1[0], Data = germanData, OnlyDraft = true });
await sut.IndexAsync(ids2[0], new IndexData { Data = englishData }, true); await sut.IndexAsync(new Update { Id = ids2[0], Data = englishData, OnlyDraft = true });
} }
private async Task AddInvariantContent(string text1, string text2, bool onlyDraft = false) private async Task AddInvariantContent(string text1, string text2, bool onlyDraft = false)
@ -229,8 +229,8 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text
new ContentFieldData() new ContentFieldData()
.AddValue("iv", text2)); .AddValue("iv", text2));
await sut.IndexAsync(ids1[0], new IndexData { Data = data1 }, onlyDraft); await sut.IndexAsync(new Update { Id = ids1[0], Data = data1, OnlyDraft = onlyDraft });
await sut.IndexAsync(ids2[0], new IndexData { Data = data2 }, onlyDraft); await sut.IndexAsync(new Update { Id = ids2[0], Data = data2, OnlyDraft = onlyDraft });
} }
private async Task DeleteAsync(Guid id) private async Task DeleteAsync(Guid id)

Loading…
Cancel
Save