Skip to content
This repository has been archived by the owner on Aug 1, 2022. It is now read-only.

Commit

Permalink
refactor(AutoComplete): refactor autocomplete and add delete documents (
Browse files Browse the repository at this point in the history
#41)

* refactor(AutoComplete): refactor autocomplete

* chore(AutoComplete): Added CancellationToken parameter

* chore: Modify the method name DeleteMultiAsync to DeleteAsync, SetMultiAsync to SetAsync

* chore(AutoComplete): Added constraint to id

* chore: Masa.BuildingBlocks modified to MASA.BuildingBlocks
  • Loading branch information
zhenlei520 authored Apr 20, 2022
1 parent 692f774 commit 15d2042
Show file tree
Hide file tree
Showing 15 changed files with 267 additions and 17 deletions.
7 changes: 7 additions & 0 deletions Masa.BuildingBlocks.sln
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Storage", "Storage", "{4CC7
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.BuildingBlocks.Storage.ObjectStorage", "src\Storage\Masa.BuildingBlocks.Storage.ObjectStorage\Masa.BuildingBlocks.Storage.ObjectStorage.csproj", "{D52E223F-F406-47A7-BF3E-924DC2D74981}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.BuildingBlocks.SearchEngine.AutoComplete.Tests", "test\Masa.BuildingBlocks.SearchEngine.AutoComplete.Tests\Masa.BuildingBlocks.SearchEngine.AutoComplete.Tests.csproj", "{F8681808-45F3-4C2D-8A37-D24C0A3414A5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -134,6 +136,10 @@ Global
{D52E223F-F406-47A7-BF3E-924DC2D74981}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D52E223F-F406-47A7-BF3E-924DC2D74981}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D52E223F-F406-47A7-BF3E-924DC2D74981}.Release|Any CPU.Build.0 = Release|Any CPU
{F8681808-45F3-4C2D-8A37-D24C0A3414A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F8681808-45F3-4C2D-8A37-D24C0A3414A5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F8681808-45F3-4C2D-8A37-D24C0A3414A5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F8681808-45F3-4C2D-8A37-D24C0A3414A5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -167,6 +173,7 @@ Global
{5E3CAD7F-B036-4CA0-9473-4CCA4F5AC5A8} = {022D6FF5-4B65-4213-9A97-C69E2B2F99E1}
{4CC735F3-DB32-4D9D-863D-BB6E1BA362B9} = {77D17E30-CB7C-4DD7-8CF1-9D5350FF2304}
{D52E223F-F406-47A7-BF3E-924DC2D74981} = {4CC735F3-DB32-4D9D-863D-BB6E1BA362B9}
{F8681808-45F3-4C2D-8A37-D24C0A3414A5} = {6ED365E6-4A1A-499F-85FB-F22E865CA4BA}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {40383055-CC50-4600-AD9A-53C14F620D03}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[](README.zh-CN.md) | EN

# Masa.BuildingBlocks
# MASA.BuildingBlocks
Building blocks of the MASA Stack, provides a unified interface standard for [MASA Contrib](https://github.com/masastack/MASA.Contrib/blob/main/README.md) implementation specifications and process connector.


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
namespace Masa.BuildingBlocks.SearchEngine.AutoComplete;
public class AutoCompleteDocument<TValue>
public class AutoCompleteDocument<TValue> where TValue : notnull
{
public string Id => IdGenerator();
private string _id;

public string Id
{
get
{
if (string.IsNullOrEmpty(_id))
return Value?.ToString() ?? throw new ArgumentException("{Id} cannot be empty", nameof(Id));

return _id;
}
init
{
if (string.IsNullOrEmpty(value))
throw new ArgumentException("{Id} cannot be empty", nameof(Id));

_id = value;
}
}

public string Text { get; set; }

Expand All @@ -11,11 +29,15 @@ public AutoCompleteDocument()
{
}

public AutoCompleteDocument(string text, TValue value) : this()
public AutoCompleteDocument(string text, TValue value)
: this(value?.ToString() ?? throw new ArgumentException($"{value} cannot be empty", nameof(value)), text, value)
{
}

public AutoCompleteDocument(string id, string text, TValue value) : this()
{
Id = id;
Text = text;
Value = value;
}

protected virtual string IdGenerator() => $"[{Value}]{Text}";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace Masa.BuildingBlocks.SearchEngine.AutoComplete;
public abstract class BaseAutoCompleteClient : IAutoCompleteClient
{
public virtual Task<GetResponse<AutoCompleteDocument<Guid>, Guid>> GetAsync(string keyword, AutoCompleteOptions? options = null,
CancellationToken cancellationToken = default)
=> GetAsync<Guid>(keyword, options, cancellationToken);

public virtual Task<GetResponse<AutoCompleteDocument<TValue>, TValue>> GetAsync<TValue>(string keyword,
AutoCompleteOptions? options = null,
CancellationToken cancellationToken = default) where TValue : notnull
=> GetAsync<AutoCompleteDocument<TValue>, TValue>(keyword, options, cancellationToken);

public abstract Task<GetResponse<TAudoCompleteDocument, TValue>> GetAsync<TAudoCompleteDocument, TValue>(string keyword,
AutoCompleteOptions? options = null,
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument<TValue> where TValue : notnull;

public virtual Task<SetResponse> SetAsync(AutoCompleteDocument<Guid> document, SetOptions? options = null,
CancellationToken cancellationToken = default)
=> SetAsync<AutoCompleteDocument<Guid>, Guid>(document, options, cancellationToken);

public virtual Task<SetResponse> SetAsync(IEnumerable<AutoCompleteDocument<Guid>> documents, SetOptions? options = null,
CancellationToken cancellationToken = default)
=> SetAsync<AutoCompleteDocument<Guid>, Guid>(documents, options, cancellationToken);

public virtual Task<SetResponse> SetAsync<TValue>(AutoCompleteDocument<TValue> document, SetOptions? options = null,
CancellationToken cancellationToken = default) where TValue : notnull
=> SetAsync<AutoCompleteDocument<TValue>, TValue>(document, options, cancellationToken);

public virtual Task<SetResponse> SetAsync<TValue>(IEnumerable<AutoCompleteDocument<TValue>> documents, SetOptions? options = null,
CancellationToken cancellationToken = default) where TValue : notnull
=> SetAsync<AutoCompleteDocument<TValue>, TValue>(documents, options, cancellationToken);

public virtual Task<SetResponse> SetAsync<TAudoCompleteDocument, TValue>(TAudoCompleteDocument document, SetOptions? options = null,
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument<TValue> where TValue : notnull
=> SetAsync<TAudoCompleteDocument, TValue>(new List<TAudoCompleteDocument> { document }, options, cancellationToken);

public abstract Task<SetResponse> SetAsync<TAudoCompleteDocument, TValue>(IEnumerable<TAudoCompleteDocument> documents,
SetOptions? options = null,
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument<TValue> where TValue : notnull;

public abstract Task<DeleteResponse> DeleteAsync(string id, CancellationToken cancellationToken = default);

public virtual Task<DeleteResponse> DeleteAsync<T>(T id, CancellationToken cancellationToken = default) where T : IComparable
=> DeleteAsync(id!.ToString() ?? throw new ArgumentNullException($"{id} is not null", nameof(id)), cancellationToken);

public abstract Task<DeleteMultiResponse> DeleteAsync(IEnumerable<string> ids, CancellationToken cancellationToken = default);

public virtual Task<DeleteMultiResponse> DeleteAsync<T>(IEnumerable<T> ids, CancellationToken cancellationToken = default) where T : IComparable
{
var type = typeof(T);
if (!type.IsPrimitive && type != typeof(Guid) && type != typeof(string))
throw new NotSupportedException("Unsupported types, id only supports simple types or guid, string");

return DeleteAsync(ids.Select(id => id.ToString() ?? throw new ArgumentNullException($"{id} is not null", nameof(id))), cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,58 @@
namespace Masa.BuildingBlocks.SearchEngine.AutoComplete;

public interface IAutoCompleteClient
{
Task<GetResponse<AutoCompleteDocument<TValue>, TValue>> GetAsync<TValue>(
Task<GetResponse<AutoCompleteDocument<Guid>, Guid>> GetAsync(
string keyword,
AutoCompleteOptions? options = null,
CancellationToken cancellationToken = default);

Task<GetResponse<AutoCompleteDocument<TValue>, TValue>> GetAsync<TValue>(
string keyword,
AutoCompleteOptions? options = null,
CancellationToken cancellationToken = default) where TValue : notnull;

Task<GetResponse<TAudoCompleteDocument, TValue>> GetAsync<TAudoCompleteDocument, TValue>(
string keyword,
AutoCompleteOptions? options = null,
CancellationToken cancellationToken = default)
where TAudoCompleteDocument : AutoCompleteDocument<TValue>;
where TAudoCompleteDocument : AutoCompleteDocument<TValue> where TValue : notnull;

Task<SetResponse> SetAsync<TValue>(
AutoCompleteDocument<TValue>[] results,
Task<SetResponse> SetAsync(
AutoCompleteDocument<Guid> document,
SetOptions? options = null,
CancellationToken cancellationToken = default);

Task<SetResponse> SetAsync(
IEnumerable<AutoCompleteDocument<Guid>> documents,
SetOptions? options = null,
CancellationToken cancellationToken = default);

Task<SetResponse> SetAsync<TValue>(
AutoCompleteDocument<TValue> document,
SetOptions? options = null,
CancellationToken cancellationToken = default) where TValue : notnull;

Task<SetResponse> SetAsync<TValue>(
IEnumerable<AutoCompleteDocument<TValue>> documents,
SetOptions? options = null,
CancellationToken cancellationToken = default) where TValue : notnull;

Task<SetResponse> SetAsync<TAudoCompleteDocument, TValue>(
TAudoCompleteDocument[] documents,
TAudoCompleteDocument document,
SetOptions? options = null,
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument<TValue> ;
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument<TValue> where TValue : notnull;

Task<SetResponse> SetAsync<TAudoCompleteDocument, TValue>(
IEnumerable<TAudoCompleteDocument> documents,
SetOptions? options = null,
CancellationToken cancellationToken = default) where TAudoCompleteDocument : AutoCompleteDocument<TValue> where TValue : notnull;

Task<DeleteResponse> DeleteAsync(string id, CancellationToken cancellationToken = default);

Task<DeleteResponse> DeleteAsync<T>(T id, CancellationToken cancellationToken = default) where T : IComparable;

Task<DeleteMultiResponse> DeleteAsync(IEnumerable<string> ids, CancellationToken cancellationToken = default);

Task<DeleteMultiResponse> DeleteAsync<T>(IEnumerable<T> ids, CancellationToken cancellationToken = default) where T : IComparable;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public int PageSize
}
}

public SearchType SearchType { get; }
public SearchType? SearchType { get; }

public AutoCompleteOptions(SearchType searchType = SearchType.Fuzzy)
public AutoCompleteOptions(SearchType? searchType = null)
{
this.Field = "id";
this.Field = "text";
this.Page = 1;
this.PageSize = 10;
this.SearchType = searchType;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Masa.BuildingBlocks.SearchEngine.AutoComplete.Response;
public class DeleteMultiResponse : ResponseBase
{
public List<DeleteRangeResponseItems> Data { get; set; }

public DeleteMultiResponse(bool isValid, string message) : base(isValid, message)
{
}

public DeleteMultiResponse(bool isValid, string message, IEnumerable<DeleteRangeResponseItems>? data) : this(isValid, message)
{
ArgumentNullException.ThrowIfNull(data, nameof(data));

Data = data.ToList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Masa.BuildingBlocks.SearchEngine.AutoComplete.Response;
public class DeleteRangeResponseItems
{
public string Id { get; }

public bool IsValid { get; }

public string Message { get; }

public DeleteRangeResponseItems(string id, bool isValid, string message)
{
this.Id = id;
this.IsValid = isValid;
this.Message = message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Masa.BuildingBlocks.SearchEngine.AutoComplete.Response;
public class DeleteResponse : ResponseBase
{
public DeleteResponse(bool isValid, string message) : base(isValid, message)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Masa.BuildingBlocks.SearchEngine.AutoComplete.Response;
public class GetResponse<TDropdownBox, TValue> : ResponseBase
where TDropdownBox : AutoCompleteDocument<TValue>
where TDropdownBox : AutoCompleteDocument<TValue> where TValue : notnull
{
public long Total { get; set; }

Expand All @@ -11,4 +11,11 @@ public class GetResponse<TDropdownBox, TValue> : ResponseBase
public GetResponse(bool isValid, string message) : base(isValid, message)
{
}

public GetResponse(bool isValid, string message, IEnumerable<TDropdownBox> data) : this(isValid, message)
{
ArgumentNullException.ThrowIfNull(data,nameof(data));

Data = data.ToList();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
namespace Masa.BuildingBlocks.SearchEngine.AutoComplete.Response;

public class SetResponse : ResponseBase
{
public List<SetResponseItems> Items { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Masa.BuildingBlocks.SearchEngine.AutoComplete.Tests;
public class CustomAutoCompleteClient : BaseAutoCompleteClient
{
public override Task<GetResponse<TAudoCompleteDocument, TValue>> GetAsync<TAudoCompleteDocument, TValue>(string keyword,
AutoCompleteOptions? options = null,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public override Task<SetResponse> SetMultiAsync<TAudoCompleteDocument, TValue>(IEnumerable<TAudoCompleteDocument> documents,
SetOptions? options = null,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public override Task<DeleteResponse> DeleteAsync(string id)
{
throw new NotImplementedException();
}

public override Task<DeleteMultiResponse> DeleteMultiAsync(IEnumerable<string> ids)
=> Task.FromResult(new DeleteMultiResponse(true, ""));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" />
<PackageReference Include="coverlet.collector" Version="3.0.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\SearchEngine\Masa.BuildingBlocks.SearchEngine.AutoComplete\Masa.BuildingBlocks.SearchEngine.AutoComplete.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace Masa.BuildingBlocks.SearchEngine.AutoComplete.Tests;

[TestClass]
public class TestAutoCompleteClient
{
[TestMethod]
public async Task TestDeleteMultiAsyncReturnThrowNotSupportedException()
{
var client = new CustomAutoCompleteClient();
await Assert.ThrowsExceptionAsync<NotSupportedException>(() => client.DeleteMultiAsync(new List<AutoCompleteDocument<int>>()
{
new("2", 2),
new("1", 1),
}));
}

[TestMethod]
public async Task TestDeleteMultiAsyncReturnSuccess()
{
var client = new CustomAutoCompleteClient();
var response = await client.DeleteMultiAsync(new[] { 1, 2 });
Assert.IsTrue(response.IsValid);

response = await client.DeleteMultiAsync(new[] { "1", "2" });
Assert.IsTrue(response.IsValid);

response = await client.DeleteMultiAsync(new[] { 1d, 2d });
Assert.IsTrue(response.IsValid);

response = await client.DeleteMultiAsync(new[] { 1l, 2l });
Assert.IsTrue(response.IsValid);

response = await client.DeleteMultiAsync(new[] { Guid.NewGuid(), Guid.NewGuid() });
Assert.IsTrue(response.IsValid);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
global using Masa.BuildingBlocks.SearchEngine.AutoComplete.Options;
global using Masa.BuildingBlocks.SearchEngine.AutoComplete.Response;
global using Microsoft.VisualStudio.TestTools.UnitTesting;

0 comments on commit 15d2042

Please sign in to comment.