-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathFastFilter.cs
More file actions
262 lines (227 loc) · 8.46 KB
/
FastFilter.cs
File metadata and controls
262 lines (227 loc) · 8.46 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
#if IS_VSTEST_REPO
using System.Collections.Immutable;
#endif
using System.Linq;
using System.Text.RegularExpressions;
#if !IS_VSTEST_REPO
using Microsoft.CodeAnalysis;
#endif
#if IS_VSTEST_REPO
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
#endif
namespace Microsoft.VisualStudio.TestPlatform.Common.Filtering;
#if !IS_VSTEST_REPO
[Embedded]
#endif
internal sealed class FastFilter
{
#if IS_VSTEST_REPO
internal FastFilter(ImmutableDictionary<string, ISet<string>> filterProperties, Operation filterOperation, Operator filterOperator)
#else
internal FastFilter(Dictionary<string, ISet<string>> filterProperties, Operation filterOperation, Operator filterOperator)
#endif
{
#if IS_VSTEST_REPO
ValidateArg.NotNullOrEmpty(filterProperties, nameof(filterProperties));
#endif
FilterProperties = filterProperties;
IsFilteredOutWhenMatched =
(filterOperation != Operation.Equal || filterOperator != Operator.Or && filterOperator != Operator.None)
&& (filterOperation == Operation.NotEqual && (filterOperator == Operator.And || filterOperator == Operator.None)
? true
#if IS_VSTEST_REPO
: throw new ArgumentException(Resources.Resources.FastFilterException));
#else
: throw new ArgumentException("An error occurred while creating Fast filter."));
#endif
}
#if IS_VSTEST_REPO
internal ImmutableDictionary<string, ISet<string>> FilterProperties { get; }
#else
internal Dictionary<string, ISet<string>> FilterProperties { get; }
#endif
internal bool IsFilteredOutWhenMatched { get; }
internal Regex? PropertyValueRegex { get; set; }
internal string? PropertyValueRegexReplacement { get; set; }
internal string[]? ValidForProperties(IEnumerable<string>? properties)
{
if (properties is null)
{
return null;
}
return FilterProperties.Keys.All(name => properties.Contains(name))
? null
: FilterProperties.Keys.Where(name => !properties.Contains(name)).ToArray();
}
internal bool Evaluate(Func<string, object?> propertyValueProvider)
{
#if IS_VSTEST_REPO
ValidateArg.NotNull(propertyValueProvider, nameof(propertyValueProvider));
#endif
bool matched = false;
foreach (var name in FilterProperties.Keys)
{
// If there is no value corresponding to given name, treat it as unmatched.
if (!TryGetPropertyValue(name, propertyValueProvider, out var singleValue, out var multiValues))
{
continue;
}
if (singleValue != null)
{
var value = PropertyValueRegex == null ? singleValue : ApplyRegex(singleValue);
matched = value != null && FilterProperties[name].Contains(value);
}
else
{
var values = PropertyValueRegex == null ? multiValues : multiValues?.Select(value => ApplyRegex(value));
matched = values?.Any(result => result != null && FilterProperties[name].Contains(result)) == true;
}
if (matched)
{
break;
}
}
return IsFilteredOutWhenMatched ? !matched : matched;
}
/// <summary>
/// Apply regex matching or replacement to given value.
/// </summary>
/// <returns>For matching, returns the result of matching, null if no match found. For replacement, returns the result of replacement.</returns>
private string? ApplyRegex(string value)
{
#if IS_VSTEST_REPO
TPDebug.Assert(PropertyValueRegex != null);
#endif
string? result = null;
if (PropertyValueRegexReplacement == null)
{
var match = PropertyValueRegex!.Match(value);
if (match.Success)
{
result = match.Value;
}
}
else
{
result = PropertyValueRegex!.Replace(value, PropertyValueRegexReplacement);
}
return result;
}
/// <summary>
/// Returns property value for Property using propertValueProvider.
/// </summary>
private static bool TryGetPropertyValue(string name, Func<string, object?> propertyValueProvider, out string? singleValue, out string[]? multiValues)
{
var propertyValue = propertyValueProvider(name);
if (null != propertyValue)
{
multiValues = propertyValue as string[];
singleValue = multiValues == null ? propertyValue.ToString() : null;
return true;
}
singleValue = null;
multiValues = null;
return false;
}
internal static Builder CreateBuilder()
{
return new Builder();
}
internal sealed class Builder
{
private bool _operatorEncountered;
private Operator _fastFilterOperator = Operator.None;
private bool _conditionEncountered;
private Operation _fastFilterOperation;
#if IS_VSTEST_REPO
private readonly ImmutableDictionary<string, ImmutableHashSet<string>.Builder>.Builder _filterDictionaryBuilder = ImmutableDictionary.CreateBuilder<string, ImmutableHashSet<string>.Builder>(StringComparer.OrdinalIgnoreCase);
#else
private readonly Dictionary<string, ISet<string>> _filterDictionaryBuilder = new Dictionary<string, ISet<string>>(StringComparer.OrdinalIgnoreCase);
#endif
private bool _containsValidFilter = true;
internal bool ContainsValidFilter => _containsValidFilter && _conditionEncountered;
internal void AddOperator(Operator @operator)
{
if (_containsValidFilter && (@operator == Operator.And || @operator == Operator.Or))
{
if (_operatorEncountered)
{
_containsValidFilter = _fastFilterOperator == @operator;
}
else
{
_operatorEncountered = true;
_fastFilterOperator = @operator;
if ((_fastFilterOperation == Operation.NotEqual && _fastFilterOperator == Operator.Or)
|| (_fastFilterOperation == Operation.Equal && _fastFilterOperator == Operator.And))
{
_containsValidFilter = false;
}
}
}
else
{
_containsValidFilter = false;
}
}
internal void AddCondition(Condition condition)
{
if (!_containsValidFilter)
{
return;
}
if (_conditionEncountered)
{
if (condition.Operation == _fastFilterOperation)
{
AddProperty(condition.Name, condition.Value);
}
else
{
_containsValidFilter = false;
}
}
else
{
_conditionEncountered = true;
_fastFilterOperation = condition.Operation;
AddProperty(condition.Name, condition.Value);
// Don't support `Contains`.
if (_fastFilterOperation is not Operation.Equal and not Operation.NotEqual)
{
_containsValidFilter = false;
}
}
}
private void AddProperty(string name, string value)
{
if (!_filterDictionaryBuilder.TryGetValue(name, out var values))
{
#if IS_VSTEST_REPO
values = ImmutableHashSet.CreateBuilder<string>(StringComparer.OrdinalIgnoreCase);
#else
values = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
#endif
_filterDictionaryBuilder.Add(name, values);
}
values.Add(value);
}
internal FastFilter? ToFastFilter()
{
return ContainsValidFilter
? new FastFilter(
#if IS_VSTEST_REPO
_filterDictionaryBuilder.ToImmutableDictionary(kvp => kvp.Key, kvp => (ISet<string>)_filterDictionaryBuilder[kvp.Key].ToImmutable()),
#else
_filterDictionaryBuilder,
#endif
_fastFilterOperation,
_fastFilterOperator)
: null;
}
}
}