Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(elasticsearch): autoComplete Support alwaysGetNewestElasticClient #392

Merged
merged 8 commits into from
Jan 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -10,12 +10,12 @@ public class DefaultTypeAliasFactory : MasaFactoryBase<ITypeAliasProvider, TypeA

protected override string SpecifyServiceNotFoundMessage => "Please make sure you have used [{0}] TypeAlias, it was not found";

protected override MasaFactoryOptions<TypeAliasRelationOptions> FactoryOptions => _optionsMonitor.Value;
protected override MasaFactoryOptions<TypeAliasRelationOptions> FactoryOptions => _options.CurrentValue;

private readonly IOptions<TypeAliasFactoryOptions> _optionsMonitor;
private readonly IOptionsMonitor<TypeAliasFactoryOptions> _options;

public DefaultTypeAliasFactory(IServiceProvider serviceProvider) : base(serviceProvider)
{
_optionsMonitor = serviceProvider.GetRequiredService<IOptions<TypeAliasFactoryOptions>>();
_options = serviceProvider.GetRequiredService<IOptionsMonitor<TypeAliasFactoryOptions>>();
}
}
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ public virtual TService Create()
{
var defaultOptions = GetDefaultOptions(FactoryOptions.Options);
if (defaultOptions == null)
throw new NotImplementedException(DefaultServiceNotFoundMessage);
throw new NotSupportedException(DefaultServiceNotFoundMessage);

return defaultOptions.Func.Invoke(ServiceProvider);
}
@@ -37,7 +37,7 @@ public virtual TService Create(string name)
{
var options = FactoryOptions.Options.SingleOrDefault(c => c.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
if (options == null)
throw new NotImplementedException(string.Format(SpecifyServiceNotFoundMessage, name));
throw new NotSupportedException(string.Format(SpecifyServiceNotFoundMessage, name));

return options.Func.Invoke(ServiceProvider);
}
Original file line number Diff line number Diff line change
@@ -25,13 +25,13 @@ public class DefaultIdGeneratorFactory : MasaFactoryBase<IIdGenerator, IdGenerat
protected override string SpecifyServiceNotFoundMessage { get; } =
"Please make sure you have used [{0}] IdGenerator, it was not found";

protected override MasaFactoryOptions<IdGeneratorRelationOptions> FactoryOptions => _optionsMonitor.CurrentValue;
protected override MasaFactoryOptions<IdGeneratorRelationOptions> FactoryOptions => _options.CurrentValue;

private readonly IOptionsMonitor<IdGeneratorFactoryOptions> _optionsMonitor;
private readonly IOptionsMonitor<IdGeneratorFactoryOptions> _options;

public DefaultIdGeneratorFactory(IServiceProvider serviceProvider) : base(serviceProvider)
{
_optionsMonitor = serviceProvider.GetRequiredService<IOptionsMonitor<IdGeneratorFactoryOptions>>();
_options = serviceProvider.GetRequiredService<IOptionsMonitor<IdGeneratorFactoryOptions>>();
}

public IIdGenerator<TOut> Create<TOut>() where TOut : notnull
Original file line number Diff line number Diff line change
@@ -9,12 +9,12 @@ public class DefaultDeserializerFactory : MasaFactoryBase<IDeserializer, Deseria
protected override string DefaultServiceNotFoundMessage => "Default deserializer not found, you need to add it, like services.AddJson()";

protected override string SpecifyServiceNotFoundMessage => "Please make sure you have used [{0}] deserializer, it was not found";
protected override MasaFactoryOptions<DeserializerRelationOptions> FactoryOptions => _optionsMonitor.CurrentValue;
protected override MasaFactoryOptions<DeserializerRelationOptions> FactoryOptions => _options.CurrentValue;

private readonly IOptionsMonitor<DeserializerFactoryOptions> _optionsMonitor;
private readonly IOptionsMonitor<DeserializerFactoryOptions> _options;

public DefaultDeserializerFactory(IServiceProvider serviceProvider) : base(serviceProvider)
{
_optionsMonitor = serviceProvider.GetRequiredService<IOptionsMonitor<DeserializerFactoryOptions>>();
_options = serviceProvider.GetRequiredService<IOptionsMonitor<DeserializerFactoryOptions>>();
}
}
Original file line number Diff line number Diff line change
@@ -9,12 +9,12 @@ public class DefaultSerializerFactory : MasaFactoryBase<ISerializer, SerializerR
protected override string DefaultServiceNotFoundMessage => "Default serializer not found, you need to add it, like services.AddJson()";

protected override string SpecifyServiceNotFoundMessage => "Please make sure you have used [{0}] serializer, it was not found";
protected override MasaFactoryOptions<SerializerRelationOptions> FactoryOptions => _optionsMonitor.CurrentValue;
protected override MasaFactoryOptions<SerializerRelationOptions> FactoryOptions => _options.CurrentValue;

private readonly IOptionsMonitor<SerializerFactoryOptions> _optionsMonitor;
private readonly IOptionsMonitor<SerializerFactoryOptions> _options;

public DefaultSerializerFactory(IServiceProvider serviceProvider) : base(serviceProvider)
{
_optionsMonitor = serviceProvider.GetRequiredService<IOptionsMonitor<SerializerFactoryOptions>>();
_options = serviceProvider.GetRequiredService<IOptionsMonitor<SerializerFactoryOptions>>();
}
}
Original file line number Diff line number Diff line change
@@ -11,12 +11,12 @@ protected override string DefaultServiceNotFoundMessage

