Skip to content

Commit

Permalink
Feature: Remove the non-shard key related handling logic (#32)
Browse files Browse the repository at this point in the history
* feat: remove route key related handling logic

* feat: remove collection route key attribute

* feat: remove useless class

* feat: remove route key code

* perf: remove useless code
  • Loading branch information
AElfBourneShi authored Sep 26, 2024
1 parent 411fd2f commit eb3add5
Show file tree
Hide file tree
Showing 18 changed files with 31 additions and 860 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public override void ConfigureServices(ServiceConfigurationContext context)
services.AddTransient(typeof(IElasticsearchRepository<,>), typeof(ElasticsearchRepository<,>));
services.AddTransient(typeof(ICollectionNameProvider<>), typeof(ElasticsearchCollectionNameProvider<>));
services.AddSingleton(typeof (IShardingKeyProvider<>), typeof (ShardingKeyProvider<>));
services.AddSingleton(typeof(ICollectionRouteKeyProvider<>), typeof(CollectionRouteKeyProvider<>));
services.AddSingleton(typeof(IElasticsearchQueryableFactory<>), typeof(ElasticsearchQueryableFactory<>));
services.AddSingleton(typeof (IShardingCollectionTailProvider<>), typeof (ShardingCollectionTailProvider<>));
var configuration = context.Services.GetConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,16 @@ public class ElasticsearchCollectionNameProvider<TEntity> : CollectionNameProvid
{
private readonly IElasticIndexService _elasticIndexService;
private readonly IShardingKeyProvider<TEntity> _shardingKeyProvider;
private readonly ICollectionRouteKeyProvider<TEntity> _collectionRouteKeyProvider;
private readonly AElfEntityMappingOptions _entityMappingOptions;
private readonly ILogger<ElasticsearchCollectionNameProvider<TEntity>> _logger;

public ElasticsearchCollectionNameProvider(IShardingKeyProvider<TEntity> shardingKeyProvider,
IElasticIndexService elasticIndexService,
ICollectionRouteKeyProvider<TEntity> collectionRouteKeyProvider,
IOptions<AElfEntityMappingOptions> entityMappingOptions,
ILogger<ElasticsearchCollectionNameProvider<TEntity>> logger)
{
_elasticIndexService = elasticIndexService;
_shardingKeyProvider = shardingKeyProvider;
_collectionRouteKeyProvider = collectionRouteKeyProvider;
_entityMappingOptions = entityMappingOptions.Value;
_logger = logger;
}
Expand All @@ -42,7 +39,7 @@ protected override async Task<List<string>> GetCollectionNameAsync(List<Collecti
var shardKeyCollectionNames = await _shardingKeyProvider.GetCollectionNameAsync(conditions);
if (shardKeyCollectionNames.IsNullOrEmpty())
{
return await _collectionRouteKeyProvider.GetCollectionNameAsync(conditions);
return new List<string> { GetDefaultCollectionName() };

Check warning on line 42 in src/AElf.EntityMapping.Elasticsearch/ElasticsearchCollectionNameProvider.cs

View check run for this annotation

Codecov / codecov/patch

src/AElf.EntityMapping.Elasticsearch/ElasticsearchCollectionNameProvider.cs#L42

Added line #L42 was not covered by tests
}

return shardKeyCollectionNames;
Expand Down Expand Up @@ -71,9 +68,7 @@ protected override async Task<List<string>> GetCollectionNameByEntityAsync(List<

protected override async Task<string> GetCollectionNameByIdAsync<TKey>(TKey id)
{
if (!_shardingKeyProvider.IsShardingCollection())
return GetDefaultCollectionName();
return await _collectionRouteKeyProvider.GetCollectionNameAsync(id.ToString());
return GetDefaultCollectionName();
}

protected override string FormatCollectionName(string name)
Expand Down
7 changes: 0 additions & 7 deletions src/AElf.EntityMapping.Elasticsearch/IndexNameHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,4 @@ public static string GetDefaultFullIndexName(Type type,string collectionPrefix)
: $"{collectionPrefix.ToLower()}.{GetDefaultIndexName(type)}";
return fullIndexName;
}
public static string GetCollectionRouteKeyIndexName(Type type, string fieldName,string collectionPrefix)
{
var routeIndexName= collectionPrefix.IsNullOrWhiteSpace()
? $"route.{type.Name.ToLower()}.{fieldName.ToLower()}"
: $"{collectionPrefix.ToLower()}.route.{type.Name.ToLower()}.{fieldName.ToLower()}";
return routeIndexName;
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,6 @@ public async Task CreateIndexTemplateAsync(string indexTemplateName,string index
_logger.LogInformation("Index template {indexTemplateName} created successfully", indexTemplateName);
}
}

public async Task CreateCollectionRouteKeyIndexAsync(Type type, int shard = 1, int numberOfReplicas = 1)
{
var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
foreach (var property in properties)
{
CollectionRouteKeyAttribute shardRouteAttribute = (CollectionRouteKeyAttribute)Attribute.GetCustomAttribute(property, typeof(CollectionRouteKeyAttribute));
if (shardRouteAttribute != null)
{
if (property.PropertyType != typeof(string))
{
throw new NotSupportedException(
$"{type.Name} Attribute Error! NeedShardRouteAttribute only support string type, please check field: {property.Name}");
}
var indexName = IndexNameHelper.GetCollectionRouteKeyIndexName(type, property.Name,_entityMappingOptions.CollectionPrefix);
await CreateIndexAsync(indexName, typeof(RouteKeyCollection), shard, numberOfReplicas);
}
}
}

public async Task DeleteIndexAsync(string collectionName = null, CancellationToken cancellationToken = default)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ private async Task HandleModuleAsync(Type moduleType)
await _elasticIndexService.CreateIndexTemplateAsync(indexTemplateName, indexName, t,
_elasticsearchOptions.NumberOfShards,
_elasticsearchOptions.NumberOfReplicas);
//create index marked field cache
// await _elasticIndexService.InitializeCollectionRouteKeyCacheAsync(t);
//create non shard key route index
await _elasticIndexService.CreateCollectionRouteKeyIndexAsync(t, _elasticsearchOptions.NumberOfShards,
_elasticsearchOptions.NumberOfReplicas);
await CreateShardingCollectionTailIndexAsync();
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@ Task CreateIndexAsync(string indexName, Type indexEntityType, int shard = 1, int
Task CreateIndexTemplateAsync(string indexTemplateName, string indexName, Type indexEntityType, int numberOfShards,
int numberOfReplicas);

Task CreateCollectionRouteKeyIndexAsync(Type indexEntityType, int numberOfShards,
int numberOfReplicas);
Task DeleteIndexAsync(string collectionName = null, CancellationToken cancellationToken = default);
}
Loading

0 comments on commit eb3add5

Please sign in to comment.