| | | 1 | | #pragma warning disable RS0030 // Do not use banned APIs |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Concurrent; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Linq; |
| | | 7 | | using System.Linq.Expressions; |
| | | 8 | | using System.Reflection; |
| | | 9 | | using Jellyfin.Database.Implementations.Entities; |
| | | 10 | | using Microsoft.EntityFrameworkCore; |
| | | 11 | | |
| | | 12 | | namespace Jellyfin.Database.Implementations; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Contains a number of query related extensions. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <remarks> |
| | | 18 | | /// Every helper here binds its values through <see cref="EF.Parameter{T}(T)"/>. Values embedded as bare |
| | | 19 | | /// constants are inlined into the SQL as literals, which gives each distinct value its own entry in EF's |
| | | 20 | | /// compiled query cache and its own statement for the database to plan. |
| | | 21 | | /// </remarks> |
| | | 22 | | public static class JellyfinQueryHelperExtensions |
| | | 23 | | { |
| | 2 | 24 | | private static readonly MethodInfo _containsMethodGenericCache = typeof(Enumerable).GetMethods(BindingFlags.Public | |
| | 2 | 25 | | private static readonly MethodInfo _efParameterInstruction = typeof(EF).GetMethod(nameof(EF.Parameter), BindingFlags |
| | 2 | 26 | | private static readonly ConcurrentDictionary<Type, MethodInfo> _containsQueryCache = new(); |
| | 2 | 27 | | private static readonly ConcurrentDictionary<Type, MethodInfo> _efParameterCache = new(); |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Builds an optimised query checking one property against a list of values while maintaining an optimal query. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <typeparam name="TEntity">The entity.</typeparam> |
| | | 33 | | /// <typeparam name="TProperty">The property type to compare.</typeparam> |
| | | 34 | | /// <param name="query">The source query.</param> |
| | | 35 | | /// <param name="oneOf">The list of items to check. An empty list matches nothing.</param> |
| | | 36 | | /// <param name="property">Property expression.</param> |
| | | 37 | | /// <returns>A Query.</returns> |
| | | 38 | | public static IQueryable<TEntity> WhereOneOrMany<TEntity, TProperty>(this IQueryable<TEntity> query, IReadOnlyList<T |
| | | 39 | | { |
| | 497 | 40 | | return query.Where(OneOrManyExpressionBuilder(oneOf, property)); |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Builds an optimised query expression checking one property against a list of values while maintaining an optimal |
| | | 45 | | /// </summary> |
| | | 46 | | /// <typeparam name="TEntity">The entity.</typeparam> |
| | | 47 | | /// <typeparam name="TProperty">The property type to compare.</typeparam> |
| | | 48 | | /// <param name="oneOf">The list of items to check. An empty list matches nothing.</param> |
| | | 49 | | /// <param name="property">Property expression.</param> |
| | | 50 | | /// <returns>A Query.</returns> |
| | | 51 | | public static Expression<Func<TEntity, bool>> OneOrManyExpressionBuilder<TEntity, TProperty>(this IReadOnlyList<TPro |
| | | 52 | | { |
| | 543 | 53 | | ArgumentNullException.ThrowIfNull(oneOf); |
| | 543 | 54 | | ArgumentNullException.ThrowIfNull(property); |
| | | 55 | | |
| | 543 | 56 | | var parameter = Expression.Parameter(typeof(TEntity), "item"); |
| | 543 | 57 | | property = ParameterReplacer.Replace<Func<TEntity, TProperty>, Func<TEntity, TProperty>>(property, property.Para |
| | | 58 | | |
| | 543 | 59 | | if (oneOf.Count == 0) |
| | | 60 | | { |
| | | 61 | | // Fail closed, and without asking the database to unpack an empty collection to prove it. |
| | 1 | 62 | | return Expression.Lambda<Func<TEntity, bool>>(Expression.Constant(false), parameter); |
| | | 63 | | } |
| | | 64 | | |
| | 542 | 65 | | if (oneOf.Count == 1) |
| | | 66 | | { |
| | 447 | 67 | | var value = Expression.Call( |
| | 447 | 68 | | null, |
| | 447 | 69 | | EfParameterFor(typeof(TProperty)), |
| | 447 | 70 | | Expression.Constant(oneOf[0], typeof(TProperty))); |
| | | 71 | | |
| | 447 | 72 | | return Expression.Lambda<Func<TEntity, bool>>( |
| | 447 | 73 | | typeof(TProperty).IsValueType |
| | 447 | 74 | | ? Expression.Equal(property.Body, value) |
| | 447 | 75 | | : Expression.ReferenceEqual(property.Body, value), |
| | 447 | 76 | | parameter); |
| | | 77 | | } |
| | | 78 | | |
| | 95 | 79 | | var containsMethodInfo = _containsQueryCache.GetOrAdd(typeof(TProperty), static (key) => _containsMethodGenericC |
| | | 80 | | |
| | | 81 | | // Binding the whole collection as one parameter keeps the statement identical for any element |
| | | 82 | | // count, instead of emitting one placeholder per element. |
| | 95 | 83 | | return Expression.Lambda<Func<TEntity, bool>>( |
| | 95 | 84 | | Expression.Call( |
| | 95 | 85 | | null, |
| | 95 | 86 | | containsMethodInfo, |
| | 95 | 87 | | Expression.Call(null, EfParameterFor(oneOf.GetType()), Expression.Constant(oneOf)), |
| | 95 | 88 | | property.Body), |
| | 95 | 89 | | parameter); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | private static MethodInfo EfParameterFor(Type type) |
| | | 93 | | { |
| | 542 | 94 | | return _efParameterCache.GetOrAdd(type, static (key) => _efParameterInstruction.MakeGenericMethod(key)); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Builds a query that checks referenced ItemValues for a cross BaseItem lookup. |
| | | 99 | | /// </summary> |
| | | 100 | | /// <param name="baseQuery">The source query.</param> |
| | | 101 | | /// <param name="context">The database context.</param> |
| | | 102 | | /// <param name="itemValueType">The type of item value to reference.</param> |
| | | 103 | | /// <param name="referenceIds">The list of BaseItem ids to check matches.</param> |
| | | 104 | | /// <param name="invert">If set an exclusion check is performed instead.</param> |
| | | 105 | | /// <returns>A Query.</returns> |
| | | 106 | | public static IQueryable<BaseItemEntity> WhereReferencedItem( |
| | | 107 | | this IQueryable<BaseItemEntity> baseQuery, |
| | | 108 | | JellyfinDbContext context, |
| | | 109 | | ItemValueType itemValueType, |
| | | 110 | | IReadOnlyList<Guid> referenceIds, |
| | | 111 | | bool invert = false) |
| | | 112 | | { |
| | 0 | 113 | | return baseQuery.WhereReferencedItem(context, [itemValueType], referenceIds, invert); |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Builds a query that checks referenced ItemValues of any of the given types for a cross BaseItem lookup. |
| | | 118 | | /// </summary> |
| | | 119 | | /// <param name="baseQuery">The source query.</param> |
| | | 120 | | /// <param name="context">The database context.</param> |
| | | 121 | | /// <param name="itemValueTypes">The types of item value to reference.</param> |
| | | 122 | | /// <param name="referenceIds">The list of BaseItem ids to check matches.</param> |
| | | 123 | | /// <param name="invert">If set an exclusion check is performed instead.</param> |
| | | 124 | | /// <returns>A Query.</returns> |
| | | 125 | | /// <remarks> |
| | | 126 | | /// Matching is on CleanName alone. Genre/artist/album etc items do not set an ItemValue of their own |
| | | 127 | | /// type, so the referenced item's Type is never consulted and ids whose names clean to the same value |
| | | 128 | | /// are interchangeable across types. |
| | | 129 | | /// </remarks> |
| | | 130 | | public static IQueryable<BaseItemEntity> WhereReferencedItem( |
| | | 131 | | this IQueryable<BaseItemEntity> baseQuery, |
| | | 132 | | JellyfinDbContext context, |
| | | 133 | | IReadOnlyList<ItemValueType> itemValueTypes, |
| | | 134 | | IReadOnlyList<Guid> referenceIds, |
| | | 135 | | bool invert = false) |
| | | 136 | | { |
| | 0 | 137 | | ArgumentNullException.ThrowIfNull(context); |
| | | 138 | | |
| | | 139 | | // Flat sub-selects rather than a correlated .Any(...Any(...)). |
| | 0 | 140 | | var referencedCleanValues = context.BaseItems |
| | 0 | 141 | | .Where(OneOrManyExpressionBuilder<BaseItemEntity, Guid>(referenceIds, e => e.Id)) |
| | 0 | 142 | | .Select(e => e.CleanName); |
| | | 143 | | |
| | 0 | 144 | | var matchingItemIds = context.ItemValuesMap |
| | 0 | 145 | | .Where(OneOrManyExpressionBuilder<ItemValueMap, ItemValueType>(itemValueTypes, m => m.ItemValue.Type)) |
| | 0 | 146 | | .Where(m => referencedCleanValues.Contains(m.ItemValue.CleanValue)) |
| | 0 | 147 | | .Select(m => m.ItemId); |
| | | 148 | | |
| | 0 | 149 | | return invert |
| | 0 | 150 | | ? baseQuery.Where(e => !matchingItemIds.Contains(e.Id)) |
| | 0 | 151 | | : baseQuery.Where(e => matchingItemIds.Contains(e.Id)); |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Filters items that have any of the specified providers, optionally restricted to given values. |
| | | 156 | | /// </summary> |
| | | 157 | | /// <param name="baseQuery">The source query.</param> |
| | | 158 | | /// <param name="providerIds">Dictionary mapping provider names to values to match. An empty value array matches any |
| | | 159 | | /// <returns>A filtered query.</returns> |
| | | 160 | | public static IQueryable<BaseItemEntity> WhereHasAnyProviderIds( |
| | | 161 | | this IQueryable<BaseItemEntity> baseQuery, |
| | | 162 | | IReadOnlyDictionary<string, string[]> providerIds) |
| | | 163 | | { |
| | 0 | 164 | | return baseQuery.WhereProviderMatch(Flatten(providerIds), false); |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// Filters items that have any of the specified providers, optionally restricted to a given value. |
| | | 169 | | /// </summary> |
| | | 170 | | /// <param name="baseQuery">The source query.</param> |
| | | 171 | | /// <param name="providerIds">Dictionary mapping provider names to optional values. An empty value matches any value |
| | | 172 | | /// <returns>A filtered query.</returns> |
| | | 173 | | public static IQueryable<BaseItemEntity> WhereHasAnyProviderId( |
| | | 174 | | this IQueryable<BaseItemEntity> baseQuery, |
| | | 175 | | IReadOnlyDictionary<string, string> providerIds) |
| | | 176 | | { |
| | 0 | 177 | | return baseQuery.WhereProviderMatch(providerIds, false); |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | /// <summary> |
| | | 181 | | /// Excludes items that have any of the specified providers, optionally restricted to a given value. |
| | | 182 | | /// </summary> |
| | | 183 | | /// <param name="baseQuery">The source query.</param> |
| | | 184 | | /// <param name="providerIds">Dictionary mapping provider names to optional values. An empty value excludes any valu |
| | | 185 | | /// <returns>A filtered query.</returns> |
| | | 186 | | public static IQueryable<BaseItemEntity> WhereExcludeProviderIds( |
| | | 187 | | this IQueryable<BaseItemEntity> baseQuery, |
| | | 188 | | IReadOnlyDictionary<string, string> providerIds) |
| | | 189 | | { |
| | 0 | 190 | | return baseQuery.WhereProviderMatch(providerIds, true); |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | private static IEnumerable<KeyValuePair<string, string>> Flatten(IReadOnlyDictionary<string, string[]> providerIds) |
| | | 194 | | { |
| | 0 | 195 | | ArgumentNullException.ThrowIfNull(providerIds); |
| | | 196 | | |
| | 0 | 197 | | foreach (var (provider, values) in providerIds) |
| | | 198 | | { |
| | 0 | 199 | | if (values is null || values.Length == 0) |
| | | 200 | | { |
| | 0 | 201 | | yield return new KeyValuePair<string, string>(provider, string.Empty); |
| | 0 | 202 | | continue; |
| | | 203 | | } |
| | | 204 | | |
| | 0 | 205 | | foreach (var value in values) |
| | | 206 | | { |
| | 0 | 207 | | yield return new KeyValuePair<string, string>(provider, value); |
| | | 208 | | } |
| | 0 | 209 | | } |
| | 0 | 210 | | } |
| | | 211 | | |
| | | 212 | | /// <summary> |
| | | 213 | | /// Matches items against a set of (provider, value) pairs, where an empty value means any value for |
| | | 214 | | /// that provider. Emits a single EXISTS over the provider collection with the predicates OR'd, rather |
| | | 215 | | /// than one subquery per predicate group. |
| | | 216 | | /// </summary> |
| | | 217 | | private static IQueryable<BaseItemEntity> WhereProviderMatch( |
| | | 218 | | this IQueryable<BaseItemEntity> baseQuery, |
| | | 219 | | IEnumerable<KeyValuePair<string, string>> providerIds, |
| | | 220 | | bool invert) |
| | | 221 | | { |
| | 0 | 222 | | ArgumentNullException.ThrowIfNull(providerIds); |
| | | 223 | | |
| | 0 | 224 | | var existenceOnly = new List<string>(); |
| | 0 | 225 | | var specificValues = new List<string>(); |
| | 0 | 226 | | foreach (var (provider, value) in providerIds) |
| | | 227 | | { |
| | 0 | 228 | | if (string.IsNullOrEmpty(value)) |
| | | 229 | | { |
| | 0 | 230 | | existenceOnly.Add(provider); |
| | | 231 | | } |
| | | 232 | | else |
| | | 233 | | { |
| | 0 | 234 | | specificValues.Add(provider + ":" + value); |
| | | 235 | | } |
| | | 236 | | } |
| | | 237 | | |
| | 0 | 238 | | if (existenceOnly.Count == 0 && specificValues.Count == 0) |
| | | 239 | | { |
| | 0 | 240 | | return baseQuery; |
| | | 241 | | } |
| | | 242 | | |
| | 0 | 243 | | var predicate = ProviderPredicate(existenceOnly, specificValues); |
| | | 244 | | |
| | | 245 | | // NOT EXISTS rather than NOT IN: the latter yields no rows at all if the subquery can produce NULL. |
| | 0 | 246 | | return invert |
| | 0 | 247 | | ? baseQuery.Where(e => !e.Provider!.AsQueryable().Any(predicate)) |
| | 0 | 248 | | : baseQuery.Where(e => e.Provider!.AsQueryable().Any(predicate)); |
| | | 249 | | } |
| | | 250 | | |
| | | 251 | | private static Expression<Func<BaseItemProvider, bool>> ProviderPredicate( |
| | | 252 | | IReadOnlyList<string> existenceOnly, |
| | | 253 | | IReadOnlyList<string> specificValues) |
| | | 254 | | { |
| | 0 | 255 | | var byProvider = existenceOnly.OneOrManyExpressionBuilder<BaseItemProvider, string>(p => p.ProviderId); |
| | 0 | 256 | | var byPair = specificValues.OneOrManyExpressionBuilder<BaseItemProvider, string>(p => p.ProviderId + ":" + p.Pro |
| | | 257 | | |
| | | 258 | | // Both builders mint their own parameter; rebind so the two bodies can share one lambda. |
| | 0 | 259 | | var parameter = byProvider.Parameters[0]; |
| | 0 | 260 | | var reboundPair = ParameterReplacer.Replace<Func<BaseItemProvider, bool>, Func<BaseItemProvider, bool>>(byPair, |
| | | 261 | | |
| | 0 | 262 | | return Expression.Lambda<Func<BaseItemProvider, bool>>( |
| | 0 | 263 | | Expression.OrElse(byProvider.Body, reboundPair.Body), |
| | 0 | 264 | | parameter); |
| | | 265 | | } |
| | | 266 | | |
| | | 267 | | internal static class ParameterReplacer |
| | | 268 | | { |
| | | 269 | | // Produces an expression identical to 'expression' |
| | | 270 | | // except with 'source' parameter replaced with 'target' expression. |
| | | 271 | | internal static Expression<TOutput> Replace<TInput, TOutput>( |
| | | 272 | | Expression<TInput> expression, |
| | | 273 | | ParameterExpression source, |
| | | 274 | | ParameterExpression target) |
| | | 275 | | { |
| | 543 | 276 | | return new ParameterReplacerVisitor<TOutput>(source, target) |
| | 543 | 277 | | .VisitAndConvert(expression); |
| | | 278 | | } |
| | | 279 | | |
| | | 280 | | private sealed class ParameterReplacerVisitor<TOutput> : ExpressionVisitor |
| | | 281 | | { |
| | | 282 | | private readonly ParameterExpression _source; |
| | | 283 | | private readonly ParameterExpression _target; |
| | | 284 | | |
| | 543 | 285 | | public ParameterReplacerVisitor(ParameterExpression source, ParameterExpression target) |
| | | 286 | | { |
| | 543 | 287 | | _source = source; |
| | 543 | 288 | | _target = target; |
| | 543 | 289 | | } |
| | | 290 | | |
| | | 291 | | internal Expression<TOutput> VisitAndConvert<T>(Expression<T> root) |
| | | 292 | | { |
| | 543 | 293 | | return (Expression<TOutput>)VisitLambda(root); |
| | | 294 | | } |
| | | 295 | | |
| | | 296 | | protected override Expression VisitLambda<T>(Expression<T> node) |
| | | 297 | | { |
| | | 298 | | // Leave all parameters alone except the one we want to replace. |
| | 543 | 299 | | var parameters = node.Parameters.Select(p => p == _source ? _target : p); |
| | | 300 | | |
| | 543 | 301 | | return Expression.Lambda<TOutput>(Visit(node.Body), parameters); |
| | | 302 | | } |
| | | 303 | | |
| | | 304 | | protected override Expression VisitParameter(ParameterExpression node) |
| | | 305 | | { |
| | | 306 | | // Replace the source with the target, visit other params as usual. |
| | 543 | 307 | | return node == _source ? _target : base.VisitParameter(node); |
| | | 308 | | } |
| | | 309 | | } |
| | | 310 | | } |
| | | 311 | | } |