protected override string SpecifyServiceNotFoundMessage => "Please make sure you have used [{0}] typeConvert, it was not found";

protected override MasaFactoryOptions<TypeConvertRelationOptions> FactoryOptions => _optionsMonitor.CurrentValue;
protected override MasaFactoryOptions<TypeConvertRelationOptions> FactoryOptions => _options.CurrentValue;

private readonly IOptionsMonitor<TypeConvertFactoryOptions> _optionsMonitor;
private readonly IOptionsMonitor<TypeConvertFactoryOptions> _options;

public DefaultTypeConvertFactory(IServiceProvider serviceProvider) : base(serviceProvider)
{
_optionsMonitor = serviceProvider.GetRequiredService<IOptionsMonitor<TypeConvertFactoryOptions>>();
_options = serviceProvider.GetRequiredService<IOptionsMonitor<TypeConvertFactoryOptions>>();
}
}
Original file line number Diff line number Diff line change
@@ -12,12 +12,12 @@ protected override string DefaultServiceNotFoundMessage
protected override string SpecifyServiceNotFoundMessage
=> $"Please make sure you have used [{0}] {nameof(IRulesEngineClient)}, it was not found";

protected override MasaFactoryOptions<RulesEngineRelationOptions> FactoryOptions => _options.Value;
protected override MasaFactoryOptions<RulesEngineRelationOptions> FactoryOptions => _options.CurrentValue;

private readonly IOptions<RulesEngineFactoryOptions> _options;
private readonly IOptionsMonitor<RulesEngineFactoryOptions> _options;

public DefaultRulesEngineFactory(IServiceProvider serviceProvider) : base(serviceProvider)
{
_options = serviceProvider.GetRequiredService<IOptions<RulesEngineFactoryOptions>>();
_options = serviceProvider.GetRequiredService<IOptionsMonitor<RulesEngineFactoryOptions>>();
}
}
Original file line number Diff line number Diff line change
@@ -5,6 +5,10 @@ namespace Masa.BuildingBlocks.SearchEngine.AutoComplete;

public abstract class AutoCompleteClientBase : IAutoCompleteClient
{
public abstract Task<bool> BuildAsync(CancellationToken cancellationToken = default);

public abstract Task RebuildAsync(CancellationToken cancellationToken = default);

public virtual Task<GetResponse<AutoCompleteDocument<Guid>>> GetAsync(
string keyword,
AutoCompleteOptions? options = null,
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace

namespace Masa.BuildingBlocks.SearchEngine.AutoComplete;

public class AutoCompleteFactory : MasaFactoryBase<IAutoCompleteClient, AutoCompleteRelationsOptions>, IAutoCompleteFactory
{
protected override string DefaultServiceNotFoundMessage => "No default AutoComplete found";
protected override string SpecifyServiceNotFoundMessage => "Please make sure you have used [{0}] AutoComplete, it was not found";
protected override MasaFactoryOptions<AutoCompleteRelationsOptions> FactoryOptions => _options.CurrentValue;

private readonly IOptionsMonitor<AutoCompleteFactoryOptions> _options;

public AutoCompleteFactory(IServiceProvider serviceProvider) : base(serviceProvider)
{
_options = serviceProvider.GetRequiredService<IOptionsMonitor<AutoCompleteFactoryOptions>>();
}

public IAutoCompleteClient CreateClient() => base.Create();

public IAutoCompleteClient CreateClient(string name) => base.Create(name);
}
Original file line number Diff line number Diff line change
@@ -5,6 +5,10 @@ namespace Masa.BuildingBlocks.SearchEngine.AutoComplete;

public interface IAutoCompleteClient
{
Task<bool> BuildAsync(CancellationToken cancellationToken = default);

Task RebuildAsync(CancellationToken cancellationToken = default);

Task<GetResponse<AutoCompleteDocument<Guid>>> GetAsync(
string keyword,
AutoCompleteOptions? options = null,
Original file line number Diff line number Diff line change
@@ -3,9 +3,11 @@

namespace Masa.BuildingBlocks.SearchEngine.AutoComplete;

public interface IAutoCompleteFactory
public interface IAutoCompleteFactory : IMasaFactory<IAutoCompleteClient>
{
[Obsolete("Use Create() instead")]
IAutoCompleteClient CreateClient();

[Obsolete("Use Create(name) instead")]
IAutoCompleteClient CreateClient(string name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace

namespace Masa.BuildingBlocks.SearchEngine.AutoComplete;

public class AutoCompleteFactoryOptions: MasaFactoryOptions<AutoCompleteRelationsOptions>
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace

namespace Masa.BuildingBlocks.SearchEngine.AutoComplete;

public class AutoCompleteRelationsOptions : MasaRelationOptions<IAutoCompleteClient>
{
public AutoCompleteRelationsOptions(string name) : base(name)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

global using Masa.BuildingBlocks.Data;
global using Masa.BuildingBlocks.SearchEngine.AutoComplete.Options;
global using Masa.BuildingBlocks.SearchEngine.AutoComplete.Response;

global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Options;
Original file line number Diff line number Diff line change
@@ -5,6 +5,16 @@ namespace Masa.BuildingBlocks.SearchEngine.AutoComplete.Tests;

public class CustomAutoCompleteClient : AutoCompleteClientBase
{
public override Task<bool> BuildAsync(CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public override Task RebuildAsync(CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public override Task<GetResponse<TAudoCompleteDocument>> GetBySpecifyDocumentAsync<TAudoCompleteDocument>(string keyword, AutoCompleteOptions? options = null,
CancellationToken cancellationToken = default)
{
Loading