-
-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathMongoDbSCIMExpressionLinqExtensions.cs
More file actions
239 lines (214 loc) · 13.2 KB
/
MongoDbSCIMExpressionLinqExtensions.cs
File metadata and controls
239 lines (214 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright (c) SimpleIdServer. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using MongoDB.Driver.Linq;
using SimpleIdServer.Scim.Domains;
using SimpleIdServer.Scim.Parser;
using SimpleIdServer.Scim.Parser.Expressions;
using SimpleIdServer.Scim.Parser.Operators;
using SimpleIdServer.Scim.Persistence.MongoDB.Models;
using System;
using System.Linq;
using System.Linq.Expressions;
namespace SimpleIdServer.Scim.Persistence.MongoDB.Extensions
{
public static class MongoDbSCIMExpressionLinqExtensions
{
#region Filter Metadata
public static IMongoQueryable<SCIMRepresentationModel> EvaluateMongoDbRepresentations(this SCIMExpression expression, IMongoQueryable<SCIMRepresentationModel> representations)
{
var treeNodeParameter = Expression.Parameter(typeof(SCIMRepresentationModel), "tn");
var anyWhereExpression = expression.EvaluateRepresentations(treeNodeParameter);
if (anyWhereExpression == null) return null;
var enumarableType = typeof(Queryable);
var whereMethod = enumarableType.GetMethods()
.Where(m => m.Name == "Where" && m.IsGenericMethodDefinition)
.Where(m => m.GetParameters().Count() == 2).First().MakeGenericMethod(typeof(SCIMRepresentationModel));
var equalLambda = Expression.Lambda<Func<SCIMRepresentationModel, bool>>(anyWhereExpression, treeNodeParameter);
var whereExpr = Expression.Call(whereMethod, Expression.Constant(representations), equalLambda);
var finalSelectArg = Expression.Parameter(typeof(IQueryable<SCIMRepresentationModel>), "f");
var finalSelectRequestBody = Expression.Lambda(whereExpr, new ParameterExpression[] { finalSelectArg });
var result = (IMongoQueryable<SCIMRepresentationModel>)finalSelectRequestBody.Compile().DynamicInvoke(representations);
return result;
}
public static Expression EvaluateRepresentations(this SCIMExpression expression, ParameterExpression parameterExpression)
{
var logicalExpression = expression as SCIMLogicalExpression;
var comparisonExpression = expression as SCIMComparisonExpression;
if (logicalExpression != null) return logicalExpression.EvaluateRepresentations(parameterExpression);
if (comparisonExpression != null) return comparisonExpression.EvaluateRepresentations(parameterExpression);
return null;
}
public static Expression EvaluateRepresentations(this SCIMLogicalExpression expression, ParameterExpression parameterExpression)
{
var leftExpression = expression.LeftExpression.EvaluateRepresentations(parameterExpression);
var rightExpression = expression.RightExpression.EvaluateRepresentations(parameterExpression);
if (leftExpression == null && rightExpression == null) return null;
if (leftExpression != null && rightExpression == null) return leftExpression;
if (leftExpression == null && rightExpression != null) return rightExpression;
switch (expression.LogicalOperator)
{
case SCIMLogicalOperators.AND:
return Expression.AndAlso(leftExpression, rightExpression);
default:
return Expression.OrElse(leftExpression, rightExpression);
}
}
public static Expression EvaluateRepresentations(this SCIMComparisonExpression expression, ParameterExpression parameterExpression)
{
var lastChild = expression.LeftExpression.GetLastChild();
var fullPath = lastChild.GetFullPath();
if (!ParserConstants.MappingStandardAttributePathToProperty.ContainsKey(fullPath)) return null;
var propertyName = ParserConstants.MappingStandardAttributePathToProperty[fullPath];
var type = ParserConstants.MappingStandardAttributeTypeToType[fullPath];
MemberExpression propertyValueString = null,
propertyValueInteger = null,
propertyValueDatetime = null;
var schemaAttr = lastChild.SchemaAttribute;
switch (type)
{
case SCIMSchemaAttributeTypes.STRING:
propertyValueString = Expression.Property(parameterExpression, propertyName);
schemaAttr = new SCIMSchemaAttribute(Guid.NewGuid().ToString())
{
Type = SCIMSchemaAttributeTypes.STRING
};
break;
case SCIMSchemaAttributeTypes.INTEGER:
propertyValueInteger = Expression.Property(parameterExpression, propertyName);
schemaAttr = new SCIMSchemaAttribute(Guid.NewGuid().ToString())
{
Type = SCIMSchemaAttributeTypes.INTEGER
};
break;
case SCIMSchemaAttributeTypes.DATETIME:
propertyValueDatetime = Expression.Property(parameterExpression, propertyName);
schemaAttr = new SCIMSchemaAttribute(Guid.NewGuid().ToString())
{
Type = SCIMSchemaAttributeTypes.DATETIME
};
break;
}
return SCIMExpressionLinqExtensions.BuildComparisonExpression(
expression,
schemaAttr,
propertyValueString,
propertyValueInteger,
propertyValueDatetime,
representationParameter: Expression.Parameter(typeof(SCIMRepresentationAttribute), "tn"));
}
#endregion
#region Filter attributes
public static IMongoQueryable<EnrichedAttribute> EvaluateMongoDbAttributes(this SCIMExpression expression, IMongoQueryable<EnrichedAttribute> attributes)
{
var treeNodeParameter = Expression.Parameter(typeof(EnrichedAttribute), "tn");
var anyWhereExpression = expression.EvaluateAttributes(treeNodeParameter);
if (anyWhereExpression == null) return null;
var enumarableType = typeof(Queryable);
var whereMethod = enumarableType.GetMethods()
.Where(m => m.Name == "Where" && m.IsGenericMethodDefinition)
.Where(m => m.GetParameters().Count() == 2).First().MakeGenericMethod(typeof(EnrichedAttribute));
var equalLambda = Expression.Lambda<Func<EnrichedAttribute, bool>>(anyWhereExpression, treeNodeParameter);
var whereExpr = Expression.Call(whereMethod, Expression.Constant(attributes), equalLambda);
var finalSelectArg = Expression.Parameter(typeof(IQueryable<EnrichedAttribute>), "f");
var finalSelectRequestBody = Expression.Lambda(whereExpr, new ParameterExpression[] { finalSelectArg });
var result = (IMongoQueryable<EnrichedAttribute>)finalSelectRequestBody.Compile().DynamicInvoke(attributes);
return result;
}
public static Expression EvaluateAttributes(this SCIMExpression expression, ParameterExpression parameterExpression)
{
var attrExpression = expression as SCIMAttributeExpression;
var logicalExpression = expression as SCIMLogicalExpression;
var comparisonExpression = expression as SCIMComparisonExpression;
if (attrExpression != null) return attrExpression.EvaluateAttributes(parameterExpression);
if (logicalExpression != null) return logicalExpression.EvaluateAttributes(parameterExpression);
if (comparisonExpression != null) return comparisonExpression.EvaluateAttributes(parameterExpression);
return null;
}
public static Expression EvaluateAttributes(this SCIMAttributeExpression expression, ParameterExpression parameterExpression)
{
var schemaAttributeIdProperty = Expression.Property(Expression.Property(parameterExpression, "Attribute"), "SchemaAttributeId");
var equal = Expression.Equal(schemaAttributeIdProperty, Expression.Constant(expression.SchemaAttribute.Id));
var complex = expression as SCIMComplexAttributeExpression;
if (complex != null)
{
return complex.GroupingFilter.EvaluateAttributes(parameterExpression);
}
return equal;
}
public static Expression EvaluateAttributes(this SCIMLogicalExpression expression, ParameterExpression parameterExpression)
{
var leftExpression = expression.LeftExpression.EvaluateAttributes(parameterExpression);
var rightExpression = expression.RightExpression.EvaluateAttributes(parameterExpression);
if (leftExpression == null && rightExpression == null) return null;
if (leftExpression != null && rightExpression == null) return leftExpression;
if (leftExpression == null && rightExpression != null) return rightExpression;
switch (expression.LogicalOperator)
{
case SCIMLogicalOperators.AND:
return Expression.AndAlso(leftExpression, rightExpression);
default:
return Expression.OrElse(leftExpression, rightExpression);
}
}
public static Expression EvaluateAttributes(this SCIMComparisonExpression expression, ParameterExpression parameterExpression)
{
var lastChild = expression.LeftExpression.GetLastChild();
if (ParserConstants.MappingStandardAttributePathToProperty.ContainsKey(lastChild.GetFullPath())) return null;
var attr = Expression.Property(parameterExpression, "Attribute");
var schemaAttributeId = Expression.Property(attr, "SchemaAttributeId");
var propertyValueString = Expression.Property(attr, "ValueString");
var propertyValueInteger = Expression.Property(attr, "ValueInteger");
var propertyValueDatetime = Expression.Property(attr, "ValueDateTime");
var propertyValueBoolean = Expression.Property(attr, "ValueBoolean");
var propertyValueDecimal = Expression.Property(attr, "ValueDecimal");
var propertyValueBinary = Expression.Property(attr, "ValueBinary");
// Use MongoDB-optimized comparison expressions for string types to avoid $expr performance issues
Expression comparison;
if (lastChild.SchemaAttribute.Type == SCIMSchemaAttributeTypes.STRING && !lastChild.SchemaAttribute.CaseExact)
{
comparison = BuildMongoDbOptimizedStringComparison(expression, propertyValueString);
}
else
{
comparison = SCIMExpressionLinqExtensions.BuildComparisonExpression(expression, lastChild.SchemaAttribute,
propertyValueString,
propertyValueInteger,
propertyValueDatetime,
propertyValueBoolean,
propertyValueDecimal,
propertyValueBinary,
parameterExpression);
}
return Expression.And(Expression.Equal(schemaAttributeId, Expression.Constant(lastChild.SchemaAttribute.Id)), comparison);
}
/// <summary>
/// Builds MongoDB-optimized string comparison expressions that avoid $expr and can use indexes effectively
/// </summary>
private static Expression BuildMongoDbOptimizedStringComparison(SCIMComparisonExpression expression, MemberExpression propertyValueString)
{
var value = expression.Value;
switch (expression.ComparisonOperator)
{
case SCIMComparisonOperators.EQ:
return MongoDbOptimizedExpressionExtensions.CreateOptimizedCaseInsensitiveEqual(propertyValueString, value);
case SCIMComparisonOperators.NE:
var equalExpr = MongoDbOptimizedExpressionExtensions.CreateOptimizedCaseInsensitiveEqual(propertyValueString, value);
return Expression.Not(equalExpr);
case SCIMComparisonOperators.SW:
return MongoDbOptimizedExpressionExtensions.CreateOptimizedCaseInsensitiveStartsWith(propertyValueString, value);
case SCIMComparisonOperators.EW:
return MongoDbOptimizedExpressionExtensions.CreateOptimizedCaseInsensitiveEndsWith(propertyValueString, value);
case SCIMComparisonOperators.CO:
return MongoDbOptimizedExpressionExtensions.CreateOptimizedCaseInsensitiveContains(propertyValueString, value);
default:
// For other operators (GT, LT, GE, LE, PR), fall back to default behavior
// as they don't typically benefit from case-insensitive optimization
return SCIMExpressionLinqExtensions.BuildComparisonExpression(
expression,
new SCIMSchemaAttribute("temp") { Type = SCIMSchemaAttributeTypes.STRING, CaseExact = false },
propertyValueString);
}
}
#endregion
}
}