Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework spatial boolean methods (#2639)
- Loading branch information
1 parent
8be6cf6
commit f33e7b6
Showing
60 changed files
with
778 additions
and
415 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
...ate/Spatial/src/Data/Filters/Expressions/Handlers/QueryableSpatialBooleanMethodHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using HotChocolate.Configuration; | ||
using HotChocolate.Data.Filters.Expressions; | ||
using HotChocolate.Internal; | ||
using HotChocolate.Language; | ||
using HotChocolate.Language.Visitors; | ||
using HotChocolate.Types.Descriptors; | ||
|
||
namespace HotChocolate.Data.Filters.Spatial | ||
{ | ||
public abstract class QueryableSpatialBooleanMethodHandler | ||
: FilterFieldHandler<QueryableFilterContext, Expression> | ||
{ | ||
private readonly IExtendedType _runtimeType; | ||
|
||
protected abstract int Operation { get; } | ||
|
||
protected abstract bool IsTrue { get; } | ||
protected string GeometryFieldName { get; } | ||
protected string BufferFieldName { get; } | ||
|
||
protected QueryableSpatialBooleanMethodHandler( | ||
IFilterConvention convention, | ||
ITypeInspector inspector, | ||
MethodInfo method) | ||
{ | ||
_runtimeType = inspector.GetReturnType(method); | ||
GeometryFieldName = convention.GetOperationName(SpatialFilterOperations.Geometry); | ||
BufferFieldName = convention.GetOperationName(SpatialFilterOperations.Buffer); | ||
} | ||
|
||
public override bool CanHandle( | ||
ITypeCompletionContext context, | ||
IFilterInputTypeDefinition typeDefinition, | ||
IFilterFieldDefinition fieldDefinition) => | ||
fieldDefinition is FilterOperationFieldDefinition op && | ||
op.Id == Operation; | ||
|
||
public override bool TryHandleEnter( | ||
QueryableFilterContext context, | ||
IFilterField field, | ||
ObjectFieldNode node, | ||
[NotNullWhen(true)] out ISyntaxVisitorAction? action) | ||
{ | ||
if (field is IFilterOperationField filterOperationField) | ||
{ | ||
if (node.Value.IsNull()) | ||
{ | ||
context.ReportError( | ||
ErrorHelper.CreateNonNullError( | ||
field, node.Value, context)); | ||
action = SyntaxVisitor.Skip; | ||
return true; | ||
} | ||
|
||
if (!TryHandleOperation( | ||
context, | ||
filterOperationField, | ||
node, | ||
out Expression? nestedProperty)) | ||
{ | ||
context.ReportError( | ||
ErrorHelper.CouldNotCreateFilterForOperation( | ||
field, node.Value, context)); | ||
action = SyntaxVisitor.Skip; | ||
return true; | ||
} | ||
|
||
context.RuntimeTypes.Push(_runtimeType); | ||
context.PushInstance(nestedProperty ); | ||
action = SyntaxVisitor.SkipAndLeave; | ||
} | ||
else | ||
{ | ||
action = SyntaxVisitor.Break; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
protected abstract bool TryHandleOperation( | ||
QueryableFilterContext context, | ||
IFilterOperationField field, | ||
ObjectFieldNode node, | ||
[NotNullWhen(true)] out Expression? result); | ||
|
||
public override bool TryHandleLeave( | ||
QueryableFilterContext context, | ||
IFilterField field, | ||
ObjectFieldNode node, | ||
[NotNullWhen(true)] out ISyntaxVisitorAction? action) | ||
{ | ||
// Dequeue last | ||
Expression instance = context.PopInstance(); | ||
context.RuntimeTypes.Pop(); | ||
Expression condition = IsTrue ? instance : FilterExpressionBuilder.Not(instance); | ||
|
||
if (context.InMemory) | ||
{ | ||
condition = FilterExpressionBuilder.NotNullAndAlso( | ||
context.GetInstance(), | ||
condition); | ||
} | ||
|
||
context.GetLevel().Enqueue(condition); | ||
action = SyntaxVisitor.Continue; | ||
return true; | ||
} | ||
} | ||
} |
38 changes: 3 additions & 35 deletions
38
...Spatial/src/Data/Filters/Expressions/Handlers/QueryableSpatialContainsOperationHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,19 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using HotChocolate.Data.Filters.Expressions; | ||
using HotChocolate.Language; | ||
using HotChocolate.Types.Descriptors; | ||
using NetTopologySuite.Geometries; | ||
|
||
namespace HotChocolate.Data.Filters.Spatial | ||
{ | ||
public class QueryableSpatialContainsOperationHandler | ||
: QueryableSpatialMethodHandler | ||
: QueryableSpatialContainsOperationHandlerBase | ||
{ | ||
private static readonly MethodInfo _contains = | ||
typeof(Geometry).GetMethod(nameof(Geometry.Contains))!; | ||
|
||
public QueryableSpatialContainsOperationHandler( | ||
IFilterConvention convention, | ||
ITypeInspector inspector) | ||
: base(convention, inspector, _contains) | ||
: base(convention, inspector) | ||
{ | ||
} | ||
|
||
protected override int Operation => SpatialFilterOperations.Contains; | ||
|
||
protected override bool TryHandleOperation( | ||
QueryableFilterContext context, | ||
IFilterOperationField field, | ||
ObjectFieldNode node, | ||
[NotNullWhen(true)] out Expression? result) | ||
{ | ||
if (TryGetParameter(field, node.Value, GeometryFieldName, out Geometry g)) | ||
{ | ||
if (TryGetParameter(field, node.Value, BufferFieldName, out double buffer)) | ||
{ | ||
result = ExpressionBuilder.Contains( | ||
context.GetInstance(), | ||
ExpressionBuilder.Buffer(g, buffer)); | ||
|
||
return true; | ||
} | ||
|
||
result = ExpressionBuilder.Contains(context.GetInstance(), g); | ||
return true; | ||
} | ||
|
||
result = null; | ||
return false; | ||
} | ||
protected override bool IsTrue => true; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...ial/src/Data/Filters/Expressions/Handlers/QueryableSpatialContainsOperationHandlerBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using HotChocolate.Data.Filters.Expressions; | ||
using HotChocolate.Language; | ||
using HotChocolate.Types.Descriptors; | ||
using NetTopologySuite.Geometries; | ||
using static HotChocolate.Data.Filters.Spatial.SpatialOperationHandlerHelper; | ||
|
||
namespace HotChocolate.Data.Filters.Spatial | ||
{ | ||
public abstract class QueryableSpatialContainsOperationHandlerBase | ||
: QueryableSpatialBooleanMethodHandler | ||
{ | ||
private static readonly MethodInfo _contains = | ||
typeof(Geometry).GetMethod(nameof(Geometry.Contains))!; | ||
|
||
public QueryableSpatialContainsOperationHandlerBase( | ||
IFilterConvention convention, | ||
ITypeInspector inspector) | ||
: base(convention, inspector, _contains) | ||
{ | ||
} | ||
|
||
protected override bool TryHandleOperation( | ||
QueryableFilterContext context, | ||
IFilterOperationField field, | ||
ObjectFieldNode node, | ||
[NotNullWhen(true)] out Expression? result) | ||
{ | ||
if (TryGetParameter(field, node.Value, GeometryFieldName, out Geometry g)) | ||
{ | ||
if (TryGetParameter(field, node.Value, BufferFieldName, out double buffer)) | ||
{ | ||
result = ExpressionBuilder.Contains( | ||
context.GetInstance(), | ||
ExpressionBuilder.Buffer(g, buffer)); | ||
|
||
return true; | ||
} | ||
|
||
result = ExpressionBuilder.Contains(context.GetInstance(), g); | ||
return true; | ||
} | ||
|
||
result = null; | ||
return false; | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...Spatial/src/Data/Filters/Expressions/Handlers/QueryableSpatialDistanceOperationHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 3 additions & 35 deletions
38
...patial/src/Data/Filters/Expressions/Handlers/QueryableSpatialIntersectOperationHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,19 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using HotChocolate.Data.Filters.Expressions; | ||
using HotChocolate.Language; | ||
using HotChocolate.Types.Descriptors; | ||
using NetTopologySuite.Geometries; | ||
|
||
namespace HotChocolate.Data.Filters.Spatial | ||
{ | ||
public class QueryableSpatialIntersectsOperationHandler | ||
: QueryableSpatialMethodHandler | ||
: QueryableSpatialIntersectsOperationHandlerBase | ||
{ | ||
private static readonly MethodInfo _intersects = | ||
typeof(Geometry).GetMethod(nameof(Geometry.Intersects))!; | ||
|
||
public QueryableSpatialIntersectsOperationHandler( | ||
IFilterConvention convention, | ||
ITypeInspector inspector) | ||
: base(convention, inspector, _intersects) | ||
: base(convention, inspector) | ||
{ | ||
} | ||
|
||
protected override int Operation => SpatialFilterOperations.Intersects; | ||
|
||
protected override bool TryHandleOperation( | ||
QueryableFilterContext context, | ||
IFilterOperationField field, | ||
ObjectFieldNode node, | ||
[NotNullWhen(true)] out Expression? result) | ||
{ | ||
if (TryGetParameter(field, node.Value, GeometryFieldName, out Geometry g)) | ||
{ | ||
if (TryGetParameter(field, node.Value, BufferFieldName, out double buffer)) | ||
{ | ||
result = ExpressionBuilder.Intersects( | ||
context.GetInstance(), | ||
ExpressionBuilder.Buffer(g, buffer)); | ||
|
||
return true; | ||
} | ||
|
||
result = ExpressionBuilder.Intersects(context.GetInstance(), g); | ||
return true; | ||
} | ||
|
||
result = null; | ||
return false; | ||
} | ||
protected override bool IsTrue => true; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...l/src/Data/Filters/Expressions/Handlers/QueryableSpatialIntersectsOperationHandlerBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using HotChocolate.Data.Filters.Expressions; | ||
using HotChocolate.Language; | ||
using HotChocolate.Types.Descriptors; | ||
using NetTopologySuite.Geometries; | ||
using static HotChocolate.Data.Filters.Spatial.SpatialOperationHandlerHelper; | ||
|
||
namespace HotChocolate.Data.Filters.Spatial | ||
{ | ||
public abstract class QueryableSpatialIntersectsOperationHandlerBase | ||
: QueryableSpatialBooleanMethodHandler | ||
{ | ||
private static readonly MethodInfo _intersects = | ||
typeof(Geometry).GetMethod(nameof(Geometry.Intersects))!; | ||
|
||
public QueryableSpatialIntersectsOperationHandlerBase( | ||
IFilterConvention convention, | ||
ITypeInspector inspector) | ||
: base(convention, inspector, _intersects) | ||
{ | ||
} | ||
|
||
protected override bool TryHandleOperation( | ||
QueryableFilterContext context, | ||
IFilterOperationField field, | ||
ObjectFieldNode node, | ||
[NotNullWhen(true)] out Expression? result) | ||
{ | ||
if (TryGetParameter(field, node.Value, GeometryFieldName, out Geometry g)) | ||
{ | ||
if (TryGetParameter(field, node.Value, BufferFieldName, out double buffer)) | ||
{ | ||
result = ExpressionBuilder.Intersects( | ||
context.GetInstance(), | ||
ExpressionBuilder.Buffer(g, buffer)); | ||
|
||
return true; | ||
} | ||
|
||
result = ExpressionBuilder.Intersects(context.GetInstance(), g); | ||
return true; | ||
} | ||
|
||
result = null; | ||
return false; | ||
} | ||
} | ||
} |
Oops, something went wrong.