Skip to content

Commit

Permalink
Merge pull request #41 from Evodim/feature/table_client_add_new_instr…
Browse files Browse the repository at this point in the history
…uction_filter

addnew query instruction helper for tags
  • Loading branch information
medevod authored Feb 7, 2023
2 parents e74ba4e + a0d5a8e commit 26612b7
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async Task Run()

await foreach (var batch in _projectionClient.GetAsync(p =>
p
.WithTags()
.IncludeTags()
.WherePartitionKey()
.GreaterThanOrEqual("~")))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Azure.EntityServices.Queries;
using Azure.EntityServices.Tables;
using Azure.EntityServices.Tables;
using Common.Samples.Models;
using Common.Samples.Tools.Fakes;
using Microsoft.Extensions.Azure;
Expand All @@ -25,7 +24,6 @@ public SampleConsole(IAzureClientFactory<IEntityTableClient<PersonEntity>> entit
{
_entityClient = entityClientFactory.CreateClient("Source");
_projectionClient = entityClientFactory.CreateClient("Projection");

}

public async Task Run()
Expand All @@ -43,20 +41,17 @@ public async Task Run()
await _entityClient.AddOrReplaceManyAsync(persons);

var count = 0;
await foreach (var result in _entityClient
await foreach (var result in _entityClient
.GetAsync(filter => filter
.WhereTag("LastName")
.GreaterThan("")))
.WithTag(p => p.LastName)))

{

foreach (var person in result)
{
Console.WriteLine(person.LastName);
{
Console.WriteLine(person.LastName);
}
count += result.Count();
await _projectionClient.AddOrReplaceManyAsync(result);


}
Console.WriteLine($"updated projection entities: {count}");
Console.WriteLine("====================================");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ namespace Azure.EntityServices.Tables
/// </summary>
public static class TableQueryExtensions
{
public static IFilterOperator<T> IgnoreTags<T>(this IQuery<T> query)

=> (query as IQueryCompose<T>)
.AddQuery("PartitionKey")
.LessThan("~")
.AndRowKey()
.LessThan("~");


public static IQueryFilter<T> WherePartitionKey<T>(this IQuery<T> query)
=> (query as IQueryCompose<T>).AddQuery("PartitionKey");

Expand Down
66 changes: 64 additions & 2 deletions src/Azure.EntityServices.Tables/Queries/TableTagQueryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,77 @@ namespace Azure.EntityServices.Tables
/// </summary>
public static class TableTagQueryExtensions
{
public static IFilterOperator<T> IgnoreTags<T>(this IQuery<T> query)

=> (query as IQueryCompose<T>)
.AddQuery("PartitionKey")
.LessThan("~")
.AndRowKey()
.LessThan("~");

/// <summary>
/// Use this extension to show all entities of a table including tagged partitions and rows
/// Use this extension to include tagged entities (replicated partitions and rows)
/// </summary>
public static IQuery<T> WithTags<T>(this IQuery<T> query)
public static IQuery<T> IncludeTags<T>(this IQuery<T> query)
{
(query as ITagQueryCompose<T>).TagName = " ";

return query;
}

/// <summary>
/// Filter only tagged partitions and rows
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="query"></param>
/// <returns></returns>
public static IFilterOperator<T> WithOnlyTags<T>(this IQuery<T> query)

{
(query as ITagQueryCompose<T>).TagName = " ";
return (query as IQueryCompose<T>)
.AddQuery("PartitionKey")
.GreaterThan("~")
.AndRowKey()
.GreaterThan("~");
}

/// <summary>
/// Allow to retrieve all entities for a given tag
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="query"></param>
/// <param name="tagName"></param>
/// <returns></returns>
public static IFilterOperator<T> WithTag<T>(this IQuery<T> query, string tagName)
{
(query as ITagQueryCompose<T>).TagName = " ";
return query
.WhereRowKey()
.GreaterThan($"~{tagName}-")
.AndRowKey()
.LessThan($"~{tagName}-~");
}

/// <summary>
/// Allow to retrieve all entities for a given tag as property selector
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="P"></typeparam>
/// <param name="query"></param>
/// <param name="tagSelector"></param>
/// <returns></returns>
public static IFilterOperator<T> WithTag<T, P>(this IQuery<T> query, Expression<Func<T, P>> tagSelector)
{
var tagName = tagSelector.GetPropertyInfo().Name;
(query as ITagQueryCompose<T>).TagName = " ";
return query
.WhereRowKey()
.GreaterThan($"~{tagName}-")
.AndRowKey()
.LessThan($"~{tagName}-~");
}

public static ITagQueryFilter<T> WhereTag<T>(this IQuery<T> query, string tagName)
{
(query as ITagQueryCompose<T>).TagName = tagName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Azure.EntityServices.Queries;
using Common.Samples.Models;
using Azure.EntityServices.Tables;
using Azure.EntityServices.Tables.Core;
using Common.Samples.Models;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
Expand Down Expand Up @@ -154,6 +154,7 @@ public void Should_Build_Table_Filter_Expression_With_Invariant_Floating_Point_V
result.Should()
.Be("PartitionKey eq 'tenantId' and Latitude eq 48.77309806265856 and Distance eq '148.45648566856' and BankAmount eq '1248.7731'");
}

[TestMethod]
public void Should_Use_Multi_PartitionKey_filter()
{
Expand All @@ -166,14 +167,43 @@ public void Should_Use_Multi_PartitionKey_filter()
.Equal("partition2")
.OrPartitionKey()
.Equal("Partition3");

var queryStr = builder.Build();

queryStr.Trim()
.Should()
.Be("PartitionKey eq 'partition1' or PartitionKey eq 'partition2' or PartitionKey eq 'Partition3'");
}

[TestMethod]
public void Should_Use_WithTag_Extension_To_Get_All_Tag_Values_Without_Filter_Operator()
{
var builder = new TableStorageQueryBuilder<PersonEntity>(new TagFilterExpression<PersonEntity>());

(builder.Query as TagFilterExpression<PersonEntity>)
.WithTag("Created")
.And(p => p.TenantId).Equal("10");
var queryStr = builder.Build();

queryStr.Trim()
.Should()
.Be("RowKey gt '~Created-' and RowKey lt '~Created-~' and TenantId eq '10'");
}

[TestMethod]
public void Should_Use_IncludeTags_To_Get_All_Entities_Included_All_Tags()
{
var builder = new TableStorageQueryBuilder<PersonEntity>(new TagFilterExpression<PersonEntity>());

}

(builder.Query as TagFilterExpression<PersonEntity>)
.IncludeTags()
.WherePartitionKey().Equal("tenant1");

var queryStr = builder.Build();

queryStr.Trim()
.Should()
.Be("PartitionKey eq 'tenant1'");
}
}
}

0 comments on commit 26612b7

Please sign in to comment.