Skip to content

Commit

Permalink
refactor: remove LinqKit (#453)
Browse files Browse the repository at this point in the history
  • Loading branch information
melflitty-aneo authored Aug 1, 2023
2 parents a3dd2b8 + 449ed9a commit a9bd09a
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 55 deletions.
1 change: 0 additions & 1 deletion Common/src/ArmoniK.Core.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
<PackageReference Include="Calzolari.Grpc.AspNetCore.Validation" Version="6.3.0" />
<PackageReference Include="Grpc.HealthCheck" Version="2.54.0" />
<PackageReference Include="JetBrains.Annotations" Version="2022.3.1" />
<PackageReference Include="LinqKit.Microsoft.EntityFrameworkCore" Version="7.1.4" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="stateless" Version="5.13.0" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
Expand Down
89 changes: 89 additions & 0 deletions Common/src/gRPC/ExpressionExt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// This file is part of the ArmoniK project
//
// Copyright (C) ANEO, 2021-2023. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Linq.Expressions;

namespace ArmoniK.Core.Common.gRPC;

public static class ExpressionExt
{
private static Expression<Func<T, bool>> MakeBinaryExpression<T>(ExpressionType expressionType,
Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
{
var parameter = Expression.Parameter(typeof(T));

var leftVisitor = new ReplaceExpressionVisitor(expr1.Parameters[0],
parameter);
var left = leftVisitor.Visit(expr1.Body);

var rightVisitor = new ReplaceExpressionVisitor(expr2.Parameters[0],
parameter);
var right = rightVisitor.Visit(expr2.Body);

return Expression.Lambda<Func<T, bool>>(Expression.MakeBinary(expressionType,
left!,
right!),
parameter);
}

/// <summary>
/// Combines two predicate expressions using a logical AND condition
/// </summary>
/// <typeparam name="T"> The type of the input parameter the predicate expressions are evaluated on </typeparam>
/// <param name="expr1"> The first predicate expression to combine </param>
/// <param name="expr2"> The second predicate expression to combine </param>
/// <returns> A new predicate expression that represents the logical AND of the two expressions </returns>
public static Expression<Func<T, bool>> ExpressionAnd<T>(this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
=> MakeBinaryExpression(ExpressionType.AndAlso,
expr1,
expr2);


/// <summary>
/// Combines two predicate expressions using a logical OR condition
/// </summary>
/// <typeparam name="T"> The type of the input parameter the predicate expressions are evaluated on </typeparam>
/// <param name="expr1"> The first predicate expression to combine </param>
/// <param name="expr2"> The second predicate expression to combine </param>
/// <returns> A new predicate expression that represents the logical OR of the two expressions </returns>
public static Expression<Func<T, bool>> ExpressionOr<T>(this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
=> MakeBinaryExpression(ExpressionType.OrElse,
expr1,
expr2);

private class ReplaceExpressionVisitor : ExpressionVisitor
{
private readonly Expression newValue_;
private readonly Expression oldValue_;

public ReplaceExpressionVisitor(Expression oldValue,
Expression newValue)
{
oldValue_ = oldValue;
newValue_ = newValue;
}

public override Expression? Visit(Expression? node)
=> node == oldValue_
? newValue_
: base.Visit(node);
}
}
16 changes: 9 additions & 7 deletions Common/src/gRPC/ListApplicationsRequestExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
using ArmoniK.Api.gRPC.V1.Applications;
using ArmoniK.Core.Common.Storage;

using LinqKit;

namespace ArmoniK.Core.Common.gRPC;

public static class ListApplicationsRequestExt
Expand Down Expand Up @@ -67,22 +65,25 @@ public static class ListApplicationsRequestExt
/// <exception cref="ArgumentOutOfRangeException">the given message is not recognized</exception>
public static Expression<Func<TaskData, bool>> ToApplicationFilter(this Filters filters)
{
var predicate = PredicateBuilder.New<TaskData>();
Expression<Func<TaskData, bool>> expr = data => false;


if (filters.Or == null || !filters.Or.Any())
{
return data => true;
}


foreach (var filtersAnd in filters.Or)
{
var predicateAnd = PredicateBuilder.New<TaskData>(data => true);
Expression<Func<TaskData, bool>> exprAnd = data => true;

foreach (var filterField in filtersAnd.And)
{
switch (filterField.ValueConditionCase)
{
case FilterField.ValueConditionOneofCase.FilterString:
predicateAnd = predicateAnd.And(filterField.FilterString.Operator.ToFilter(filterField.Field.ApplicationField_.Field.ToField(),
exprAnd = exprAnd.ExpressionAnd(filterField.FilterString.Operator.ToFilter(filterField.Field.ApplicationField_.Field.ToField(),
filterField.FilterString.Value));
break;
case FilterField.ValueConditionOneofCase.None:
Expand All @@ -91,9 +92,10 @@ public static Expression<Func<TaskData, bool>> ToApplicationFilter(this Filters
}
}

predicate = predicate.Or(predicateAnd);
expr = expr.ExpressionOr(exprAnd);
}

return predicate;

return expr;
}
}
18 changes: 8 additions & 10 deletions Common/src/gRPC/ListPartitionsRequestExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

using ArmoniK.Core.Common.Storage;

using LinqKit;

namespace ArmoniK.Core.Common.gRPC;

public static class ListPartitionsRequestExt
Expand Down Expand Up @@ -68,7 +66,7 @@ public static class ListPartitionsRequestExt
/// <exception cref="ArgumentOutOfRangeException">the given message is not recognized</exception>
public static Expression<Func<PartitionData, bool>> ToPartitionFilter(this Filters filters)
{
var predicate = PredicateBuilder.New<PartitionData>();
Expression<Func<PartitionData, bool>> expr = data => false;

if (filters.Or == null || !filters.Or.Any())
{
Expand All @@ -77,25 +75,25 @@ public static Expression<Func<PartitionData, bool>> ToPartitionFilter(this Filte

foreach (var filtersAnd in filters.Or)
{
var predicateAnd = PredicateBuilder.New<PartitionData>(data => true);
Expression<Func<PartitionData, bool>> exprAnd = data => true;
foreach (var filterField in filtersAnd.And)
{
switch (filterField.ValueConditionCase)
{
case FilterField.ValueConditionOneofCase.FilterString:
predicateAnd = predicateAnd.And(filterField.FilterString.Operator.ToFilter(filterField.Field.ToField(),
exprAnd = exprAnd.ExpressionAnd(filterField.FilterString.Operator.ToFilter(filterField.Field.ToField(),
filterField.FilterString.Value));
break;
case FilterField.ValueConditionOneofCase.FilterNumber:
predicateAnd = predicateAnd.And(filterField.FilterNumber.Operator.ToFilter(filterField.Field.ToField(),
exprAnd = exprAnd.ExpressionAnd(filterField.FilterNumber.Operator.ToFilter(filterField.Field.ToField(),
filterField.FilterNumber.Value));
break;
case FilterField.ValueConditionOneofCase.FilterBoolean:
predicateAnd = predicateAnd.And(filterField.FilterBoolean.Operator.ToFilter(filterField.Field.ToField(),
exprAnd = exprAnd.ExpressionAnd(filterField.FilterBoolean.Operator.ToFilter(filterField.Field.ToField(),
filterField.FilterBoolean.Value));
break;
case FilterField.ValueConditionOneofCase.FilterArray:
predicateAnd = predicateAnd.And(filterField.FilterArray.Operator.ToFilter(filterField.Field.ToField(),
exprAnd = exprAnd.ExpressionAnd(filterField.FilterArray.Operator.ToFilter(filterField.Field.ToField(),
filterField.FilterArray.Value));
break;
case FilterField.ValueConditionOneofCase.None:
Expand All @@ -104,9 +102,9 @@ public static Expression<Func<PartitionData, bool>> ToPartitionFilter(this Filte
}
}

predicate = predicate.Or(predicateAnd);
expr = expr.ExpressionOr(exprAnd);
}

return predicate;
return expr;
}
}
18 changes: 8 additions & 10 deletions Common/src/gRPC/ListResultsRequestExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
using ArmoniK.Api.gRPC.V1.Results;
using ArmoniK.Core.Common.Storage;

using LinqKit;

namespace ArmoniK.Core.Common.gRPC;

public static class ListResultsRequestExt
Expand Down Expand Up @@ -66,7 +64,7 @@ public static class ListResultsRequestExt
/// <exception cref="ArgumentOutOfRangeException">the given message is not recognized</exception>
public static Expression<Func<Result, bool>> ToResultFilter(this Filters filters)
{
var predicate = PredicateBuilder.New<Result>();
Expression<Func<Result, bool>> expr = data => false;

if (filters.Or == null || !filters.Or.Any())
{
Expand All @@ -75,25 +73,25 @@ public static Expression<Func<Result, bool>> ToResultFilter(this Filters filters

foreach (var filtersAnd in filters.Or)
{
var predicateAnd = PredicateBuilder.New<Result>(data => true);
Expression<Func<Result, bool>> exprAnd = data => true;
foreach (var filterField in filtersAnd.And)
{
switch (filterField.ValueConditionCase)
{
case FilterField.ValueConditionOneofCase.FilterString:
predicateAnd = predicateAnd.And(filterField.FilterString.Operator.ToFilter(filterField.Field.ToField(),
exprAnd = exprAnd.ExpressionAnd(filterField.FilterString.Operator.ToFilter(filterField.Field.ToField(),
filterField.FilterString.Value));
break;
case FilterField.ValueConditionOneofCase.FilterStatus:
predicateAnd = predicateAnd.And(filterField.FilterStatus.Operator.ToFilter(filterField.Field.ToField(),
exprAnd = exprAnd.ExpressionAnd(filterField.FilterStatus.Operator.ToFilter(filterField.Field.ToField(),
filterField.FilterStatus.Value));
break;
case FilterField.ValueConditionOneofCase.FilterDate:
predicateAnd = predicateAnd.And(filterField.FilterDate.Operator.ToFilter(filterField.Field.ToField(),
exprAnd = exprAnd.ExpressionAnd(filterField.FilterDate.Operator.ToFilter(filterField.Field.ToField(),
filterField.FilterDate.Value.ToDateTime()));
break;
case FilterField.ValueConditionOneofCase.FilterArray:
predicateAnd = predicateAnd.And(filterField.FilterArray.Operator.ToFilter(filterField.Field.ToField(),
exprAnd = exprAnd.ExpressionAnd(filterField.FilterArray.Operator.ToFilter(filterField.Field.ToField(),
filterField.FilterArray.Value));
break;
case FilterField.ValueConditionOneofCase.None:
Expand All @@ -102,9 +100,9 @@ public static Expression<Func<Result, bool>> ToResultFilter(this Filters filters
}
}

predicate = predicate.Or(predicateAnd);
expr = expr.ExpressionOr(exprAnd);
}

return predicate;
return expr;
}
}
22 changes: 10 additions & 12 deletions Common/src/gRPC/ListSessionsRequestExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
using ArmoniK.Api.gRPC.V1.Sessions;
using ArmoniK.Core.Common.Storage;

using LinqKit;

namespace ArmoniK.Core.Common.gRPC;

public static class ListSessionsRequestExt
Expand Down Expand Up @@ -72,7 +70,7 @@ public static class ListSessionsRequestExt
/// <exception cref="ArgumentOutOfRangeException">the given message is not recognized</exception>
public static Expression<Func<SessionData, bool>> ToSessionDataFilter(this Filters filters)
{
var predicate = PredicateBuilder.New<SessionData>();
Expression<Func<SessionData, bool>> expr = data => false;

if (filters.Or == null || !filters.Or.Any())
{
Expand All @@ -81,33 +79,33 @@ public static Expression<Func<SessionData, bool>> ToSessionDataFilter(this Filte

foreach (var filtersAnd in filters.Or)
{
var predicateAnd = PredicateBuilder.New<SessionData>(data => true);
Expression<Func<SessionData, bool>> exprAnd = data => true;
foreach (var filterField in filtersAnd.And)
{
switch (filterField.ValueConditionCase)
{
case FilterField.ValueConditionOneofCase.FilterString:
predicateAnd = predicateAnd.And(filterField.FilterString.Operator.ToFilter(filterField.Field.ToField(),
exprAnd = exprAnd.ExpressionAnd(filterField.FilterString.Operator.ToFilter(filterField.Field.ToField(),
filterField.FilterString.Value));
break;
case FilterField.ValueConditionOneofCase.FilterNumber:
predicateAnd = predicateAnd.And(filterField.FilterNumber.Operator.ToFilter(filterField.Field.ToField(),
exprAnd = exprAnd.ExpressionAnd(filterField.FilterNumber.Operator.ToFilter(filterField.Field.ToField(),
filterField.FilterNumber.Value));
break;
case FilterField.ValueConditionOneofCase.FilterBoolean:
predicateAnd = predicateAnd.And(filterField.FilterBoolean.Operator.ToFilter(filterField.Field.ToField(),
exprAnd = exprAnd.ExpressionAnd(filterField.FilterBoolean.Operator.ToFilter(filterField.Field.ToField(),
filterField.FilterBoolean.Value));
break;
case FilterField.ValueConditionOneofCase.FilterStatus:
predicateAnd = predicateAnd.And(filterField.FilterStatus.Operator.ToFilter(filterField.Field.ToField(),
exprAnd = exprAnd.ExpressionAnd(filterField.FilterStatus.Operator.ToFilter(filterField.Field.ToField(),
filterField.FilterStatus.Value));
break;
case FilterField.ValueConditionOneofCase.FilterDate:
predicateAnd = predicateAnd.And(filterField.FilterDate.Operator.ToFilter(filterField.Field.ToField(),
exprAnd = exprAnd.ExpressionAnd(filterField.FilterDate.Operator.ToFilter(filterField.Field.ToField(),
filterField.FilterDate.Value.ToDateTime()));
break;
case FilterField.ValueConditionOneofCase.FilterArray:
predicateAnd = predicateAnd.And(filterField.FilterArray.Operator.ToFilter(filterField.Field.ToField(),
exprAnd = exprAnd.ExpressionAnd(filterField.FilterArray.Operator.ToFilter(filterField.Field.ToField(),
filterField.FilterArray.Value));
break;
case FilterField.ValueConditionOneofCase.None:
Expand All @@ -116,9 +114,9 @@ public static Expression<Func<SessionData, bool>> ToSessionDataFilter(this Filte
}
}

predicate = predicate.Or(predicateAnd);
expr = expr.ExpressionOr(exprAnd);
}

return predicate;
return expr;
}
}
Loading

0 comments on commit a9bd09a

Please sign in to comment.